-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelp.tests.ps1
More file actions
224 lines (198 loc) · 9.39 KB
/
Copy pathHelp.tests.ps1
File metadata and controls
224 lines (198 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# spell-checker:ignore BHPS juneb_get_help
# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'commandName',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'commands',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'commandParameterNames',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'helpLinks',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'helpParameterNames',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments',
'parameterHelpType',
Justification = 'false positive'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidGlobalVars',
'global:CustomTypes',
Justification = 'Needed for comparison'
)]
param()
BeforeDiscovery {
function global:FilterOutCommonParameters {
param ($Parameters)
$commonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters +
[System.Management.Automation.PSCmdlet]::OptionalCommonParameters
$Parameters | Where-Object { $_.Name -notin $commonParameters -and $_.IsDynamic -eq $false } | Sort-Object -Property 'Name' -Unique
}
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
# build or not. If it does not exist, it is not running in a psake build, so build the module.
if ($null -eq $Env:BHBuildOutput) {
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
$invokePsakeParameters = @{
TaskList = 'Build'
BuildFile = $buildFilePath
}
Invoke-psake @invokePsakeParameters
}
# PowerShellBuild outputs to Output/<ModuleName>/<Version>/, override BHBuildOutput
$projectRoot = Split-Path -Path $PSScriptRoot -Parent
$sourceManifest = Join-Path -Path $projectRoot -ChildPath "$Env:BHProjectName/$Env:BHProjectName.psd1"
$moduleVersion = (Import-PowerShellDataFile -Path $sourceManifest).ModuleVersion
$Env:BHBuildOutput = Join-Path -Path $projectRoot -ChildPath "Output/$Env:BHProjectName/$moduleVersion"
# Define the path to the module manifest
$moduleManifestFilename = $Env:BHProjectName + '.psd1'
$moduleManifestPath = Join-Path -Path $Env:BHBuildOutput -ChildPath $moduleManifestFilename
$global:CustomTypes = @()
@(
'Enums'
'Classes'
) | ForEach-Object {
$path = Join-Path -Path $Env:BHBuildOutput -ChildPath $_
if (Test-Path -Path $path) {
$global:CustomTypes += (Get-ChildItem -Path $path -Recurse -ErrorAction 'SilentlyContinue').BaseName
}
}
# Remove all versions of the module from the session. Pester can't handle multiple versions.
Get-Module -Name $Env:BHProjectName | Remove-Module -Force -ErrorAction 'Ignore'
Import-Module -Name $moduleManifestPath -Verbose:$false -ErrorAction 'Stop'
# Get module commands
$getCommandParameters = @{
Module = (Get-Module -Name $Env:BHProjectName)
CommandType = [System.Management.Automation.CommandTypes[]]'Cmdlet, Function' # Not alias
}
if ($PSVersionTable.PSVersion.Major -lt 6) {
$getCommandParameters.CommandType[0] += 'Workflow'
}
$commands = Get-Command @getCommandParameters
## When testing help, remember that help is cached at the beginning of each session.
## To test, restart session.
}
BeforeAll {
# Check if the BHBuildOutput environment variable exists to determine if this test is running in a psake
# build or not. If it does not exist, it is not running in a psake build, so build the module.
if ($null -eq $Env:BHBuildOutput) {
$buildFilePath = Join-Path -Path $PSScriptRoot -ChildPath '..\build.psake.ps1'
$invokePsakeParameters = @{
TaskList = 'Build'
BuildFile = $buildFilePath
}
Invoke-psake @invokePsakeParameters
}
}
Describe "Test help for <_.Name>" -ForEach $commands {
BeforeDiscovery {
# Get command help, parameters, and links. These are duplicated in BeforeAll
# below; they must also exist here because the nested Context blocks use them in
# -ForEach, which Pester evaluates during discovery (before BeforeAll runs).
$command = $_
$commandName = $command.Name
$commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters
$commandParameterNames = $commandParameters.Name
$helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter
$helpParameterNames = $helpParameters.Name
$helpLinks = $commandHelp.relatedLinks.navigationLink.uri | Where-Object { $_ -match '^https?://' }
}
BeforeAll {
# These variables are needed in both discovery and test phases so we need to duplicate them here
$command = $_
$commandName = $_.Name
$commandHelp = Get-Help -Name $command.Name -ErrorAction 'SilentlyContinue'
$commandParameters = global:FilterOutCommonParameters -Parameters $command.ParameterSets.Parameters
$commandParameterNames = $commandParameters.Name
$helpParameters = global:FilterOutCommonParameters -Parameters $commandHelp.Parameters.Parameter
$helpParameterNames = $helpParameters.Name
}
# If help is not found, synopsis in auto-generated help is the syntax diagram
It 'Help is not auto-generated' {
$commandHelp.Synopsis | Should -Not -BeLike '*`[`<CommonParameters`>`]*'
}
# Should be a description for every function
It 'Has description' {
$commandHelp.Description | Should -Not -BeNullOrEmpty
}
# Should be at least one example
It 'Has example code' {
($commandHelp.Examples.Example | Select-Object -First 1).Code | Should -Not -BeNullOrEmpty
}
# Should be at least one example description
It 'Has example help' {
($commandHelp.Examples.Example.Remarks | Select-Object -First 1).Text | Should -Not -BeNullOrEmpty
}
It 'Help link <_> is valid' -ForEach $helpLinks {
$currentProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$invokeWebRequestParameters = @{
Uri = $_
UseBasicParsing = $true
ErrorAction = 'Continue'
}
$invokeWebRequestResult = Invoke-WebRequest @invokeWebRequestParameters
$ProgressPreference = $currentProgressPreference
$statusCode = $invokeWebRequestResult.StatusCode
$statusCode | Should -Be '200'
}
Context 'Parameter <_.Name>' -Foreach $commandParameters {
BeforeAll {
$parameter = $_
$parameterName = $parameter.Name
$parameterHelp = $commandHelp.parameters.parameter | Where-Object { $_.Name -eq $parameterName }
$parameterHelpType = if ($parameterHelp.type.name) { $parameterHelp.type.name }
}
# Should be a description for every parameter
It 'Has description' {
$parameterHelp.Description.Text | Should -Not -BeNullOrEmpty
}
# Required value in Help should match IsMandatory property of parameter
It 'Has correct [mandatory] value' {
# Skip parameters that have different mandatory status across parameter sets
$parameterSetsWithParam = $command.ParameterSets | Where-Object { $_.Parameters.Name -contains $parameterName }
$mandatoryValues = $parameterSetsWithParam | ForEach-Object {
($_.Parameters | Where-Object { $_.Name -eq $parameterName }).IsMandatory
} | Sort-Object -Unique
if ($mandatoryValues.Count -gt 1) {
Set-ItResult -Skipped -Because "Parameter '$parameterName' has varying mandatory status across parameter sets"
return
}
$codeMandatory = $_.IsMandatory.toString().ToLower()
$helpRequired = $parameterHelp.Required.ToLower()
$helpRequired | Should -Be $codeMandatory
}
# Parameter type in help should match code
It 'Has correct parameter type' {
# Handle array of custom types by removing [] from end
$typeName = ($parameter.ParameterType.Name).TrimEnd('[]')
if ($typeName -in $global:CustomTypes) {
# Skip custom enums or classes. They won't show up in the help.
}
else {
$parameterHelpType | Should -Be $parameter.ParameterType.Name
}
}
}
Context 'Test <_> help parameter help for <commandName>' -Foreach $helpParameterNames {
# Shouldn't find extra parameters in help
It 'finds help parameter in code: <_>' {
$_ -in $commandParameterNames | Should -Be $true
}
}
}