Skip to content

Update special variable entries #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 21 additions & 16 deletions Engine/SpecialVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand All @@ -176,6 +180,7 @@ internal enum PreferenceVariable
PathExt,
PSEmailServer,
PSDefaultParameterValues,
PSModuleAutoLoadingPreference,
pwd,
Null,
True,
Expand Down
25 changes: 21 additions & 4 deletions Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1
Original file line number Diff line number Diff line change
@@ -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}

Expand Down Expand Up @@ -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
}
}

Expand Down