1+ if ($PSVersionTable.PSVersion -ge [Version ]' 5.0' ) {
2+
3+ # Check if PSScriptAnalyzer is already loaded so we don't
4+ # overwrite a test version of Invoke-ScriptAnalyzer by
5+ # accident
6+ if (! (Get-Module PSScriptAnalyzer) -and ! $testingLibraryUsage )
7+ {
8+ Import-Module - Verbose PSScriptAnalyzer
9+ }
10+
11+ $directory = Split-Path - Parent $MyInvocation.MyCommand.Path
12+ $violationsUsingScriptDefinition = Invoke-ScriptAnalyzer - ScriptDefinition (Get-Content - Raw " $directory \RuleSuppression.ps1" )
13+ $violations = Invoke-ScriptAnalyzer " $directory \RuleSuppression.ps1"
14+
15+ Describe " RuleSuppressionWithoutScope" {
16+
17+ Context " Class" {
18+ It " Does not raise violations" {
19+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSAvoidUsingInvokeExpression" }
20+ $suppression.Count | Should Be 0
21+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSAvoidUsingInvokeExpression" }
22+ $suppression.Count | Should Be 0
23+ }
24+ }
25+
26+ Context " FunctionInClass" {
27+ It " Does not raise violations" {
28+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSAvoidUsingCmdletAliases" }
29+ $suppression.Count | Should Be 0
30+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSAvoidUsingCmdletAliases" }
31+ $suppression.Count | Should Be 0
32+ }
33+ }
34+
35+ Context " Script" {
36+ It " Does not raise violations" {
37+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSProvideCommentHelp" }
38+ $suppression.Count | Should Be 0
39+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSProvideCommentHelp" }
40+ $suppression.Count | Should Be 0
41+ }
42+ }
43+
44+ Context " RuleSuppressionID" {
45+ It " Only suppress violations for that ID" {
46+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSAvoidDefaultValueForMandatoryParameter" }
47+ $suppression.Count | Should Be 1
48+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSAvoidDefaultValueForMandatoryParameter" }
49+ $suppression.Count | Should Be 1
50+ }
51+ }
52+ }
53+
54+ Describe " RuleSuppressionWithScope" {
55+ Context " FunctionScope" {
56+ It " Does not raise violations" {
57+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSAvoidUsingPositionalParameters" }
58+ $suppression.Count | Should Be 0
59+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSAvoidUsingPositionalParameters" }
60+ $suppression.Count | Should Be 0
61+ }
62+ }
63+
64+ Context " ClassScope" {
65+ It " Does not raise violations" {
66+ $suppression = $violations | Where-Object {$_.RuleName -eq " PSAvoidUsingConvertToSecureStringWithPlainText" }
67+ $suppression.Count | Should Be 0
68+ $suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq " PSAvoidUsingConvertToSecureStringWithPlainText" }
69+ $suppression.Count | Should Be 0
70+ }
71+ }
72+ }
73+ }
0 commit comments