Skip to content

Commit a79f723

Browse files
author
Quoc Truong
committed
Add tests for rule suppressions
1 parent 0c1a1cc commit a79f723

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Tests/Engine/RuleSuppression.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideCommentHelp", "", Scope="Function", Target="*")]
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPositionalParameters", Scope="Function", Target="Test*")]
3+
Param(
4+
)
5+
6+
function SuppressMe ()
7+
{
8+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSProvideVerboseMessage")]
9+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUninitializedVariable", "unused1")]
10+
Param([string]$unUsed1, [int] $unUsed2)
11+
{
12+
Write-Host "I do nothing"
13+
}
14+
15+
}
16+
17+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Scope="Class")]
18+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("pSAvoidUsingInvokeExpression", "")]
19+
class TestClass
20+
{
21+
[void] TestFunction2()
22+
{
23+
Write-Host "Should not use positional parameters"
24+
$a = ConvertTo-SecureString -AsPlainText "Test" -Force
25+
}
26+
27+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
28+
[void] TestFunction()
29+
{
30+
Write-Host "Should not use write-host!"
31+
Invoke-Expression "invoke expression"
32+
}
33+
34+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases","")]
35+
[bool] Suppress()
36+
{
37+
gps
38+
return $true
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Import-Module -Verbose PSScriptAnalyzer
2+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
3+
$violations = Invoke-ScriptAnalyzer $directory\RuleSuppression.ps1
4+
5+
Describe "RuleSuppressionWithoutScope" {
6+
Context "Function" {
7+
It "Does not raise violations" {
8+
$suppression = $violations | Where-Object { $_.RuleName -eq "PSProvideVerboseMessage" }
9+
$suppression.Count | Should Be 0
10+
}
11+
}
12+
13+
Context "Class" {
14+
It "Does not raise violations" {
15+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingInvokeExpression" }
16+
$suppression.Count | Should Be 0
17+
}
18+
}
19+
20+
Context "FunctionInClass" {
21+
It "Does not raise violations" {
22+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingCmdletAliases" }
23+
$suppression.Count | Should Be 0
24+
}
25+
}
26+
27+
Context "Script" {
28+
It "Does not raise violations" {
29+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSProvideCommentHelp" }
30+
$suppression.Count | Should Be 0
31+
}
32+
}
33+
34+
Context "RuleSuppressionID" {
35+
It "Only suppress violations for that ID" {
36+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUninitializedVariable" }
37+
$suppression.Count | Should Be 1
38+
}
39+
}
40+
}
41+
42+
Describe "RuleSuppressionWithScope" {
43+
Context "FunctionScope" {
44+
It "Does not raise violations" {
45+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingPositionalParameters" }
46+
$suppression.Count | Should Be 1
47+
}
48+
}
49+
50+
Context "ClassScope" {
51+
It "Does not raise violations" {
52+
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingConvertToSecureStringWithPlainText" }
53+
$suppression.Count | Should Be 0
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)