1+ # Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)
2+
3+ # $outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'out'
4+ # $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
5+ # $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
6+ # $outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
7+ #
8+ # # Get module commands
9+ # # Remove all versions of the module from the session. Pester can't handle multiple versions.
10+ # #Get-Module $env:BHProjectName | Remove-Module -Force
11+ # #Import-Module -Name (Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1") -Verbose:$false -ErrorAction Stop
12+ # $commands = Get-Command -Module (Get-Module $env:BHProjectName) -CommandType Cmdlet, Function, Workflow # Not alias
13+ #
14+ # ## When testing help, remember that help is cached at the beginning of each session.
15+ # ## To test, restart session.
16+ #
17+ # foreach ($command in $commands) {
18+ # $commandName = $command.Name
19+ #
20+ # # The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
21+ # $help = Get-Help $commandName -ErrorAction SilentlyContinue
22+ #
23+ # Describe "Test help for $commandName" -Tags @('MetaTest') {
24+ #
25+ # # If help is not found, synopsis in auto-generated help is the syntax diagram
26+ # It 'should not be auto-generated' {
27+ # $help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
28+ # }
29+ #
30+ # # Should be a description for every function
31+ # It "gets description for $commandName" {
32+ # $help.Description | Should Not BeNullOrEmpty
33+ # }
34+ #
35+ # # Should be at least one example
36+ # It "gets example code from $commandName" {
37+ # ($help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
38+ # }
39+ #
40+ # # Should be at least one example description
41+ # It "gets example help from $commandName" {
42+ # ($help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
43+ # }
44+ #
45+ # Context "Test parameter help for $commandName" {
46+ #
47+ # $common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer',
48+ # 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Confirm', 'Whatif'
49+ #
50+ # $parameters = $command.ParameterSets.Parameters |
51+ # Sort-Object -Property Name -Unique |
52+ # Where-Object { $_.Name -notin $common }
53+ # $parameterNames = $parameters.Name
54+ #
55+ # ## Without the filter, WhatIf and Confirm parameters are still flagged in "finds help parameter in code" test
56+ # $helpParameters = $help.Parameters.Parameter |
57+ # Where-Object { $_.Name -notin $common } |
58+ # Sort-Object -Property Name -Unique
59+ # $helpParameterNames = $helpParameters.Name
60+ #
61+ # foreach ($parameter in $parameters) {
62+ # $parameterName = $parameter.Name
63+ # $parameterHelp = $help.parameters.parameter | Where-Object Name -EQ $parameterName
64+ #
65+ # # Should be a description for every parameter
66+ # It "gets help for parameter: $parameterName : in $commandName" {
67+ # $parameterHelp.Description.Text | Should Not BeNullOrEmpty
68+ # }
69+ #
70+ # # Required value in Help should match IsMandatory property of parameter
71+ # It "help for $parameterName parameter in $commandName has correct Mandatory value" {
72+ # $codeMandatory = $parameter.IsMandatory.toString()
73+ # $parameterHelp.Required | Should Be $codeMandatory
74+ # }
75+ #
76+ # # Parameter type in Help should match code
77+ # # It "help for $commandName has correct parameter type for $parameterName" {
78+ # # $codeType = $parameter.ParameterType.Name
79+ # # # To avoid calling Trim method on a null object.
80+ # # $helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
81+ # # $helpType | Should be $codeType
82+ # # }
83+ # }
84+ #
85+ # foreach ($helpParm in $HelpParameterNames) {
86+ # # Shouldn't find extra parameters in help.
87+ # It "finds help parameter in code: $helpParm" {
88+ # $helpParm -in $parameterNames | Should Be $true
89+ # }
90+ # }
91+ # }
92+ #
93+ # Context "Help Links should be Valid for $commandName" {
94+ # $link = $help.relatedLinks.navigationLink.uri
95+ #
96+ # foreach ($link in $links) {
97+ # if ($link) {
98+ # # Should have a valid uri if one is provided.
99+ # it "[$link] should have 200 Status Code for $commandName" {
100+ # $Results = Invoke-WebRequest -Uri $link -UseBasicParsing
101+ # $Results.StatusCode | Should Be '200'
102+ # }
103+ # }
104+ # }
105+ # }
106+ # }
107+ # }
0 commit comments