Skip to content

Commit 42bbd98

Browse files
author
Kapil Borle
authored
Merge pull request #752 from PowerShell/kapilmb/fix-avoid-global-var-rule
Excludes lastexitcode from AvoidGlobalVars checks
2 parents b0aae75 + 956594c commit 42bbd98

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Engine/SpecialVars.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ internal enum PreferenceVariable
164164
internal const string Null = "null";
165165
internal const string True = "true";
166166
internal const string False = "false";
167+
internal const string LastExitCode = "LastExitCode";
167168

168169
internal static readonly string[] OtherInitializedVariables = new string[]
169170
{
@@ -184,7 +185,8 @@ internal enum PreferenceVariable
184185
pwd,
185186
Null,
186187
True,
187-
False
188+
False,
189+
LastExitCode
188190
};
189191

190192
}

Tests/Rules/AvoidGlobalOrUnitializedVars.tests.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Describe "AvoidGlobalVars" {
2828
It "has 1 avoid using global variable violation" {
2929
$globalViolations.Count | Should Be 1
3030
}
31-
31+
3232
<#
3333
# PSAvoidUninitializedVariable rule has been deprecated
3434
It "has 4 violations for dsc resources (not counting the variables in parameters)" {
@@ -47,6 +47,19 @@ Describe "AvoidGlobalVars" {
4747
$noGlobalViolations.Count | Should Be 0
4848
}
4949
}
50+
51+
Context "When a script contains global:lastexitcode" {
52+
It "returns no violation" {
53+
$def = @'
54+
if ($global:lastexitcode -ne 0)
55+
{
56+
exit
57+
}
58+
'@
59+
$local:violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -IncludeRule $globalName
60+
$local:violations.Count | Should Be 0
61+
}
62+
}
5063
}
5164

5265
<#
@@ -66,6 +79,6 @@ Describe "AvoidUnitializedVars" {
6679
It "returns no violations" {
6780
$noUninitializedViolations.Count | Should Be 0
6881
}
69-
}
82+
}
7083
}
71-
#>
84+
#>

0 commit comments

Comments
 (0)