1- <#
1+ <#
22. SYNOPSIS
3- Tests the PowerShell help for the commands in a module.
3+ Tests the PowerShell help for the commands in a module.
44
55. DESCRIPTION
6- This Pester test verifies that the commands in a module have basic help content.
6+ This Pester test verifies that the commands in a module have basic help content.
77It works on all command types and both comment-based and XML help.
88
99This test verifies that Get-Help is not autogenerating help because it cannot
@@ -21,15 +21,15 @@ When testing attributes of parameters that appear in multiple parameter sets,
2121this test uses the parameter that appears in the default parameter set, if one
2222is defined.
2323
24- You can run this Tests file from any location. For a help test that is located in a module
24+ You can run this Tests file from any location. For a help test that is located in a module
2525directory, use https://github.com/juneb/PesterTDD/InModule.Help.Tests.ps1
2626
2727. PARAMETER ModuleName
2828Enter the name of the module to test. You can enter only one name at a time. This
2929parameter is mandatory.
3030
3131. PARAMETER RequiredVersion
32- Enter the version of the module to test. This parameter is optional. If you
32+ Enter the version of the module to test. This parameter is optional. If you
3333omit it, the test runs on the latest version of the module in $env:PSModulePath.
3434
3535. EXAMPLE
@@ -57,13 +57,13 @@ Param
5757 [ValidateScript ({ Get-Module - ListAvailable - Name $_ })]
5858 [string ]
5959 $ModuleName = ' PSScriptAnalyzer' ,
60-
60+
6161 [Parameter (Mandatory = $false )]
6262 [System.Version ]
6363 $RequiredVersion
6464)
6565
66- # Requires -Module @ {ModuleName = ' Pester' ; ModuleVersion = ' 3.4.0' }
66+ # # Requires -Module @ {ModuleName = ' Pester' ; ModuleVersion = ' 3.4.0' }
6767
6868<#
6969. SYNOPSIS
@@ -87,16 +87,16 @@ Enter a CommandInfo object, such as the object that Get-Command returns. You
8787can also pipe a CommandInfo object to the function.
8888
8989This parameter takes a CommandInfo object, instead of a command name, so
90- you can use the parameters of Get-Command to specify the module and version
90+ you can use the parameters of Get-Command to specify the module and version
9191of the command.
9292
9393. EXAMPLE
9494PS C:\> Get-ParametersDefaultFirst -Command (Get-Command New-Guid)
95- This command uses the Command parameter to specify the command to
95+ This command uses the Command parameter to specify the command to
9696Get-ParametersDefaultFirst
9797
9898. EXAMPLE
99- PS C:\> Get-Command New-Guid | Get-ParametersDefaultFirst
99+ PS C:\> Get-Command New-Guid | Get-ParametersDefaultFirst
100100You can also pipe a CommandInfo object to Get-ParametersDefaultFirst
101101
102102. EXAMPLE
@@ -107,7 +107,7 @@ command runs Get-Command module-qualified name value.
107107. EXAMPLE
108108PS C:\> $ModuleSpec = @{ModuleName='BetterCredentials';RequiredVersion=4.3}
109109PS C:\> Get-Command -FullyQualifiedName $ModuleSpec | Get-ParametersDefaultFirst
110- This command uses a Microsoft.PowerShell.Commands.ModuleSpecification object to
110+ This command uses a Microsoft.PowerShell.Commands.ModuleSpecification object to
111111specify the module and version. You can also use it to specify the module GUID.
112112Then, it pipes the CommandInfo object to Get-ParametersDefaultFirst.
113113#>
@@ -119,7 +119,7 @@ function Get-ParametersDefaultFirst {
119119 [System.Management.Automation.CommandInfo ]
120120 $Command
121121 )
122-
122+
123123 BEGIN {
124124 $Common = ' Debug' , ' ErrorAction' , ' ErrorVariable' , ' InformationAction' , ' InformationVariable' , ' OutBuffer' , ' OutVariable' , ' PipelineVariable' , ' Verbose' , ' WarningAction' , ' WarningVariable'
125125 $parameters = @ ()
@@ -128,7 +128,7 @@ function Get-ParametersDefaultFirst {
128128 if ($defaultPSetName = $Command.DefaultParameterSet ) {
129129 $defaultParameters = ($Command.ParameterSets | Where-Object Name -eq $defaultPSetName ).parameters | Where-Object Name -NotIn $common
130130 $otherParameters = ($Command.ParameterSets | Where-Object Name -ne $defaultPSetName ).parameters | Where-Object Name -NotIn $common
131-
131+
132132 $parameters = $defaultParameters
133133 if ($parameters -and $otherParameters ) {
134134 $otherParameters | ForEach-Object {
@@ -142,8 +142,8 @@ function Get-ParametersDefaultFirst {
142142 else {
143143 $parameters = $Command.ParameterSets.Parameters | Where-Object Name -NotIn $common | Sort-Object Name - Unique
144144 }
145-
146-
145+
146+
147147 return $parameters
148148 }
149149 END { }
@@ -161,7 +161,7 @@ following properties:
161161 -- [string] $CommandName
162162 -- [string] $ModuleName (or PSSnapin name)
163163 -- [string] $ModuleVersion (or PowerShell Version)
164-
164+
165165. PARAMETER CommandInfo
166166Specifies a Commandinfo object, e.g. (Get-Command Get-Item).
167167
@@ -191,7 +191,7 @@ function Get-CommandVersion {
191191 [System.Management.Automation.CommandInfo ]
192192 $CommandInfo
193193 )
194-
194+
195195 if ((-not ((($commandModuleName = $CommandInfo.Module.Name ) -and ($commandVersion = $CommandInfo.Module.Version )) -or
196196 (($commandModuleName = $CommandInfo.PSSnapin ) -and ($commandVersion = $CommandInfo.PSSnapin.Version ))))) {
197197 Write-Error " For $ ( $CommandInfo.Name ) : Can't find PSSnapin/module name and version"
@@ -215,7 +215,7 @@ Import-Module $ModuleName -RequiredVersion $RequiredVersion -ErrorAction Stop
215215$ms = $null
216216$commands = $null
217217$paramBlackList = @ ()
218- if ($PSVersionTable.PSVersion -lt [Version ]' 5.0' ) {
218+ if ($PSVersionTable.PSVersion -lt [Version ]' 5.0.0 ' ) {
219219 $ms = New-Object - TypeName ' Microsoft.PowerShell.Commands.ModuleSpecification' - ArgumentList $ModuleName
220220 $commands = Get-Command - Module $ms.Name
221221 $paramBlackList += ' SaveDscDependency'
@@ -230,79 +230,80 @@ else {
230230
231231foreach ($command in $commands ) {
232232 $commandName = $command.Name
233-
233+
234234 # Get the module name and version of the command. Used in the Describe name.
235235 $commandModuleVersion = Get-CommandVersion - CommandInfo $command
236-
236+
237237 # The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
238238 $Help = Get-Help $ModuleName \$commandName - ErrorAction SilentlyContinue
239239 if ($Help.Synopsis -like ' *`[`<CommonParameters`>`]*' ) {
240240 $Help = Get-Help $commandName - ErrorAction SilentlyContinue
241241 }
242-
242+
243243 Describe " Test help for $commandName in $ ( $commandModuleVersion.ModuleName ) ($ ( $commandModuleVersion.Version ) )" {
244-
244+
245245 # If help is not found, synopsis in auto-generated help is the syntax diagram
246246 It " should not be auto-generated" {
247- $Help.Synopsis | Should Not BeLike ' *`[`<CommonParameters`>`]*'
247+ # Replace pester BeLike with powershell -like as pester 3.3.9 does not support BeLike
248+ $Help.Synopsis -like ' *`[`<CommonParameters`>`]*' | Should Be $false
248249 }
249-
250+
250251 # Should be a description for every function
251252 It " gets description for $commandName " {
252253 $Help.Description | Should Not BeNullOrEmpty
253254 }
254-
255+
255256 # Should be at least one example
256257 It " gets example code from $commandName " {
257258 ($Help.Examples.Example | Select-Object - First 1 ).Code | Should Not BeNullOrEmpty
258259 }
259-
260+
260261 # Should be at least one example description
261262 It " gets example help from $commandName " {
262263 ($Help.Examples.Example.Remarks | Select-Object - First 1 ).Text | Should Not BeNullOrEmpty
263264 }
264-
265+
265266 Context " Test parameter help for $commandName " {
266-
267+
267268 $Common = ' Debug' , ' ErrorAction' , ' ErrorVariable' , ' InformationAction' , ' InformationVariable' , ' OutBuffer' , ' OutVariable' ,
268269 ' PipelineVariable' , ' Verbose' , ' WarningAction' , ' WarningVariable'
269-
270- # Get parameters. When >1 parameter with same name,
270+
271+ # Get parameters. When >1 parameter with same name,
271272 # get parameter from the default parameter set, if any.
272273 $parameters = Get-ParametersDefaultFirst - Command $command
273-
274+
274275 $parameterNames = $parameters.Name
275276 $HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object - Unique
276-
277+
277278 foreach ($parameter in $parameters ) {
278279 if ($parameter -in $paramBlackList ) {
279280 continue
280281 }
281282 $parameterName = $parameter.Name
282283 $parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
283-
284+
284285 # Should be a description for every parameter
285286 It " gets help for parameter: $parameterName : in $commandName " {
286- # `$parameterHelp.Description.Text | Should Not BeNullOrEmpty` fails for -Settings paramter
287+ # `$parameterHelp.Description.Text | Should Not BeNullOrEmpty` fails for -Settings paramter
287288 # without explicit [string] casting on the Text property
288289 [string ]::IsNullOrEmpty($parameterHelp.Description.Text ) | Should Be $false
289290 }
290-
291+
291292 # Required value in Help should match IsMandatory property of parameter
292293 It " help for $parameterName parameter in $commandName has correct Mandatory value" {
293294 $codeMandatory = $parameter.IsMandatory.toString ()
294295 $parameterHelp.Required | Should Be $codeMandatory
295296 }
296-
297+
297298 # Parameter type in Help should match code
298299 It " help for $commandName has correct parameter type for $parameterName " {
299300 $codeType = $parameter.ParameterType.Name
300301 # To avoid calling Trim method on a null object.
301302 $helpType = if ($parameterHelp.parameterValue ) { $parameterHelp.parameterValue.Trim () }
302- $helpType | Should be $codeType
303+ $helpType | Should be $codeType
303304 }
304305 }
305-
306+
306307 foreach ($helpParm in $HelpParameterNames ) {
307308 if ($helpParm -in $paramBlackList ) {
308309 continue
0 commit comments