From eb7398555d760ec1f895bdca520badf593200cc2 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 3 Mar 2018 13:18:07 +0000 Subject: [PATCH 1/6] Add warnings about readonly variables introduced in PowerShell 6.0 --- Rules/AvoidAssignmentToAutomaticVariable.cs | 33 +++++++++-- Rules/Strings.Designer.cs | 9 +++ Rules/Strings.resx | 3 + ...oidAssignmentToAutomaticVariable.tests.ps1 | 59 +++++++++++-------- 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/Rules/AvoidAssignmentToAutomaticVariable.cs b/Rules/AvoidAssignmentToAutomaticVariable.cs index a474b4492..e7bdf2b78 100644 --- a/Rules/AvoidAssignmentToAutomaticVariable.cs +++ b/Rules/AvoidAssignmentToAutomaticVariable.cs @@ -31,10 +31,16 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules public class AvoidAssignmentToAutomaticVariable : IScriptRule { private static readonly IList _readOnlyAutomaticVariables = new List() - { - // Attempting to assign to any of those read-only variable would result in an error at runtime - "?", "true", "false", "Host", "PSCulture", "Error", "ExecutionContext", "Home", "PID", "PSEdition", "PSHome", "PSUICulture", "PSVersionTable", "ShellId" - }; + { + // Attempting to assign to any of those read-only variable would result in an error at runtime + "?", "true", "false", "Host", "PSCulture", "Error", "ExecutionContext", "Home", "PID", "PSEdition", "PSHome", "PSUICulture", "PSVersionTable", "ShellId" + }; + + private static readonly IList _readOnlyAutomaticVariablesIntroducedInVersion6_0 = new List() + { + // Attempting to assign to any of those read-only variable will result in an error at runtime + "IsCoreCLR", "IsLinux", "IsMacOS", "IsWindows" + }; /// /// Checks for assignment to automatic variables. @@ -56,6 +62,12 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); } + + if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) + { + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), + variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + } } IEnumerable parameterAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true); @@ -64,11 +76,22 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) var variableExpressionAst = parameterAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; var variableName = variableExpressionAst.VariablePath.UserPath; // also check the parent to exclude parameter attributes such as '[Parameter(Mandatory=$true)]' where the read-only variable $true appears. - if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase) && !(variableExpressionAst.Parent is NamedAttributeArgumentAst)) + if (variableExpressionAst.Parent is NamedAttributeArgumentAst) + { + continue; + } + + if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) { yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); } + + if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) + { + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), + variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + } } } diff --git a/Rules/Strings.Designer.cs b/Rules/Strings.Designer.cs index 565e67629..80c99bbdc 100644 --- a/Rules/Strings.Designer.cs +++ b/Rules/Strings.Designer.cs @@ -142,6 +142,15 @@ internal static string AvoidAssignmentToReadOnlyAutomaticVariableError { } } + /// + /// Looks up a localized string similar to Starting from PowerShell 6.0, the Variable '{0}' cannot be assigned any more since it is a readonly automatic variable that is built into PowerShell, please use a different name.. + /// + internal static string AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error { + get { + return ResourceManager.GetString("AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error", resourceCulture); + } + } + /// /// Looks up a localized string similar to Avoid Using ComputerName Hardcoded. /// diff --git a/Rules/Strings.resx b/Rules/Strings.resx index 5e0e1314e..968a4d6eb 100644 --- a/Rules/Strings.resx +++ b/Rules/Strings.resx @@ -1005,4 +1005,7 @@ AvoidAssignmentToAutomaticVariable + + Starting from PowerShell 6.0, the Variable '{0}' cannot be assigned any more since it is a readonly automatic variable that is built into PowerShell, please use a different name. + \ No newline at end of file diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index dc21ac4dc..7108b087e 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -4,46 +4,50 @@ $ruleName = "PSAvoidAssignmentToAutomaticVariable" Describe "AvoidAssignmentToAutomaticVariables" { Context "ReadOnly Variables" { - $readOnlyVariableSeverity = "Error" $testCases_ReadOnlyVariables = @( - @{ VariableName = '?' } - @{ VariableName = 'Error' } - @{ VariableName = 'ExecutionContext' } - @{ VariableName = 'false' } - @{ VariableName = 'Home' } - @{ VariableName = 'Host' } - @{ VariableName = 'PID' } - @{ VariableName = 'PSCulture' } - @{ VariableName = 'PSEdition' } - @{ VariableName = 'PSHome' } - @{ VariableName = 'PSUICulture' } - @{ VariableName = 'PSVersionTable' } - @{ VariableName = 'ShellId' } - @{ VariableName = 'true' } + @{ VariableName = '?'; ExpectedSeverity = 'Error'; } + @{ VariableName = 'Error' ; ExpectedSeverity = 'Error' } + @{ VariableName = 'ExecutionContext'; ExpectedSeverity = 'Error' } + @{ VariableName = 'false'; ExpectedSeverity = 'Error' } + @{ VariableName = 'Home'; ExpectedSeverity = 'Error' } + @{ VariableName = 'Host'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PID'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PSCulture'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PSEdition'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PSHome'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PSUICulture'; ExpectedSeverity = 'Error' } + @{ VariableName = 'PSVersionTable'; ExpectedSeverity = 'Error' } + @{ VariableName = 'ShellId'; ExpectedSeverity = 'Error' } + @{ VariableName = 'true'; ExpectedSeverity = 'Error' } + # Variables introuced only in PowerShell 6.0 have a Severity of Warning only + @{ VariableName = 'IsCoreCLR'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsLinux'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsMacOS'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsWindows'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } ) - It "Variable '' produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { - param ($VariableName) + It "Variable produces warning of Severity " -TestCases $testCases_ReadOnlyVariables { + param ($VariableName, $ExpectedSeverity) $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" | Where-Object { $_.RuleName -eq $ruleName } $warnings.Count | Should -Be 1 - $warnings.Severity | Should -Be $readOnlyVariableSeverity + $warnings.Severity | Should -Be $ExpectedSeverity } - It "Using Variable '' as parameter name produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { - param ($VariableName) + It "Using Variable as parameter name produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { + param ($VariableName, $ExpectedSeverity) [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" | Where-Object {$_.RuleName -eq $ruleName } $warnings.Count | Should -Be 1 - $warnings.Severity | Should -Be $readOnlyVariableSeverity + $warnings.Severity | Should -Be $ExpectedSeverity } - It "Using Variable '' as parameter name in param block produces warning of severity error" -TestCases $testCases_ReadOnlyVariables { - param ($VariableName) + It "Using Variable '' as parameter name in param block produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { + param ($VariableName, $ExpectedSeverity) [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" | Where-Object {$_.RuleName -eq $ruleName } $warnings.Count | Should -Be 1 - $warnings.Severity | Should -Be $readOnlyVariableSeverity + $warnings.Severity | Should -Be $ExpectedSeverity } It "Does not flag parameter attributes" { @@ -52,7 +56,12 @@ Describe "AvoidAssignmentToAutomaticVariables" { } It "Setting Variable '' throws exception to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { - param ($VariableName) + param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr) + + if ($OnlyPresentInCoreClr -and !$IsCoreCLR) + { + continue + } # Setting the $Error variable has the side effect of the ErrorVariable to contain only the exception message string, therefore exclude this case. # 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. From 178711cc522449ae7b1a93fa71e9deebce0e10ce Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 3 Mar 2018 13:26:34 +0000 Subject: [PATCH 2/6] use custom message for variables that only apply to ps 6.0 --- Rules/AvoidAssignmentToAutomaticVariable.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/AvoidAssignmentToAutomaticVariable.cs b/Rules/AvoidAssignmentToAutomaticVariable.cs index e7bdf2b78..564f5eaf4 100644 --- a/Rules/AvoidAssignmentToAutomaticVariable.cs +++ b/Rules/AvoidAssignmentToAutomaticVariable.cs @@ -65,7 +65,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) { - yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName), variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); } } @@ -89,7 +89,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) { - yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName), variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); } } From c55f39c3ac0f5a4aea4616530b5daaafe814680f Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 3 Mar 2018 13:29:55 +0000 Subject: [PATCH 3/6] Remove redundant quotes in IT block because Pester adds them now automatically --- Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index 7108b087e..1dc3af5aa 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -42,7 +42,7 @@ Describe "AvoidAssignmentToAutomaticVariables" { $warnings.Severity | Should -Be $ExpectedSeverity } - It "Using Variable '' as parameter name in param block produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { + It "Using Variable as parameter name in param block produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity) [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" | Where-Object {$_.RuleName -eq $ruleName } @@ -55,7 +55,7 @@ Describe "AvoidAssignmentToAutomaticVariables" { $warnings.Count | Should -Be 0 } - It "Setting Variable '' throws exception to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { + It "Setting Variable throws exception to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr) if ($OnlyPresentInCoreClr -and !$IsCoreCLR) From 5fda73df24025c6c718d2db6b05e2233a6940676 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 5 Mar 2018 21:58:52 +0000 Subject: [PATCH 4/6] Make it throw an Error when PSSA is executed on PSCore --- Rules/AvoidAssignmentToAutomaticVariable.cs | 17 ++++++++++++++--- ...voidAssignmentToAutomaticVariable.tests.ps1 | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Rules/AvoidAssignmentToAutomaticVariable.cs b/Rules/AvoidAssignmentToAutomaticVariable.cs index 564f5eaf4..9983c5ad7 100644 --- a/Rules/AvoidAssignmentToAutomaticVariable.cs +++ b/Rules/AvoidAssignmentToAutomaticVariable.cs @@ -65,8 +65,9 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) { + var severity = IsPowerShellVersion6OrGreater() ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning; yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName), - variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + variableExpressionAst.Extent, GetName(), severity, fileName); } } @@ -86,15 +87,25 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); } - if (_readOnlyAutomaticVariablesIntroducedInVersion6_0.Contains(variableName, StringComparer.OrdinalIgnoreCase)) { + var severity = IsPowerShellVersion6OrGreater() ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning; yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableIntroducedInPowerShell6_0Error, variableName), - variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName); + variableExpressionAst.Extent, GetName(), severity, fileName); } } } + private bool IsPowerShellVersion6OrGreater() + { + var psVersion = Helper.Instance.GetPSVersion(); + if (psVersion.Major >= 6) + { + return true; + } + return false; + } + /// /// GetName: Retrieves the name of this rule. /// diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index 1dc3af5aa..d8292be43 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -4,6 +4,12 @@ $ruleName = "PSAvoidAssignmentToAutomaticVariable" Describe "AvoidAssignmentToAutomaticVariables" { Context "ReadOnly Variables" { + $excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Warning' + if ($PSVersionTable.PSVersion.Major -ge 6) + { + $excpectedSeverityForAutomaticVariablesInPowerShell6 = 'Error' + } + $testCases_ReadOnlyVariables = @( @{ VariableName = '?'; ExpectedSeverity = 'Error'; } @{ VariableName = 'Error' ; ExpectedSeverity = 'Error' } @@ -20,10 +26,10 @@ Describe "AvoidAssignmentToAutomaticVariables" { @{ VariableName = 'ShellId'; ExpectedSeverity = 'Error' } @{ VariableName = 'true'; ExpectedSeverity = 'Error' } # Variables introuced only in PowerShell 6.0 have a Severity of Warning only - @{ VariableName = 'IsCoreCLR'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } - @{ VariableName = 'IsLinux'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } - @{ VariableName = 'IsMacOS'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } - @{ VariableName = 'IsWindows'; ExpectedSeverity = 'Warning'; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsCoreCLR'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsLinux'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsMacOS'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } + @{ VariableName = 'IsWindows'; ExpectedSeverity = $excpectedSeverityForAutomaticVariablesInPowerShell6; OnlyPresentInCoreClr = $true } ) It "Variable produces warning of Severity " -TestCases $testCases_ReadOnlyVariables { @@ -55,11 +61,13 @@ Describe "AvoidAssignmentToAutomaticVariables" { $warnings.Count | Should -Be 0 } - It "Setting Variable throws exception to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { + It "Setting Variable throws exception in applicable PowerShell version to verify the variables is read-only" -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity, $OnlyPresentInCoreClr) if ($OnlyPresentInCoreClr -and !$IsCoreCLR) { + # In this special case we expect it to not throw + Set-Variable -Name $VariableName -Value 'foo' continue } From bbc8f3d9fba4bce98b6c7f5239b7d29fd19dc704 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Fri, 23 Mar 2018 19:45:14 +0000 Subject: [PATCH 5/6] address pr comments --- .../AvoidAssignmentToAutomaticVariable.tests.ps1 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index d8292be43..ee4f2e726 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -35,29 +35,32 @@ Describe "AvoidAssignmentToAutomaticVariables" { It "Variable produces warning of Severity " -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity) - $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" | Where-Object { $_.RuleName -eq $ruleName } + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" $warnings.Count | Should -Be 1 $warnings.Severity | Should -Be $ExpectedSeverity + $warnings.RuleName | Should -Be $ruleName } It "Using Variable as parameter name produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity) - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" | Where-Object {$_.RuleName -eq $ruleName } + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo{Param(`$$VariableName)}" $warnings.Count | Should -Be 1 $warnings.Severity | Should -Be $ExpectedSeverity + $warnings.RuleName | Should -Be $ruleName } It "Using Variable as parameter name in param block produces warning of Severity error" -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity) - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" | Where-Object {$_.RuleName -eq $ruleName } + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" $warnings.Count | Should -Be 1 $warnings.Severity | Should -Be $ExpectedSeverity + $warnings.RuleName | Should -Be $ruleName } It "Does not flag parameter attributes" { - [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'function foo{Param([Parameter(Mandatory=$true)]$param1)}' | Where-Object { $_.RuleName -eq $ruleName } + [System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition 'function foo{Param([Parameter(Mandatory=$true)]$param1)}' $warnings.Count | Should -Be 0 } From d81a5a8ed7520f1acb76bf93de035a7239ccc828 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Fri, 23 Mar 2018 19:53:32 +0000 Subject: [PATCH 6/6] exclude PSUseDeclaredVarsMoreThanAssignments rule for tests --- Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 index ee4f2e726..6de3f1940 100644 --- a/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 +++ b/Tests/Rules/AvoidAssignmentToAutomaticVariable.tests.ps1 @@ -35,7 +35,7 @@ Describe "AvoidAssignmentToAutomaticVariables" { It "Variable produces warning of Severity " -TestCases $testCases_ReadOnlyVariables { param ($VariableName, $ExpectedSeverity) - $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" + $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "`$${VariableName} = 'foo'" -ExcludeRule PSUseDeclaredVarsMoreThanAssignments $warnings.Count | Should -Be 1 $warnings.Severity | Should -Be $ExpectedSeverity $warnings.RuleName | Should -Be $ruleName