Skip to content

Commit a882f23

Browse files
author
Kapil Borle
committed
Add tests to verify target behavior in rule suppression
1 parent 54389bd commit a882f23

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
77
}
88

99
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
10+
$testRootDirectory = Split-Path -Parent $directory
11+
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
12+
1013
$violationsUsingScriptDefinition = Invoke-ScriptAnalyzer -ScriptDefinition (Get-Content -Raw "$directory\RuleSuppression.ps1")
1114
$violations = Invoke-ScriptAnalyzer "$directory\RuleSuppression.ps1"
1215

@@ -146,4 +149,70 @@ Describe "RuleSuppressionWithScope" {
146149
$suppression.Count | Should Be 0
147150
}
148151
}
152+
153+
Context "Function scope with regular expression" {
154+
It "suppresses objects that match the regular expression" {
155+
$scriptDef = @'
156+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-ba[rz]')]
157+
param()
158+
function start-foo {
159+
write-host "start-foo"
160+
}
161+
162+
function start-bar {
163+
write-host "start-bar"
164+
}
165+
166+
function start-baz {
167+
write-host "start-baz"
168+
}
169+
170+
function start-bam {
171+
write-host "start-bam"
172+
}
173+
'@
174+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
175+
$suppressed.Count | Should Be 2
176+
}
177+
178+
It "suppresses objects that match glob pattern with glob in the end" {
179+
$scriptDef = @'
180+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='start-*')]
181+
param()
182+
function start-foo {
183+
write-host "start-foo"
184+
}
185+
186+
function start-bar {
187+
write-host "start-bar"
188+
}
189+
190+
function stop-bar {
191+
write-host "stop-bar"
192+
}
193+
'@
194+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
195+
$suppressed.Count | Should Be 2
196+
}
197+
198+
It "suppresses objects that match glob pattern with glob in the begining" {
199+
$scriptDef = @'
200+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function', Target='*-bar')]
201+
param()
202+
function start-foo {
203+
write-host "start-foo"
204+
}
205+
206+
function start-bar {
207+
write-host "start-bar"
208+
}
209+
210+
function start-baz {
211+
write-host "start-baz"
212+
}
213+
'@
214+
$suppressed = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDef -IncludeRule 'PSAvoidUsingWriteHost' -SuppressedOnly
215+
$suppressed.Count | Should Be 1
216+
}
217+
}
149218
}

0 commit comments

Comments
 (0)