Skip to content

Commit b90998e

Browse files
committed
Fixed Tests for RuleSuppression Feature
1 parent 5f9457f commit b90998e

File tree

4 files changed

+100
-58
lines changed

4 files changed

+100
-58
lines changed

Tests/Engine/RuleSuppression.ps1

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function", Target="*")]
2-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="Test*")]
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", "", Scope="Function", Target="*")]
33
Param(
44
)
55

@@ -39,29 +39,4 @@ function SuppressTwoVariables()
3939
)
4040
{
4141
}
42-
}
43-
44-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
45-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("pSAvoidUsingInvokeExpression", "")]
46-
class TestClass
47-
{
48-
[void] TestFunction2()
49-
{
50-
Write-Host "Should not use positional parameters"
51-
$a = ConvertTo-SecureString -AsPlainText "Test" -Force
52-
}
53-
54-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
55-
[void] TestFunction()
56-
{
57-
Write-Host "Should not use write-host!"
58-
Invoke-Expression "invoke expression"
59-
}
60-
61-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases","")]
62-
[bool] Suppress()
63-
{
64-
gps
65-
return $true
66-
}
6742
}

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
if ($PSVersionTable.PSVersion -ge [Version]'5.0') {
2-
3-
# Check if PSScriptAnalyzer is already loaded so we don't
1+
# Check if PSScriptAnalyzer is already loaded so we don't
42
# overwrite a test version of Invoke-ScriptAnalyzer by
53
# accident
64
if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
@@ -22,24 +20,6 @@ Describe "RuleSuppressionWithoutScope" {
2220
}
2321
}
2422

25-
Context "Class" {
26-
It "Does not raise violations" {
27-
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingInvokeExpression" }
28-
$suppression.Count | Should Be 0
29-
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingInvokeExpression" }
30-
$suppression.Count | Should Be 0
31-
}
32-
}
33-
34-
Context "FunctionInClass" {
35-
It "Does not raise violations" {
36-
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingCmdletAliases" }
37-
$suppression.Count | Should Be 0
38-
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingCmdletAliases" }
39-
$suppression.Count | Should Be 0
40-
}
41-
}
42-
4323
Context "Script" {
4424
It "Does not raise violations" {
4525
$suppression = $violations | Where-Object {$_.RuleName -eq "PSProvideCommentHelp" }
@@ -68,14 +48,4 @@ Describe "RuleSuppressionWithScope" {
6848
$suppression.Count | Should Be 0
6949
}
7050
}
71-
72-
Context "ClassScope" {
73-
It "Does not raise violations" {
74-
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingConvertToSecureStringWithPlainText" }
75-
$suppression.Count | Should Be 0
76-
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingConvertToSecureStringWithPlainText" }
77-
$suppression.Count | Should Be 0
78-
}
79-
}
80-
}
81-
}
51+
}

Tests/Engine/RuleSuppressionClass.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("pSAvoidUsingInvokeExpression", "")]
3+
class TestClass
4+
{
5+
[void] TestFunction2()
6+
{
7+
Write-Host "Should not use positional parameters"
8+
$a = ConvertTo-SecureString -AsPlainText "Test" -Force
9+
}
10+
11+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
12+
[void] TestFunction()
13+
{
14+
Write-Host "Should not use write-host!"
15+
Invoke-Expression "invoke expression"
16+
}
17+
18+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases","")]
19+
[bool] Suppress()
20+
{
21+
gps
22+
return $true
23+
}
24+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
if ($PSVersionTable.PSVersion -ge [Version]'5.0') {
2+
3+
# Check if PSScriptAnalyzer is already loaded so we don't
4+
# overwrite a test version of Invoke-ScriptAnalyzer by
5+
# accident
6+
if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
7+
{
8+
Import-Module -Verbose PSScriptAnalyzer
9+
}
10+
11+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
12+
$violationsUsingScriptDefinition = Invoke-ScriptAnalyzer -ScriptDefinition (Get-Content -Raw "$directory\RuleSuppression.ps1")
13+
$violations = Invoke-ScriptAnalyzer "$directory\RuleSuppression.ps1"
14+
15+
Describe "RuleSuppressionWithoutScope" {
16+
17+
Context "Class" {
18+
It "Does not raise violations" {
19+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingInvokeExpression" }
20+
$suppression.Count | Should Be 0
21+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingInvokeExpression" }
22+
$suppression.Count | Should Be 0
23+
}
24+
}
25+
26+
Context "FunctionInClass" {
27+
It "Does not raise violations" {
28+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingCmdletAliases" }
29+
$suppression.Count | Should Be 0
30+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingCmdletAliases" }
31+
$suppression.Count | Should Be 0
32+
}
33+
}
34+
35+
Context "Script" {
36+
It "Does not raise violations" {
37+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSProvideCommentHelp" }
38+
$suppression.Count | Should Be 0
39+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSProvideCommentHelp" }
40+
$suppression.Count | Should Be 0
41+
}
42+
}
43+
44+
Context "RuleSuppressionID" {
45+
It "Only suppress violations for that ID" {
46+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidDefaultValueForMandatoryParameter" }
47+
$suppression.Count | Should Be 1
48+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidDefaultValueForMandatoryParameter" }
49+
$suppression.Count | Should Be 1
50+
}
51+
}
52+
}
53+
54+
Describe "RuleSuppressionWithScope" {
55+
Context "FunctionScope" {
56+
It "Does not raise violations" {
57+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingPositionalParameters" }
58+
$suppression.Count | Should Be 0
59+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingPositionalParameters" }
60+
$suppression.Count | Should Be 0
61+
}
62+
}
63+
64+
Context "ClassScope" {
65+
It "Does not raise violations" {
66+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingConvertToSecureStringWithPlainText" }
67+
$suppression.Count | Should Be 0
68+
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidUsingConvertToSecureStringWithPlainText" }
69+
$suppression.Count | Should Be 0
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)