Created
April 26, 2020 19:43
-
-
Save Splaxi/3abd9f1b89cd3efa94836cde83a2338a to your computer and use it in GitHub Desktop.
Pester test that will validate all parameters that doesn't have a default value and see if there is an example with the specified parameter or not. Fails if not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Invoke-Pester -Path "C:\GIT\GITHUB\d365fo.tools.Workspace\Test-ParametersInExamples.ps1" -OutputFile 'C:\Temp\d365fo.tools_pester_results.xml' -OutputFormat NUnitXml | |
C:\GIT\GITHUB\dbatools.Workspace\ReportUnit.exe "C:\Temp\d365fo.tools_pester_results.xml" "C:\Temp\PesterReport-d365fo.tools.html" | |
Start-Process "C:\Temp\PesterReport-d365fo.tools.html" | |
#> | |
$moduleName = "d365fo.tools" | |
$path = "C:\GIT\GITHUB\$moduleName.Workspace\$moduleName\$moduleName" | |
Import-Module $path -Force | |
$excludeCommands = @() | |
$excludeParameters = @('EnableException' | |
, 'TrustedConnection' | |
, 'Force' | |
, 'Temporary' | |
, 'OutputCommandOnly' | |
, 'ShowOriginalProgress' | |
, 'OutputAsHashtable' | |
) | |
$commandsRaw = Get-Command -Module $moduleName | |
if ($excludeCommands.Count -gt 0) { | |
$commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch | |
} | |
else { | |
$commands = $commandsRaw | |
} | |
#Limit the number of commands that you will be working against. | |
# $commands = @('Switch-D365ActiveDatabase','Disable-D365MaintenanceMode','Enable-D365MaintenanceMode') | |
foreach ( $commandName in $commands) { | |
# command to be tested | |
# get all examples from the help | |
$examples = Get-Help $commandName -Examples | |
$parameters = (Get-Help $commandName -Full).parameters | |
# make a describe block that will contain tests for this | |
Describe "Parameters without default vaules from $commandName" { | |
foreach ($parm in $parameters.parameter) { | |
if ($parm.defaultValue -ne "False") { continue } | |
$parmName = $parm.name | |
if ($parmName -in $excludeParameters) { continue } | |
$res = $false | |
foreach ($exampleObject in $examples.Examples.Example) { | |
if ($res) { continue } | |
$example = $exampleObject.Code -replace "`n.*" -replace "PS C:\\>" | |
$res = $example -match "-$parmName( *|\:)" | |
} | |
It "$parmName is present in an example" { | |
$res | Should -BeTrue | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment