Skip to content

Commit 28e40a1

Browse files
Copilotbergmeister
andauthored
Test severity filtering of suppressed custom diagnostics
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent 3a7fed2 commit 28e40a1

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

‎Tests/Engine/CustomizedRule.tests.ps1‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,49 @@ function ReportError {
166166
167167
if ($ast.GetCommandName() -eq 'Invoke-Something') {
168168
New-Object -Typename 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord' `
169-
-ArgumentList 'This is an error', $ast.Extent, 'ReportError', Error, $ast.Extent.File, $null, $null
169+
-ArgumentList 'This is an error', $ast.Extent, 'ReportError', Error, $ast.Extent.File, 'custom-id', $null
170170
}
171171
}
172172
'@
173173
$customRuleScriptPath = Join-Path $TestDrive 'SeverityRule.psm1'
174174
Set-Content -Path $customRuleScriptPath -Value $customRuleContent
175+
(Get-ScriptAnalyzerRule -CustomRulePath $customRuleScriptPath).Severity | Should -Be 'Warning'
175176

176177
$errors = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath -Severity Error
177178
$errors.Count | Should -Be 1
178179
$errors[0].Severity | Should -Be 'Error'
179180

180181
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath -Severity Warning
181182
$warnings.Count | Should -Be 0
183+
184+
$script = @'
185+
function Suppressed {
186+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('ReportError', '', Justification = 'test justification')]
187+
param()
188+
Invoke-Something
189+
}
190+
Invoke-Something
191+
'@
192+
foreach ($mode in 'SuppressedOnly', 'IncludeSuppressed') {
193+
$parameters = @{
194+
ScriptDefinition = $script
195+
CustomRulePath = $customRuleScriptPath
196+
ErrorAction = 'Stop'
197+
$mode = $true
198+
}
199+
200+
$errors = @(Invoke-ScriptAnalyzer @parameters -Severity Error)
201+
$errors.Count | Should -Be $(if ($mode -eq 'SuppressedOnly') { 1 } else { 2 })
202+
$suppressed = @($errors | Where-Object IsSuppressed)
203+
$suppressed.Count | Should -Be 1
204+
$suppressed[0].Severity | Should -Be 'Error'
205+
$suppressed[0].RuleSuppressionID | Should -BeExactly 'custom-id'
206+
$suppressed[0].Suppression | Should -HaveCount 1
207+
$suppressed[0].Suppression[0].Justification | Should -BeExactly 'test justification'
208+
209+
$warnings = @(Invoke-ScriptAnalyzer @parameters -Severity Warning)
210+
$warnings.Count | Should -Be 0
211+
}
182212
}
183213

184214
It "can suppress custom rule with rule name expression '<RuleNameExpression>'" -TestCases @(

0 commit comments

Comments
 (0)