@@ -7,60 +7,61 @@ BeforeAll {
7
7
8
8
Describe " AvoidAssignmentToAutomaticVariables" {
9
9
Context " ReadOnly Variables" {
10
+ BeforeDiscovery {
11
+ $excpectedSeverityForAutomaticVariablesInPowerShell6 = ' Warning'
12
+ if ($PSVersionTable.PSVersion.Major -ge 6 )
13
+ {
14
+ $excpectedSeverityForAutomaticVariablesInPowerShell6 = ' Error'
15
+ }
10
16
11
- $excpectedSeverityForAutomaticVariablesInPowerShell6 = ' Warning'
12
- if ($PSVersionTable.PSVersion.Major -ge 6 )
13
- {
14
- $excpectedSeverityForAutomaticVariablesInPowerShell6 = ' Error'
17
+ $testCases_AutomaticVariables = @ (
18
+ @ { VariableName = ' ?' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
19
+ @ { VariableName = ' Error' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
20
+ @ { VariableName = ' ExecutionContext' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
21
+ @ { VariableName = ' false' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
22
+ @ { VariableName = ' Home' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
23
+ @ { VariableName = ' Host' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
24
+ @ { VariableName = ' PID' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
25
+ @ { VariableName = ' PSCulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
26
+ @ { VariableName = ' PSEdition' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
27
+ @ { VariableName = ' PSHome' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
28
+ @ { VariableName = ' PSUICulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
29
+ @ { VariableName = ' PSVersionTable' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
30
+ @ { VariableName = ' ShellId' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
31
+ @ { VariableName = ' true' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
32
+ # Variables introduced only in PowerShell 6+ have a Severity of Warning only
33
+ @ { VariableName = ' IsCoreCLR' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
34
+ @ { VariableName = ' IsLinux' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
35
+ @ { VariableName = ' IsMacOS' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
36
+ @ { VariableName = ' IsWindows' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
37
+ @ { VariableName = ' _' ; ExpectedSeverity = ' Warning' }
38
+ @ { VariableName = ' AllNodes' ; ExpectedSeverity = ' Warning' }
39
+ @ { VariableName = ' Args' ; ExpectedSeverity = ' Warning' }
40
+ @ { VariableName = ' ConsoleFilename' ; ExpectedSeverity = ' Warning' }
41
+ @ { VariableName = ' Event' ; ExpectedSeverity = ' Warning' }
42
+ @ { VariableName = ' EventArgs' ; ExpectedSeverity = ' Warning' }
43
+ @ { VariableName = ' EventSubscriber' ; ExpectedSeverity = ' Warning' }
44
+ @ { VariableName = ' ForEach' ; ExpectedSeverity = ' Warning' }
45
+ @ { VariableName = ' Input' ; ExpectedSeverity = ' Warning' }
46
+ @ { VariableName = ' Matches' ; ExpectedSeverity = ' Warning' }
47
+ @ { VariableName = ' MyInvocation' ; ExpectedSeverity = ' Warning' }
48
+ @ { VariableName = ' NestedPromptLevel' ; ExpectedSeverity = ' Warning' }
49
+ @ { VariableName = ' Profile' ; ExpectedSeverity = ' Warning' }
50
+ @ { VariableName = ' PSBoundParameters' ; ExpectedSeverity = ' Warning' }
51
+ @ { VariableName = ' PsCmdlet' ; ExpectedSeverity = ' Warning' }
52
+ @ { VariableName = ' PSCommandPath' ; ExpectedSeverity = ' Warning' }
53
+ @ { VariableName = ' ReportErrorShowExceptionClass' ; ExpectedSeverity = ' Warning' }
54
+ @ { VariableName = ' ReportErrorShowInnerException' ; ExpectedSeverity = ' Warning' }
55
+ @ { VariableName = ' ReportErrorShowSource' ; ExpectedSeverity = ' Warning' }
56
+ @ { VariableName = ' ReportErrorShowStackTrace' ; ExpectedSeverity = ' Warning' }
57
+ @ { VariableName = ' Sender' ; ExpectedSeverity = ' Warning' }
58
+ @ { VariableName = ' StackTrace' ; ExpectedSeverity = ' Warning' }
59
+ @ { VariableName = ' This' ; ExpectedSeverity = ' Warning' }
60
+ )
61
+
62
+ $testCases_ReadOnlyAutomaticVariables = $testCases_AutomaticVariables | Where-Object { $_.IsReadonly }
15
63
}
16
64
17
- $testCases_AutomaticVariables = @ (
18
- @ { VariableName = ' ?' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
19
- @ { VariableName = ' Error' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
20
- @ { VariableName = ' ExecutionContext' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
21
- @ { VariableName = ' false' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
22
- @ { VariableName = ' Home' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
23
- @ { VariableName = ' Host' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
24
- @ { VariableName = ' PID' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
25
- @ { VariableName = ' PSCulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
26
- @ { VariableName = ' PSEdition' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
27
- @ { VariableName = ' PSHome' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
28
- @ { VariableName = ' PSUICulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
29
- @ { VariableName = ' PSVersionTable' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
30
- @ { VariableName = ' ShellId' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
31
- @ { VariableName = ' true' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
32
- # Variables introduced only in PowerShell 6+ have a Severity of Warning only
33
- @ { VariableName = ' IsCoreCLR' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
34
- @ { VariableName = ' IsLinux' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
35
- @ { VariableName = ' IsMacOS' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
36
- @ { VariableName = ' IsWindows' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
37
- @ { VariableName = ' _' ; ExpectedSeverity = ' Warning' }
38
- @ { VariableName = ' AllNodes' ; ExpectedSeverity = ' Warning' }
39
- @ { VariableName = ' Args' ; ExpectedSeverity = ' Warning' }
40
- @ { VariableName = ' ConsoleFilename' ; ExpectedSeverity = ' Warning' }
41
- @ { VariableName = ' Event' ; ExpectedSeverity = ' Warning' }
42
- @ { VariableName = ' EventArgs' ; ExpectedSeverity = ' Warning' }
43
- @ { VariableName = ' EventSubscriber' ; ExpectedSeverity = ' Warning' }
44
- @ { VariableName = ' ForEach' ; ExpectedSeverity = ' Warning' }
45
- @ { VariableName = ' Input' ; ExpectedSeverity = ' Warning' }
46
- @ { VariableName = ' Matches' ; ExpectedSeverity = ' Warning' }
47
- @ { VariableName = ' MyInvocation' ; ExpectedSeverity = ' Warning' }
48
- @ { VariableName = ' NestedPromptLevel' ; ExpectedSeverity = ' Warning' }
49
- @ { VariableName = ' Profile' ; ExpectedSeverity = ' Warning' }
50
- @ { VariableName = ' PSBoundParameters' ; ExpectedSeverity = ' Warning' }
51
- @ { VariableName = ' PsCmdlet' ; ExpectedSeverity = ' Warning' }
52
- @ { VariableName = ' PSCommandPath' ; ExpectedSeverity = ' Warning' }
53
- @ { VariableName = ' ReportErrorShowExceptionClass' ; ExpectedSeverity = ' Warning' }
54
- @ { VariableName = ' ReportErrorShowInnerException' ; ExpectedSeverity = ' Warning' }
55
- @ { VariableName = ' ReportErrorShowSource' ; ExpectedSeverity = ' Warning' }
56
- @ { VariableName = ' ReportErrorShowStackTrace' ; ExpectedSeverity = ' Warning' }
57
- @ { VariableName = ' Sender' ; ExpectedSeverity = ' Warning' }
58
- @ { VariableName = ' StackTrace' ; ExpectedSeverity = ' Warning' }
59
- @ { VariableName = ' This' ; ExpectedSeverity = ' Warning' }
60
- )
61
-
62
- $testCases_ReadOnlyAutomaticVariables = $testCases_AutomaticVariables | Where-Object { $_.IsReadonly }
63
-
64
65
It " Variable <VariableName> produces warning of Severity <ExpectedSeverity>" - TestCases $testCases_AutomaticVariables {
65
66
param ($VariableName , $ExpectedSeverity )
66
67
@@ -133,6 +134,29 @@ Describe "AvoidAssignmentToAutomaticVariables" {
133
134
}
134
135
}
135
136
}
137
+ }
138
+
139
+ Context ' Suppression' {
140
+ BeforeDiscovery {
141
+ $testCases_RuleSuppression = @ (
142
+ @ { VariableName = ' true' ; Type = ' ReadOnlyAutomaticVariableError' }
143
+ @ { VariableName = ' IsWindows' ; Type = ' ReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error' }
144
+ @ { VariableName = ' ForEach' ; Type = ' WritableAutomaticVariableError' }
145
+ )
146
+ }
147
+
148
+ It ' Variable <VariableName> of type <Type> can be suppressed by RuleSuppressionId' - TestCases $testCases_RuleSuppression {
149
+ $scriptDef = @"
150
+ function suppressionTest {
151
+ [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('$ruleName ', '$VariableName ')]
152
+ param(
153
+ `$ $VariableName
154
+ )
155
+ }
156
+ "@
136
157
158
+ $warnings = @ (Invoke-ScriptAnalyzer - ScriptDefinition $scriptDef - ExcludeRule PSReviewUnusedParameter)
159
+ $warnings.Count | Should - Be 0
160
+ }
137
161
}
138
162
}
0 commit comments