|
| 1 | +# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1) |
| 2 | + |
| 3 | +BeforeDiscovery { |
| 4 | + function global:FilterOutCommonParams { |
| 5 | + param ($Params) |
| 6 | + $commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters + |
| 7 | + [System.Management.Automation.PSCmdlet]::OptionalCommonParameters |
| 8 | + $params | Where-Object { $_.Name -notin $commonParameters } | Sort-Object -Property Name -Unique |
| 9 | + } |
| 10 | + |
| 11 | + $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest |
| 12 | + $outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output' |
| 13 | + $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName |
| 14 | + $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion |
| 15 | + $outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1" |
| 16 | + |
| 17 | + # Get module commands |
| 18 | + # Remove all versions of the module from the session. Pester can't handle multiple versions. |
| 19 | + Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore |
| 20 | + Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop |
| 21 | + $params = @{ |
| 22 | + Module = (Get-Module $env:BHProjectName) |
| 23 | + CommandType = [System.Management.Automation.CommandTypes[]]'Cmdlet, Function' # Not alias |
| 24 | + } |
| 25 | + if ($PSVersionTable.PSVersion.Major -lt 6) { |
| 26 | + $params.CommandType[0] += 'Workflow' |
| 27 | + } |
| 28 | + $commands = Get-Command @params |
| 29 | + $global:customEnumTypes = @( |
| 30 | + # Add custom enums here |
| 31 | + ) |
| 32 | + |
| 33 | + ## When testing help, remember that help is cached at the beginning of each session. |
| 34 | + ## To test, restart session. |
| 35 | +} |
| 36 | + |
| 37 | +Describe "Test help for <_.Name>" -ForEach $commands { |
| 38 | + |
| 39 | + BeforeDiscovery { |
| 40 | + # Get command help, parameters, and links |
| 41 | + $command = $_ |
| 42 | + $commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue |
| 43 | + $commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters |
| 44 | + $commandParameterNames = $commandParameters.Name |
| 45 | + $helpLinks = $commandHelp.relatedLinks.navigationLink.uri |
| 46 | + } |
| 47 | + |
| 48 | + BeforeAll { |
| 49 | + # These vars are needed in both discovery and test phases so we need to duplicate them here |
| 50 | + $command = $_ |
| 51 | + $commandName = $_.Name |
| 52 | + $commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue |
| 53 | + $commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters |
| 54 | + $commandParameterNames = $commandParameters.Name |
| 55 | + $helpParameters = global:FilterOutCommonParams -Params $commandHelp.Parameters.Parameter |
| 56 | + $helpParameterNames = $helpParameters.Name |
| 57 | + } |
| 58 | + |
| 59 | + # If help is not found, synopsis in auto-generated help is the syntax diagram |
| 60 | + It 'Help is not auto-generated' { |
| 61 | + $commandHelp.Synopsis | Should -Not -BeLike '*`[`<CommonParameters`>`]*' |
| 62 | + } |
| 63 | + |
| 64 | + # Should be a description for every function |
| 65 | + It "Has description" { |
| 66 | + $commandHelp.Description | Should -Not -BeNullOrEmpty |
| 67 | + } |
| 68 | + |
| 69 | + # Should be at least one example |
| 70 | + It "Has example code" { |
| 71 | + ($commandHelp.Examples.Example | Select-Object -First 1).Code | Should -Not -BeNullOrEmpty |
| 72 | + } |
| 73 | + |
| 74 | + # Should be at least one example description |
| 75 | + It "Has example help" { |
| 76 | + ($commandHelp.Examples.Example.Remarks | Select-Object -First 1).Text | Should -Not -BeNullOrEmpty |
| 77 | + } |
| 78 | + |
| 79 | + It "Help link <_> is valid" -ForEach $helpLinks { |
| 80 | + (Invoke-WebRequest -Uri $_ -UseBasicParsing).StatusCode | Should -Be '200' |
| 81 | + } |
| 82 | + |
| 83 | + Context "Parameter <_.Name>" -ForEach $commandParameters { |
| 84 | + |
| 85 | + BeforeAll { |
| 86 | + $parameter = $_ |
| 87 | + $parameterName = $parameter.Name |
| 88 | + $parameterHelp = $commandHelp.parameters.parameter | Where-Object Name -EQ $parameterName |
| 89 | + $parameterHelpType = if ($parameterHelp.ParameterValue) { $parameterHelp.ParameterValue.Trim() } |
| 90 | + } |
| 91 | + |
| 92 | + # Should be a description for every parameter |
| 93 | + It "Has description" { |
| 94 | + $parameterHelp.Description.Text | Should -Not -BeNullOrEmpty |
| 95 | + } |
| 96 | + |
| 97 | + # Required value in Help should match IsMandatory property of parameter |
| 98 | + It "Has correct [mandatory] value" { |
| 99 | + $codeMandatory = $_.IsMandatory.toString() |
| 100 | + $parameterHelp.Required | Should -Be $codeMandatory |
| 101 | + } |
| 102 | + |
| 103 | + # Parameter type in help should match code |
| 104 | + It "Has correct parameter type" { |
| 105 | + # If it's a custom object it won't show up in the help, so we skip it. |
| 106 | + if ($parameter.ParameterType -in $global:customEnumTypes) { |
| 107 | + Set-ItResult -Skipped -Because 'Custom object types are not shown in help.' |
| 108 | + } |
| 109 | + |
| 110 | + $parameterHelpType | Should -Be $parameter.ParameterType.Name |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + Context "Test <_> help parameter help for <commandName>" -ForEach $helpParameterNames { |
| 115 | + |
| 116 | + # Shouldn't find extra parameters in help. |
| 117 | + It "finds help parameter in code: <_>" { |
| 118 | + $_ -in $parameterNames | Should -Be $true |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments