Skip to content

Instantly share code, notes, and snippets.

@Splaxi
Created October 19, 2018 12:07
Show Gist options
  • Save Splaxi/dd12e489cc0d3f231394f5ac2243c127 to your computer and use it in GitHub Desktop.
Save Splaxi/dd12e489cc0d3f231394f5ac2243c127 to your computer and use it in GitHub Desktop.
#Fill in the command that you want to generate an pester test for its examples
$commandName = "New-D365SelfSignedCertificate"
#The path where it traverse it's ways to get the file. Filename has to match the commandname
$file = Get-ChildItem -Path "C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools" -Recurse -Filter "$commandName.ps1" | Select-Object -First 1
# dot source load the file into current session
. $($file.Fullname)
$sb = [System.Text.StringBuilder]::new()
$null = $sb.AppendLine("`$commandName = `"$commandName`"")
$examples = Get-Help $commandName -Examples
$examples.Examples.Example | ForEach-Object {
$null = $sb.AppendLine('################################### New Example test ###################################')
$null = $sb.AppendLine('')
$exampleText = $_.Code -replace "`n.*" -replace "PS C:\\>"
$exampleText = $exampleText -replace "\`$", "``$"
$exampleText = $exampleText -replace '"', '`"'
$null = $sb.AppendLine("`$exampleRaw = `"$exampleText`"")
$null = $sb.AppendLine('#Remember to escape any variables names in the line above.')
$null = $sb.AppendLine('#Remember to you need to output $true to the pester test, otherwise is fails.')
$null = $sb.AppendLine('#; `$var -eq `$true')
$null = $sb.AppendLine('')
$null = $sb.AppendLine("#Here you declare any variable(s) you need to complete the test.")
$null = $sb.AppendLine('')
$null = $sb.AppendLine('$example = $exampleRaw -replace "`n.*" -replace "PS C:\\>"')
$null = $sb.AppendLine('')
$null = $sb.AppendLine('Describe "Specific example testing for $commandName" {')
$null = $sb.AppendLine('')
$null = $sb.AppendLine('It "Example - $example" {')
$null = $sb.AppendLine("# mock the tested command so we don't actually do anything")
$null = $sb.AppendLine("# because it can be unsafe and we don't have the environment setup")
$null = $sb.AppendLine('# (so the only thing we are testing is that the code is semantically')
$null = $sb.AppendLine('# correct and provides all the needed params)')
$null = $sb.AppendLine('Mock $commandName {')
$null = $sb.AppendLine('# I am returning true here,')
$null = $sb.AppendLine('# but some of the examples drill down to the returned object')
$null = $sb.AppendLine('# so in strict mode we would fail')
$null = $sb.AppendLine('$true')
$null = $sb.AppendLine('}')
$null = $sb.AppendLine('# here simply invoke the example')
$null = $sb.AppendLine('$result = Invoke-Expression $example')
$null = $sb.AppendLine('# and check that we got result from the mock')
$null = $sb.AppendLine('$result | Should -BeTrue')
$null = $sb.AppendLine('}')
$null = $sb.AppendLine('}')
}
$null = $sb.AppendLine('################################### Entire help loaded ###################################')
$null = $sb.AppendLine('')
$null = $sb.AppendLine('<#')
$sb.ToString() | Out-File "C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\tests\examples\$commandName.Tests.ps1"
Get-Help $commandName -Full | Out-File "C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\tests\examples\$commandName.Tests.ps1" -Append
'#>'| Out-File "C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\tests\examples\$commandName.Tests.ps1" -Append
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment