Skip to content

Enable suppression of custom rules when used together with -IncludeDefaultRules #1245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions Engine/Generic/RuleSuppression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
/// </summary>
public class RuleSuppression
{
private string _ruleName;

/// <summary>
/// The start offset of the rule suppression attribute (not where it starts to apply)
Expand Down Expand Up @@ -49,28 +48,8 @@ public int EndOffset
/// </summary>
public string RuleName
{
get
{
return _ruleName;
}

set
{
_ruleName = value;

if (!String.IsNullOrWhiteSpace(_ruleName)
&& (ScriptAnalyzer.Instance.ScriptRules != null
&& ScriptAnalyzer.Instance.ScriptRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
&& (ScriptAnalyzer.Instance.TokenRules != null
&& ScriptAnalyzer.Instance.TokenRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
&& (ScriptAnalyzer.Instance.ExternalRules != null
&& ScriptAnalyzer.Instance.ExternalRules.Count(item => String.Equals(item.GetFullName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0)
&& (ScriptAnalyzer.Instance.DSCResourceRules != null
&& ScriptAnalyzer.Instance.DSCResourceRules.Count(item => String.Equals(item.GetName(), _ruleName, StringComparison.OrdinalIgnoreCase)) == 0))
Copy link
Collaborator Author

@bergmeister bergmeister May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As one can see in the combination of all those statements is that we do not know whether the rule name to be suppressed is of type custom rule (where the rule name cannot be determined at design time) or not. Therefore it is not possible to make a statement whether the given rule name will be in one of the returned DiagnosticRecords, hence why the whole check is being removed.

{
Error = String.Format(Strings.RuleSuppressionRuleNameNotFound, _ruleName);
}
}
get;
set;
}

/// <summary>
Expand Down
9 changes: 0 additions & 9 deletions Engine/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Engine/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@
<data name="RuleSuppressionErrorFormat" xml:space="preserve">
<value>Suppression Message Attribute error at line {0} in {1} : {2}</value>
</data>
<data name="RuleSuppressionRuleNameNotFound" xml:space="preserve">
<value>Rule {0} cannot be found.</value>
</data>
<data name="StringConstantArgumentsSuppressionAttributeError" xml:space="preserve">
<value>All the arguments of the Suppress Message Attribute should be string constants.</value>
</data>
Expand Down
37 changes: 37 additions & 0 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,43 @@ Describe "Test importing correct customized rules" {
$customizedRulePath.Count | Should -Be 0
}

It "can suppress custom rule with rule name expression '<RuleNameExpression>'" -TestCases @(
@{RuleNameExpression = '$MyInvocation.MyCommand.Name'; RuleName = 'WarningAboutDoSomething' }
@{RuleNameExpression = '$MyInvocation.InvocationName'; RuleName = 'MyCustomRule\WarningAboutDoSomething' }
@{RuleNameExpression = "'MyRuleName'"; RuleName = 'MyRuleName' }
) {
Param($RuleNameExpression, $RuleName)

$script = @"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('$RuleName', '')]
Param()
Invoke-Something
"@
$customRuleContent = @'
function WarningAboutDoSomething {
param (
[System.Management.Automation.Language.CommandAst]$ast
)

if ($ast.GetCommandName() -eq 'Invoke-Something') {
New-Object -Typename 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord' `
-ArgumentList 'This is help',$ast.Extent,REPLACE_WITH_RULE_NAME_EXPRESSION,Warning,$ast.Extent.File,$null,$null
}
}
'@
$customRuleContent = $customRuleContent.Replace('REPLACE_WITH_RULE_NAME_EXPRESSION', $RuleNameExpression)
$testScriptPath = "TestDrive:\SuppressedCustomRule.ps1"
Set-Content -Path $testScriptPath -Value $script
$customRuleScriptPath = Join-Path $TestDrive 'MyCustomRule.psm1'
Set-Content -Path $customRuleScriptPath -Value $customRuleContent
$violationsWithoutSuppresion = Invoke-ScriptAnalyzer -ScriptDefinition 'Invoke-Something' -CustomRulePath $customRuleScriptPath
$violationsWithoutSuppresion.Count | Should -Be 1
$violations = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath
$violations.Count | Should -Be 0
$violationsWithIncludeDefaultRules = Invoke-ScriptAnalyzer -Path $testScriptPath -CustomRulePath $customRuleScriptPath -IncludeDefaultRules
$violationsWithIncludeDefaultRules.Count | Should -Be 0
}

It "will set RuleSuppressionID" {
$violations = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath $directory\samplerule
$violations[0].RuleSuppressionID | Should -Be "MyRuleSuppressionID"
Expand Down