Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
44 changes: 44 additions & 0 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,50 @@ 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') {
$correctionExtent = New-Object 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent' $ast.Extent.StartLineNumber,$ast.Extent.EndLineNumber,$ast.Extent.StartColumnNumber,$ast.Extent.EndColumnNumber,'correction','description'

# rule name has to match function name (1.18)
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
RuleName = REPLACE_WITH_RULE_NAME_EXPRESSION
Message = 'Message about usage of Invoke-Something'
Extent = $ast.Extent
"Severity" = "Warning"
}
}
}
'@
$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