Skip to content

Commit 74cc342

Browse files
authored
Merge pull request #13539 from MBradbury/powershell-test-lastexitcode
Check LASTEXITCODE is set before checking its value
2 parents e4e52c0 + 4da6b47 commit 74cc342

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

provisioner/powershell/provisioner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const wrapPowershellString string = `
5555
$exitCode = 1
5656
}
5757
58-
if ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) {
58+
if ((Test-Path variable:global:LASTEXITCODE) -and $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) {
5959
$exitCode = $LASTEXITCODE
6060
}
6161
exit $exitCode

provisioner/powershell/provisioner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestProvisionerPrepare_extractScript(t *testing.T) {
3636

3737
// File contents should contain 2 lines concatenated by newlines: foo\nbar
3838
readFile, err := os.ReadFile(file)
39-
expectedContents := "if (Test-Path variable:global:ProgressPreference) {\n set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'\n }\n \n $exitCode = 0\n try {\n $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; \n foo\n bar\n \n $exitCode = 0\n } catch {\n Write-Error \"An error occurred: $_\"\n $exitCode = 1\n }\n \n if ($LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) {\n $exitCode = $LASTEXITCODE\n }\n exit $exitCode"
39+
expectedContents := "if (Test-Path variable:global:ProgressPreference) {\n set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'\n }\n \n $exitCode = 0\n try {\n $env:PACKER_BUILDER_TYPE=\"\"; $env:PACKER_BUILD_NAME=\"\"; \n foo\n bar\n \n $exitCode = 0\n } catch {\n Write-Error \"An error occurred: $_\"\n $exitCode = 1\n }\n \n if ((Test-Path variable:global:LASTEXITCODE) -and $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0) {\n $exitCode = $LASTEXITCODE\n }\n exit $exitCode"
4040
normalizedExpectedContent := normalizeWhiteSpace(expectedContents)
4141
if err != nil {
4242
t.Fatalf("Should not be error: %s", err)

0 commit comments

Comments
 (0)