|
3 | 3 | Describe "AvoidAssignmentToAutomaticVariables" {
|
4 | 4 | Context "ReadOnly Variables" {
|
5 | 5 |
|
6 |
| - $readOnlyVariableSeverity = "Error" |
| 6 | + $excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Warning' |
| 7 | + if ($PSVersionTable.PSVersion.Major -ge 6) |
| 8 | + { |
| 9 | + $excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Error' |
| 10 | + } |
| 11 | + |
7 | 12 | $testCases_ReadOnlyVariables = @(
|
8 |
| - @{ VariableName = '?' } |
9 |
| - @{ VariableName = 'Error' } |
10 |
| - @{ VariableName = 'ExecutionContext' } |
11 |
| - @{ VariableName = 'false' } |
12 |
| - @{ VariableName = 'Home' } |
13 |
| - @{ VariableName = 'Host' } |
14 |
| - @{ VariableName = 'PID' } |
15 |
| - @{ VariableName = 'PSCulture' } |
16 |
| - @{ VariableName = 'PSEdition' } |
17 |
| - @{ VariableName = 'PSHome' } |
18 |
| - @{ VariableName = 'PSUICulture' } |
19 |
| - @{ VariableName = 'PSVersionTable' } |
20 |
| - @{ VariableName = 'ShellId' } |
21 |
| - @{ VariableName = 'true' } |
| 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 |
| 28 | + @{ VariableName = 'IsCoreCLR'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } |
| 29 | + @{ VariableName = 'IsLinux'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } |
| 30 | + @{ VariableName = 'IsMacOS'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } |
| 31 | + @{ VariableName = 'IsWindows'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } |
22 | 32 | )
|
23 | 33 |
|
24 |
| - It "Variable '<VariableName>' produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { |
25 |
| - param ($VariableName) |
| 34 | + It "Variable <VariableName> produces warning of Severity <ExpectedSeverity>" -TestCases $testCases_ReadOnlyVariables { |
| 35 | + param ($VariableName, $ExpectedSeverity) |
26 | 36 |
|
27 |
| - $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" | Where-Object { $_.RuleName -eq $ruleName } |
| 37 | + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" -ExcludeRule PSUseDeclaredVarsMoreThanAssignments |
28 | 38 | $warnings.Count | Should -Be 1
|
29 |
| - $warnings.Severity | Should -Be $readOnlyVariableSeverity |
| 39 | + $warnings.Severity | Should -Be $ExpectedSeverity |
| 40 | + $warnings.RuleName | Should -Be $ruleName |
30 | 41 | }
|
31 | 42 |
|
32 |
| - It "Using Variable '<VariableName>' as parameter name produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { |
33 |
| - param ($VariableName) |
| 43 | + It "Using Variable <VariableName> as parameter name produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { |
| 44 | + param ($VariableName, $ExpectedSeverity) |
34 | 45 |
|
35 |
| - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" | Where-Object {$_.RuleName -eq $ruleName } |
| 46 | + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" |
36 | 47 | $warnings.Count | Should -Be 1
|
37 |
| - $warnings.Severity | Should -Be $readOnlyVariableSeverity |
| 48 | + $warnings.Severity | Should -Be $ExpectedSeverity |
| 49 | + $warnings.RuleName | Should -Be $ruleName |
38 | 50 | }
|
39 | 51 |
|
40 |
| - It "Using Variable '<VariableName>' as parameter name in param block produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { |
41 |
| - param ($VariableName) |
| 52 | + It "Using Variable <VariableName> as parameter name in param block produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { |
| 53 | + param ($VariableName, $ExpectedSeverity) |
42 | 54 |
|
43 |
| - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" | Where-Object {$_.RuleName -eq $ruleName } |
| 55 | + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" |
44 | 56 | $warnings.Count | Should -Be 1
|
45 |
| - $warnings.Severity | Should -Be $readOnlyVariableSeverity |
| 57 | + $warnings.Severity | Should -Be $ExpectedSeverity |
| 58 | + $warnings.RuleName | Should -Be $ruleName |
46 | 59 | }
|
47 | 60 |
|
48 | 61 | It "Does not flag parameter attributes" {
|
49 |
| - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'function foo{Param([Parameter(Mandatory=$true)]$param1)}' | Where-Object { $_.RuleName -eq $ruleName } |
| 62 | + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'function foo{Param([Parameter(Mandatory=$true)]$param1)}' |
50 | 63 | $warnings.Count | Should -Be 0
|
51 | 64 | }
|
52 | 65 |
|
53 |
| - It "Setting Variable '<VariableName>' throws exception to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { |
54 |
| - param ($VariableName) |
| 66 | + It "Setting Variable <VariableName> throws exception in applicable PowerShell version to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { |
| 67 | + param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr) |
| 68 | + |
| 69 | + if ($OnlyPresentInCoreClr -and !$IsCoreCLR) |
| 70 | + { |
| 71 | + # In this special case we expect it to not throw |
| 72 | + Set-Variable -Name $VariableName -Value 'foo' |
| 73 | + continue |
| 74 | + } |
55 | 75 |
|
56 | 76 | # Setting the $Error variable has the side effect of the ErrorVariable to contain only the exception message string, therefore exclude this case.
|
57 | 77 | # For the library test in WMF 4, assigning a value $PSEdition does not seem to throw an error, therefore this special case is excluded as well.
|
|
0 commit comments