diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 24acb7344..02e830a8d 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -6,7 +6,9 @@ + to check for commands in Microsoft.PowerShell.Core snapin + to ignore aliases + to ignore custom defind commands - +- PSUseDeclaredVarsMoreThanAssignments rule to ignore the following special variables (#653) + + `$PSModuleAutoLoadingPreference` + + `$InformationPreference` ## [1.8.1](https://github.com/PowerShell/PSScriptAnalyzer/tree/1.8.1) - 2016-10-13 ### Added diff --git a/Engine/SpecialVars.cs b/Engine/SpecialVars.cs index 60a3f6273..231295815 100644 --- a/Engine/SpecialVars.cs +++ b/Engine/SpecialVars.cs @@ -96,27 +96,30 @@ static SpecialVars() internal const string WarningPreference = "WarningPreference"; internal const string ConfirmPreference = "ConfirmPreference"; internal const string ProgressPreference = "ProgressPreference"; + internal const string InformationPreference = "InformationPreference"; - internal static readonly string[] PreferenceVariables = new string[] - { - DebugPreference, - VerbosePreference, - ErrorActionPreference, - WhatIfPreference, - WarningPreference, + internal static readonly string[] PreferenceVariables = new string[] + { + DebugPreference, + VerbosePreference, + ErrorActionPreference, + WhatIfPreference, + WarningPreference, ConfirmPreference, - ProgressPreference + ProgressPreference, + InformationPreference }; - internal static readonly Type[] PreferenceVariableTypes = new Type[] - { - /* DebugPreference */ typeof(ActionPreference), - /* VerbosePreference */ typeof(ActionPreference), - /* ErrorPreference */ typeof(ActionPreference), - /* WhatIfPreference */ typeof(SwitchParameter), - /* WarningPreference */ typeof(ActionPreference), - /* ConfirmPreference */ typeof(ConfirmImpact), + internal static readonly Type[] PreferenceVariableTypes = new Type[] + { + /* DebugPreference */ typeof(ActionPreference), + /* VerbosePreference */ typeof(ActionPreference), + /* ErrorPreference */ typeof(ActionPreference), + /* WhatIfPreference */ typeof(SwitchParameter), + /* WarningPreference */ typeof(ActionPreference), + /* ConfirmPreference */ typeof(ConfirmImpact), /* ProgressPreference */ typeof(Enum), + /* InformationPreference */ typeof(ActionPreference), }; internal enum AutomaticVariable @@ -156,6 +159,7 @@ internal enum PreferenceVariable internal const string PathExt = "env:PATHEXT"; internal const string PSEmailServer = "PSEmailServer"; internal const string PSDefaultParameterValues = "PSDefaultParameterValues"; + internal const string PSModuleAutoLoadingPreference = "PSModuleAutoLoadingPreference"; internal const string pwd = "PWD"; internal const string Null = "null"; internal const string True = "true"; @@ -176,6 +180,7 @@ internal enum PreferenceVariable PathExt, PSEmailServer, PSDefaultParameterValues, + PSModuleAutoLoadingPreference, pwd, Null, True, diff --git a/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 b/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 index 82ad40d44..4f94c35d3 100644 --- a/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 +++ b/Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 @@ -1,7 +1,11 @@ -Import-Module PSScriptAnalyzer +$directory = Split-Path -Parent $MyInvocation.MyCommand.Path +$testRootDirectory = Split-Path -Parent $directory + +Import-Module PSScriptAnalyzer +Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1') + $violationMessage = "The variable 'declaredVar2' is assigned but never used." $violationName = "PSUseDeclaredVarsMoreThanAssignments" -$directory = Split-Path -Parent $MyInvocation.MyCommand.Path $violations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignments.ps1 | Where-Object {$_.RuleName -eq $violationName} $noViolations = Invoke-ScriptAnalyzer $directory\UseDeclaredVarsMoreThanAssignmentsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName} @@ -29,8 +33,21 @@ function MyFunc2() { $a + $a } '@ - $local:violations = Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName - $local:violations.Count | Should Be 1 + Invoke-ScriptAnalyzer -ScriptDefinition $target -IncludeRule $violationName | ` + Get-Count | ` + Should Be 1 + } + + It "does not flag `$InformationPreference variable" { + Invoke-ScriptAnalyzer -ScriptDefinition '$InformationPreference=Stop' -IncludeRule $violationName | ` + Get-Count | ` + Should Be 0 + } + + It "does not flag `$PSModuleAutoLoadingPreference variable" { + Invoke-ScriptAnalyzer -ScriptDefinition '$PSModuleAutoLoadingPreference=None' -IncludeRule $violationName | ` + Get-Count | ` + Should Be 0 } }