Skip to content

Commit ce38251

Browse files
committed
Add tests for validating comment-based help structure and check for invalid help directives
1 parent 4be93c8 commit ce38251

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

tests/QA/module.tests.ps1

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,83 @@ Describe 'Quality for module' -Tags 'TestQuality' {
179179
}
180180
}
181181

182+
Describe 'Comment-based help structure' -Tags 'helpQuality' {
183+
Context 'Validating comment-based help structure for <Name>' -ForEach $testCasesAllModuleFunction -Tag 'helpQuality' {
184+
BeforeAll {
185+
$functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1"
186+
187+
$scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName
188+
}
189+
190+
It 'Should not have invalid help directives in comment-based help for <Name>' {
191+
# Valid help directives (case-insensitive) from:
192+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comment_based_help
193+
$validDirectives = @(
194+
'SYNOPSIS'
195+
'DESCRIPTION'
196+
'PARAMETER'
197+
'EXAMPLE'
198+
'INPUTS'
199+
'OUTPUTS'
200+
'NOTES'
201+
'LINK'
202+
'COMPONENT'
203+
'ROLE'
204+
'FUNCTIONALITY'
205+
'FORWARDHELPTARGETNAME'
206+
'FORWARDHELPCATEGORY'
207+
'REMOTEHELPRUNSPACE'
208+
'EXTERNALHELP'
209+
)
210+
211+
# Find the comment-based help block
212+
if ($scriptFileRawContent -match '(?s)<#(.*?)#>')
213+
{
214+
$helpBlock = $Matches[1]
215+
216+
# Split into lines to check each one
217+
$helpLines = $helpBlock -split "`n"
218+
219+
$invalidDirectives = @()
220+
221+
foreach ($line in $helpLines)
222+
{
223+
# Check if line starts with whitespace followed by a period and text
224+
if ($line -match '^\s+\.([a-zA-Z]+)')
225+
{
226+
$directive = $Matches[1]
227+
228+
# Check if it's a valid directive
229+
if ($directive -notin $validDirectives)
230+
{
231+
$invalidDirectives += $directive
232+
}
233+
}
234+
}
235+
236+
$invalidDirectives | Should -BeNullOrEmpty -Because ('invalid help directives found that will break help parsing: {0}' -f ($invalidDirectives -join ', '))
237+
}
238+
}
239+
}
240+
}
241+
182242
Describe 'Help for module' -Tags 'helpQuality' {
183243
Context 'Validating help for <Name>' -ForEach $testCasesAllModuleFunction -Tag 'helpQuality' {
184244
BeforeAll {
185245
$functionFile = Get-ChildItem -Path $sourcePath -Recurse -Include "$Name.ps1"
186246

187247
$scriptFileRawContent = Get-Content -Raw -Path $functionFile.FullName
188248

189-
$abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $null)
249+
$parseErrors = $null
250+
$abstractSyntaxTree = [System.Management.Automation.Language.Parser]::ParseInput($scriptFileRawContent, [ref] $null, [ref] $parseErrors)
251+
252+
if ($parseErrors)
253+
{
254+
foreach ($parseError in $parseErrors)
255+
{
256+
Write-Warning -Message ('Parse error in {0}: {1}' -f $functionFile.FullName, $parseError.Message)
257+
}
258+
}
190259

191260
$astSearchDelegate = { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }
192261

0 commit comments

Comments
 (0)