Add conditional to check if LastExitCode is defined

* Add broken requires statement test case
* Add test case to reproduce invalid LastExitCode
This commit is contained in:
Wilken Rivera 2020-05-04 13:52:34 -04:00
parent 35df3914d2
commit 38f799df3e
5 changed files with 40 additions and 7 deletions

View File

@ -100,7 +100,7 @@ func (p *Provisioner) defaultExecuteCommand() string {
baseCmd := `& { if (Test-Path variable:global:ProgressPreference)` +
`{set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};` +
`{set-variable -name variable:global:ErrorActionPreference -value 'Continue'};` +
`%s. {{.Vars}};try { . '{{.Path}}' } catch { Write-Error $Error[0]; exit 1 }; exit $LastExitCode }`
`%s. {{.Vars}};try { & '{{.Path}}' } catch { Write-Error $Error[0]; exit 1 }; if ($LastExitCode) { exit $LastExitCode }}`
var debugLine string
if p.config.DebugMode != 0 {

View File

@ -44,7 +44,7 @@ func TestAccPowershellProvisioner_Invalid(t *testing.T) {
// This provisioner should fail with an exit code of 1. To assert the failure the fixture
// uses the valid_exit_codes option to confirm a non-zero exit code
testProvisioner := PowershellProvisionerAccTest{"powershell-provisioner-invalid.txt"}
testProvisioner := PowershellProvisionerAccTest{"powershell-exit_codes-provisioner.txt"}
acc.TestProvisionersAgainstBuilders(&testProvisioner, t)
}

View File

@ -0,0 +1,19 @@
{
"type": "powershell",
"inline": ["invalid-cmdlet"],
"valid_exit_codes": ["1"]
},
{
"type": "powershell",
"inline": ["#Requires -Version 10.0"],
"valid_exit_codes": ["1"]
},
{
"type": "powershell",
"script": "../../provisioner/powershell/test-fixtures/scripts/set_version_latest.ps1"
},
{
"type": "powershell",
"inline": "sc.exe start Life",
"valid_exit_codes": ["1060"]
}

View File

@ -1,5 +0,0 @@
{
"type": "powershell",
"inline": ["invalid-cmdlet"],
"valid_exit_codes": ["1"]
}

View File

@ -0,0 +1,19 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function test
{
$myNumbersCollection = 1..5
if($myNumbersCollection -contains 3)
{
"collection contains 3"
}
else
{
"collection doesn't contain 3"
}
}
test