@@ -9,29 +9,54 @@ Describe "AvoidAssignmentToAutomaticVariables" {
9
9
$excpectedSeverityForAutomaticVariablesInPowerShell6 = ' Error'
10
10
}
11
11
12
- $testCases_ReadOnlyVariables = @ (
13
- @ { VariableName = ' ?' ; ExpectedSeverity = ' Error' ; }
14
- @ { VariableName = ' Error' ; ExpectedSeverity = ' Error' }
15
- @ { VariableName = ' ExecutionContext' ; ExpectedSeverity = ' Error' }
16
- @ { VariableName = ' false' ; ExpectedSeverity = ' Error' }
17
- @ { VariableName = ' Home' ; ExpectedSeverity = ' Error' }
18
- @ { VariableName = ' Host' ; ExpectedSeverity = ' Error' }
19
- @ { VariableName = ' PID' ; ExpectedSeverity = ' Error' }
20
- @ { VariableName = ' PSCulture' ; ExpectedSeverity = ' Error' }
21
- @ { VariableName = ' PSEdition' ; ExpectedSeverity = ' Error' }
22
- @ { VariableName = ' PSHome' ; ExpectedSeverity = ' Error' }
23
- @ { VariableName = ' PSUICulture' ; ExpectedSeverity = ' Error' }
24
- @ { VariableName = ' PSVersionTable' ; ExpectedSeverity = ' Error' }
25
- @ { VariableName = ' ShellId' ; ExpectedSeverity = ' Error' }
26
- @ { VariableName = ' true' ; ExpectedSeverity = ' Error' }
27
- # Variables introuced only in PowerShell 6.0 have a Severity of Warning only
12
+ $testCases_AutomaticVariables = @ (
13
+ @ { VariableName = ' ?' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
14
+ @ { VariableName = ' Error' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
15
+ @ { VariableName = ' ExecutionContext' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
16
+ @ { VariableName = ' false' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
17
+ @ { VariableName = ' Home' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
18
+ @ { VariableName = ' Host' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
19
+ @ { VariableName = ' PID' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
20
+ @ { VariableName = ' PSCulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
21
+ @ { VariableName = ' PSEdition' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
22
+ @ { VariableName = ' PSHome' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
23
+ @ { VariableName = ' PSUICulture' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
24
+ @ { VariableName = ' PSVersionTable' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
25
+ @ { VariableName = ' ShellId' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
26
+ @ { VariableName = ' true' ; ExpectedSeverity = ' Error' ; IsReadOnly = $true }
27
+ # Variables introduced only in PowerShell 6+ have a Severity of Warning only
28
28
@ { VariableName = ' IsCoreCLR' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
29
29
@ { VariableName = ' IsLinux' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
30
30
@ { VariableName = ' IsMacOS' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
31
31
@ { VariableName = ' IsWindows' ; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6 ; OnlyPresentInCoreClr = $true }
32
+ @ { VariableName = ' _' ; ExpectedSeverity = ' Warning' }
33
+ @ { VariableName = ' AllNodes' ; ExpectedSeverity = ' Warning' }
34
+ @ { VariableName = ' Args' ; ExpectedSeverity = ' Warning' }
35
+ @ { VariableName = ' ConsoleFilename' ; ExpectedSeverity = ' Warning' }
36
+ @ { VariableName = ' Event' ; ExpectedSeverity = ' Warning' }
37
+ @ { VariableName = ' EventArgs' ; ExpectedSeverity = ' Warning' }
38
+ @ { VariableName = ' EventSubscriber' ; ExpectedSeverity = ' Warning' }
39
+ @ { VariableName = ' ForEach' ; ExpectedSeverity = ' Warning' }
40
+ @ { VariableName = ' Input' ; ExpectedSeverity = ' Warning' }
41
+ @ { VariableName = ' Matches' ; ExpectedSeverity = ' Warning' }
42
+ @ { VariableName = ' MyInvocation' ; ExpectedSeverity = ' Warning' }
43
+ @ { VariableName = ' NestedPromptLevel' ; ExpectedSeverity = ' Warning' }
44
+ @ { VariableName = ' Profile' ; ExpectedSeverity = ' Warning' }
45
+ @ { VariableName = ' PSBoundParameters' ; ExpectedSeverity = ' Warning' }
46
+ @ { VariableName = ' PsCmdlet' ; ExpectedSeverity = ' Warning' }
47
+ @ { VariableName = ' PSCommandPath' ; ExpectedSeverity = ' Warning' }
48
+ @ { VariableName = ' ReportErrorShowExceptionClass' ; ExpectedSeverity = ' Warning' }
49
+ @ { VariableName = ' ReportErrorShowInnerException' ; ExpectedSeverity = ' Warning' }
50
+ @ { VariableName = ' ReportErrorShowSource' ; ExpectedSeverity = ' Warning' }
51
+ @ { VariableName = ' ReportErrorShowStackTrace' ; ExpectedSeverity = ' Warning' }
52
+ @ { VariableName = ' Sender' ; ExpectedSeverity = ' Warning' }
53
+ @ { VariableName = ' StackTrace' ; ExpectedSeverity = ' Warning' }
54
+ @ { VariableName = ' This' ; ExpectedSeverity = ' Warning' }
32
55
)
33
56
34
- It " Variable <VariableName> produces warning of Severity <ExpectedSeverity>" - TestCases $testCases_ReadOnlyVariables {
57
+ $testCases_ReadOnlyAutomaticVariables = $testCases_AutomaticVariables | Where-Object { $_.IsReadonly }
58
+
59
+ It " Variable <VariableName> produces warning of Severity <ExpectedSeverity>" - TestCases $testCases_AutomaticVariables {
35
60
param ($VariableName , $ExpectedSeverity )
36
61
37
62
$warnings = Invoke-ScriptAnalyzer - ScriptDefinition " `$ ${VariableName} = 'foo'" - ExcludeRule PSUseDeclaredVarsMoreThanAssignments
@@ -40,7 +65,7 @@ Describe "AvoidAssignmentToAutomaticVariables" {
40
65
$warnings.RuleName | Should - Be $ruleName
41
66
}
42
67
43
- It " Using Variable <VariableName> as parameter name produces warning of Severity error" - TestCases $testCases_ReadOnlyVariables {
68
+ It " Using Variable <VariableName> as parameter name produces warning of Severity error" - TestCases $testCases_AutomaticVariables {
44
69
param ($VariableName , $ExpectedSeverity )
45
70
46
71
[System.Array ] $warnings = Invoke-ScriptAnalyzer - ScriptDefinition " function foo{Param(`$ $VariableName )}" - ExcludeRule PSReviewUnusedParameter
@@ -49,7 +74,7 @@ Describe "AvoidAssignmentToAutomaticVariables" {
49
74
$warnings.RuleName | Should - Be $ruleName
50
75
}
51
76
52
- It " Using Variable <VariableName> as parameter name in param block produces warning of Severity error" - TestCases $testCases_ReadOnlyVariables {
77
+ It " Using Variable <VariableName> as parameter name in param block produces warning of Severity error" - TestCases $testCases_AutomaticVariables {
53
78
param ($VariableName , $ExpectedSeverity )
54
79
55
80
[System.Array ] $warnings = Invoke-ScriptAnalyzer - ScriptDefinition " function foo(`$ $VariableName ){}"
@@ -77,8 +102,8 @@ Describe "AvoidAssignmentToAutomaticVariables" {
77
102
$warnings.Count | Should - Be 0
78
103
}
79
104
80
- It " Setting Variable <VariableName> throws exception in applicable PowerShell version to verify the variables is read-only" - TestCases $testCases_ReadOnlyVariables {
81
- param ($VariableName , $ExpectedSeverity , $OnlyPresentInCoreClr )
105
+ It " Setting Variable <VariableName> throws exception in applicable PowerShell version to verify the variables is read-only" - TestCases $testCases_ReadOnlyAutomaticVariables {
106
+ param ($VariableName , $ExpectedSeverity , $OnlyPresentInCoreClr , [ bool ] $IsReadOnly )
82
107
83
108
if ($OnlyPresentInCoreClr -and ! $IsCoreCLR )
84
109
{
0 commit comments