Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf99e1f4

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 parent0bef1c8 commitf99e1f4

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

‎Engine/Generic/RuleSuppression.cs‎

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
1515
/// </summary>
1616
publicclassRuleSuppression
1717
{
18-
privatestring_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
publicstringRuleName
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‎

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Engine/Strings.resx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@
165165
<dataname="RuleSuppressionErrorFormat"xml:space="preserve">
166166
<value>Suppression Message Attribute error at line {0} in {1} : {2}</value>
167167
</data>
168-
<dataname="RuleSuppressionRuleNameNotFound"xml:space="preserve">
169-
<value>Rule {0} cannot be found.</value>
170-
</data>
171168
<dataname="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‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,43 @@ Describe "Test importing correct customized rules" {
173173
$customizedRulePath.Count| Should-Be0
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-Be1
207+
$violations=Invoke-ScriptAnalyzer-Path$testScriptPath-CustomRulePath$customRuleScriptPath
208+
$violations.Count| Should-Be0
209+
$violationsWithIncludeDefaultRules=Invoke-ScriptAnalyzer-Path$testScriptPath-CustomRulePath$customRuleScriptPath-IncludeDefaultRules
210+
$violationsWithIncludeDefaultRules.Count| Should-Be0
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)

[8]ページ先頭

©2009-2025 Movatter.jp