Skip to content

Commit 4731dd1

Browse files
author
Kapil Borle
committed
Add tests to check help snippet correction
1 parent 17c7310 commit 4731dd1

File tree

1 file changed

+101
-6
lines changed

1 file changed

+101
-6
lines changed

Tests/Rules/ProvideCommentHelp.tests.ps1

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
1+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$testRootDirectory = Split-Path -Parent $directory
3+
24
Import-Module PSScriptAnalyzer
5+
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
6+
37
$violationMessage = "The cmdlet 'Comment' does not have a help comment."
48
$violationName = "PSProvideCommentHelp"
5-
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
69
$violations = Invoke-ScriptAnalyzer $directory\BadCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}
710

8-
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0')
9-
{
11+
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
1012
$dscViolations = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $directory\DSCResources\MyDscResource\MyDscResource.psm1 | Where-Object {$_.RuleName -eq $violationName}
1113
}
1214

1315
$noViolations = Invoke-ScriptAnalyzer $directory\GoodCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}
1416

17+
function Test-Correction {
18+
param($scriptDef, $expectedCorrection)
19+
20+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule $violationName
21+
$violations.Count | Should Be 1
22+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
23+
}
24+
1525
Describe "ProvideCommentHelp" {
1626
Context "When there are violations" {
1727
It "has 2 provide comment help violations" {
@@ -26,8 +36,93 @@ Describe "ProvideCommentHelp" {
2636
$violations[1].Extent.Text | Should Be "Comment"
2737
}
2838

29-
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0')
30-
{
39+
It "should return a help snippet correction with 0 parameters" {
40+
$def = @'
41+
function foo {
42+
}
43+
44+
Export-ModuleMember -Function foo
45+
'@
46+
$expectedCorrection = @'
47+
<#
48+
.SYNOPSIS
49+
Short description
50+
51+
.DESCRIPTION
52+
Long description
53+
54+
.EXAMPLE
55+
An example
56+
57+
.NOTES
58+
General notes
59+
#>
60+
'@
61+
Test-Correction $def $expectedCorrection
62+
}
63+
64+
It "should return a help snippet correction with 1 parameters" {
65+
$def = @'
66+
function foo {
67+
param($param1)
68+
}
69+
70+
Export-ModuleMember -Function foo
71+
'@
72+
$expectedCorrection = @'
73+
<#
74+
.SYNOPSIS
75+
Short description
76+
77+
.DESCRIPTION
78+
Long description
79+
80+
.PARAMETER param1
81+
Parameter description
82+
83+
.EXAMPLE
84+
An example
85+
86+
.NOTES
87+
General notes
88+
#>
89+
'@
90+
Test-Correction $def $expectedCorrection
91+
}
92+
93+
It "should return a help snippet correction with 2 parameters" {
94+
$def = @'
95+
function foo {
96+
param($param1, $param2)
97+
}
98+
99+
Export-ModuleMember -Function foo
100+
'@
101+
$expectedCorrection = @'
102+
<#
103+
.SYNOPSIS
104+
Short description
105+
106+
.DESCRIPTION
107+
Long description
108+
109+
.PARAMETER param1
110+
Parameter description
111+
112+
.PARAMETER param2
113+
Parameter description
114+
115+
.EXAMPLE
116+
An example
117+
118+
.NOTES
119+
General notes
120+
#>
121+
'@
122+
Test-Correction $def $expectedCorrection
123+
}
124+
125+
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
31126
It "Does not count violation in DSC class" {
32127
$dscViolations.Count | Should Be 0
33128
}

0 commit comments

Comments
 (0)