@@ -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