Skip to content

Commit bd7c1b1

Browse files
author
Kapil Borle
authored
Merge pull request #642 from PowerShell/kapilmb/FixDscSeverityFilter
Fix severity filtering for DSC rules
2 parents e09c469 + d8b2da9 commit bd7c1b1

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

Engine/ScriptAnalyzer.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,28 +1895,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
18951895
// Run DSC Class rule
18961896
foreach (IDSCResourceRule dscResourceRule in this.DSCResourceRules)
18971897
{
1898-
bool includeRegexMatch = false;
1899-
bool excludeRegexMatch = false;
1900-
1901-
foreach (Regex include in includeRegexList)
1902-
{
1903-
if (include.IsMatch(dscResourceRule.GetName()))
1904-
{
1905-
includeRegexMatch = true;
1906-
break;
1907-
}
1908-
}
1909-
1910-
foreach (Regex exclude in excludeRegexList)
1911-
{
1912-
if (exclude.IsMatch(dscResourceRule.GetName()))
1913-
{
1914-
excludeRegexMatch = true;
1915-
break;
1916-
}
1917-
}
1918-
1919-
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || excludeRegexMatch))
1898+
if (IsRuleAllowed(dscResourceRule))
19201899
{
19211900
this.outputWriter.WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));
19221901

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,26 @@ Describe "Test Severity" {
317317
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity information, warning
318318
$errors.Count | Should Be 1
319319
}
320+
321+
It "works for dsc rules" {
322+
$testDataPath = [System.IO.Path]::Combine($(Split-Path $directory -Parent), `
323+
'Rules', `
324+
'DSCResources', `
325+
'MSFT_WaitForAll', `
326+
'MSFT_WaitForAll.psm1')
327+
328+
Function Get-Count {begin{$count=0} process{$count++} end{$count}}
329+
330+
Invoke-ScriptAnalyzer -Path $testDataPath -Severity Error | `
331+
Where-Object {$_.RuleName -eq "PSDSCUseVerboseMessageInDSCResource"} | `
332+
Get-Count | `
333+
Should Be 0
334+
335+
Invoke-ScriptAnalyzer -Path $testDataPath -Severity Information | `
336+
Where-Object {$_.RuleName -eq "PSDSCUseVerboseMessageInDSCResource"} | `
337+
Get-Count | `
338+
Should Be 2
339+
}
320340
}
321341

322342
Context "When used incorrectly" {

0 commit comments

Comments
 (0)