|
1 | | -# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/main/Module.Help.Tests.ps1) |
| 1 | +# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1) |
2 | 2 |
|
3 | 3 | BeforeDiscovery { |
4 | | - if ($null -eq $env:BHPSModuleManifest) { |
5 | | - & "$PSScriptRoot/../Build.ps1" -Task Init |
| 4 | + if ($null -eq $env:BHProjectName) { |
| 5 | + .\build.ps1 -Task Build |
6 | 6 | } |
7 | 7 | function global:FilterOutCommonParams { |
8 | 8 | param ($Params) |
9 | | - $commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters + |
10 | | - [System.Management.Automation.PSCmdlet]::OptionalCommonParameters |
11 | | - $params | Where-Object { $_.Name -notin $commonParameters } | Sort-Object -Property Name -Unique |
| 9 | + $commonParams = [System.Management.Automation.PSCmdlet]::OptionalCommonParameters + |
| 10 | + [System.Management.Automation.PSCmdlet]::CommonParameters |
| 11 | + $params | Where-Object { $_.Name -notin $commonParams } | Sort-Object -Property Name -Unique |
12 | 12 | } |
13 | 13 |
|
14 | 14 | $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest |
15 | | - $outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output' |
| 15 | + $outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'output' |
16 | 16 | $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName |
17 | 17 | $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion |
18 | 18 | $outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1" |
@@ -42,79 +42,75 @@ Describe "Test help for <_.Name>" -ForEach $commands { |
42 | 42 | $commandHelp = Get-Help $command.Name -ErrorAction SilentlyContinue |
43 | 43 | $commandParameters = global:FilterOutCommonParams -Params $command.ParameterSets.Parameters |
44 | 44 | $commandParameterNames = $commandParameters.Name |
45 | | - $helpLinks = @($commandHelp.relatedLinks.navigationLink.uri | Where-Object { $_ }) |
46 | | - $helpParameters = global:FilterOutCommonParams -Params $commandHelp.Parameters.Parameter |
47 | | - $helpParameterNames = $helpParameters.Name |
| 45 | + # $helpLinks = $commandHelp.relatedLinks.navigationLink.uri |
48 | 46 | } |
49 | 47 |
|
50 | 48 | BeforeAll { |
51 | 49 | # These vars are needed in both discovery and test phases so we need to duplicate them here |
52 | | - $script:command = $_ |
53 | | - $script:commandName = $_.Name |
54 | | - $script:commandHelp = Get-Help $script:command.Name -ErrorAction SilentlyContinue |
55 | | - $script:commandParameters = global:FilterOutCommonParams -Params $script:command.ParameterSets.Parameters |
56 | | - $script:commandParameterNames = $script:commandParameters.Name |
57 | | - $script:helpParameters = global:FilterOutCommonParams -Params $script:commandHelp.Parameters.Parameter |
58 | | - $script:helpParameterNames = $script:helpParameters.Name |
| 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 |
59 | 57 | } |
60 | 58 |
|
61 | 59 | # If help is not found, synopsis in auto-generated help is the syntax diagram |
62 | 60 | It 'Help is not auto-generated' { |
63 | | - $script:commandHelp.Synopsis | Should -Not -BeLike '*`[`<CommonParameters`>`]*' |
| 61 | + $commandHelp.Synopsis | Should -Not -BeLike '*`[`<CommonParameters`>`]*' |
64 | 62 | } |
65 | 63 |
|
66 | 64 | # Should be a description for every function |
67 | 65 | It "Has description" { |
68 | | - $script:commandHelp.Description | Should -Not -BeNullOrEmpty |
| 66 | + $commandHelp.Description | Should -Not -BeNullOrEmpty |
69 | 67 | } |
70 | 68 |
|
71 | 69 | # Should be at least one example |
72 | 70 | It "Has example code" { |
73 | | - ($script:commandHelp.Examples.Example | Select-Object -First 1).Code | Should -Not -BeNullOrEmpty |
| 71 | + ($commandHelp.Examples.Example | Select-Object -First 1).Code | Should -Not -BeNullOrEmpty |
74 | 72 | } |
75 | 73 |
|
76 | 74 | # Should be at least one example description |
77 | 75 | It "Has example help" { |
78 | | - ($script:commandHelp.Examples.Example.Remarks | Select-Object -First 1).Text | Should -Not -BeNullOrEmpty |
| 76 | + ($commandHelp.Examples.Example.Remarks | Select-Object -First 1).Text | Should -Not -BeNullOrEmpty |
79 | 77 | } |
80 | 78 |
|
81 | | - It "Help link <_> is valid" -Tag 'Acceptance' -ForEach $helpLinks { |
82 | | - (Invoke-WebRequest -Uri $_ -UseBasicParsing -TimeoutSec 10).StatusCode | Should -Be '200' |
83 | | - } |
| 79 | + # It "Help link <_> is valid" -ForEach $helpLinks { |
| 80 | + # (Invoke-WebRequest -Uri $_ -UseBasicParsing).StatusCode | Should -Be '200' |
| 81 | + # } |
84 | 82 |
|
85 | 83 | Context "Parameter <_.Name>" -ForEach $commandParameters { |
86 | 84 |
|
87 | 85 | BeforeAll { |
88 | | - $script:parameter = $_ |
89 | | - $script:parameterName = $script:parameter.Name |
90 | | - $script:parameterHelp = $script:commandHelp.parameters.parameter | Where-Object Name -EQ $script:parameterName |
91 | | - $script:parameterHelpType = if ($script:parameterHelp.ParameterValue) { |
92 | | - $script:parameterHelp.ParameterValue.Trim() |
93 | | - } |
| 86 | + $parameter = $_ |
| 87 | + $parameterName = $parameter.Name |
| 88 | + $parameterHelp = $commandHelp.parameters.parameter | Where-Object Name -EQ $parameterName |
| 89 | + $parameterHelpType = if ($parameterHelp.ParameterValue) { $parameterHelp.ParameterValue.Trim() } |
94 | 90 | } |
95 | 91 |
|
96 | 92 | # Should be a description for every parameter |
97 | 93 | It "Has description" { |
98 | | - $script:parameterHelp.Description.Text | Should -Not -BeNullOrEmpty |
| 94 | + $parameterHelp.Description.Text | Should -Not -BeNullOrEmpty |
99 | 95 | } |
100 | 96 |
|
101 | 97 | # Required value in Help should match IsMandatory property of parameter |
102 | 98 | It "Has correct [mandatory] value" { |
103 | 99 | $codeMandatory = $_.IsMandatory.toString() |
104 | | - $script:parameterHelp.Required | Should -Be $codeMandatory |
| 100 | + $parameterHelp.Required | Should -Be $codeMandatory |
105 | 101 | } |
106 | 102 |
|
107 | 103 | # Parameter type in help should match code |
108 | 104 | It "Has correct parameter type" { |
109 | | - $script:parameterHelpType | Should -Be $script:parameter.ParameterType.Name |
| 105 | + $parameterHelpType | Should -Be $parameter.ParameterType.Name |
110 | 106 | } |
111 | 107 | } |
112 | 108 |
|
113 | 109 | Context "Test <_> help parameter help for <commandName>" -ForEach $helpParameterNames { |
114 | 110 |
|
115 | 111 | # Shouldn't find extra parameters in help. |
116 | 112 | It "finds help parameter in code: <_>" { |
117 | | - $_ -in $script:commandParameterNames | Should -Be $true |
| 113 | + $_ -in $parameterNames | Should -Be $true |
118 | 114 | } |
119 | 115 | } |
120 | 116 | } |
0 commit comments