Skip to content

Commit f99e1f4

Browse files
authored
Enable suppression of custom rules when used together with -IncludeDefaultRules (#1245)
* remove faulty error checking * fix syntax error * Add test cases * cleanup * remove resource string of error message * Make tests PS4 compatible
1 parent 0bef1c8 commit f99e1f4

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

Engine/Generic/RuleSuppression.cs

+2-23
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1515
/// </summary>
1616
public class RuleSuppression
1717
{
18-
private string _ruleName;
1918

2019
/// <summary>
2120
/// The start offset of the rule suppression attribute (not where it starts to apply)
@@ -49,28 +48,8 @@ public int EndOffset
4948
/// </summary>
5049
public string RuleName
5150
{
52-
get
53-
{
54-
return _ruleName;
55-
}
56-
57-
set
58-
{
59-
_ruleName = value;
60-
61-
if (!String.IsNullOrWhiteSpace(_ruleName)
62-
&& (ScriptAnalyzer.Instance.ScriptRules != null
63-
&& ScriptAnalyzer.Instance.ScriptRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
64-
&& (ScriptAnalyzer.Instance.TokenRules != null
65-
&& ScriptAnalyzer.Instance.TokenRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
66-
&& (ScriptAnalyzer.Instance.ExternalRules != null
67-
&& ScriptAnalyzer.Instance.ExternalRules.Count(item => String.Equals(item.GetFullName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
68-
&& (ScriptAnalyzer.Instance.DSCResourceRules != null
69-
&& ScriptAnalyzer.Instance.DSCResourceRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0))
70-
{
71-
Error = String.Format(Strings.RuleSuppressionRuleNameNotFound, _ruleName);
72-
}
73-
}
51+
get;
52+
set;
7453
}
7554

7655
/// <summary>

Engine/Strings.Designer.cs

-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Engine/Strings.resx

-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@
165165
<data name="RuleSuppressionErrorFormat" xml:space="preserve">
166166
<value>Suppression Message Attribute error at line {0} in {1} : {2}</value>
167167
</data>
168-
<data name="RuleSuppressionRuleNameNotFound" xml:space="preserve">
169-
<value>Rule {0} cannot be found.</value>
170-
</data>
171168
<data name="StringConstantArgumentsSuppressionAttributeError" xml:space="preserve">
172169
<value>All the arguments of the Suppress Message Attribute should be string constants.</value>
173170
</data>

Tests/Engine/CustomizedRule.tests.ps1

+37
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,43 @@ Describe "Test importing correct customized rules" {
173173
$customizedRulePath.Count | Should -Be 0
174174
}
175175

176+
It "can suppress custom rule with rule name expression '<RuleNameExpression>'" -TestCases @(
177+
@{RuleNameExpression = '$MyInvocation.MyCommand.Name'; RuleName = 'WarningAboutDoSomething' }
178+
@{RuleNameExpression = '$MyInvocation.InvocationName'; RuleName = 'MyCustomRule\WarningAboutDoSomething' }
179+
@{RuleNameExpression = "'MyRuleName'"; RuleName = 'MyRuleName' }
180+
) {
181+
Param($RuleNameExpression, $RuleName)
182+
183+
$script = @"
184+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('$RuleName', '')]
185+
Param()
186+
Invoke-Something
187+
"@
188+
$customRuleContent = @'
189+
function WarningAboutDoSomething {
190+
param (
191+
[System.Management.Automation.Language.CommandAst]$ast
192+
)
193+
194+
if ($ast.GetCommandName() -eq 'Invoke-Something') {
195+
New-Object -Typename 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord' `
196+
-ArgumentList 'This is help',$ast.Extent,REPLACE_WITH_RULE_NAME_EXPRESSION,Warning,$ast.Extent.File,$null,$null
197+
}
198+
}
199+
'@
200+
$customRuleContent = $customRuleContent.Replace('REPLACE_WITH_RULE_NAME_EXPRESSION', $RuleNameExpression)
201+
$testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
202+
Set-Content -Path $testScriptPath -Value $script
203+
$customRuleScriptPath = Join-Path $TestDrive 'MyCustomRule.psm1'
204+
Set-Content -Path $customRuleScriptPath -Value $customRuleContent
205+
$violationsWithoutSuppresion = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath
206+
$violationsWithoutSuppresion.Count | Should -Be 1
207+
$violations = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath
208+
$violations.Count | Should -Be 0
209+
$violationsWithIncludeDefaultRules = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath -IncludeDefaultRules
210+
$violationsWithIncludeDefaultRules.Count | Should -Be 0
211+
}
212+
176213
It "will set RuleSuppressionID" {
177214
$violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
178215
$violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID"

0 commit comments

Comments
 (0)