mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-13 18:50:11 -04:00
avoid hcl2_upgrade panic when file does not exist (#11206)
This commit is contained in:
parent
3f8c8704e5
commit
57f37c99ea
2 changed files with 14 additions and 8 deletions
|
|
@ -154,6 +154,7 @@ func (c *HCL2UpgradeCommand) RunContext(_ context.Context, cla *HCL2UpgradeArgs)
|
|||
hdl, ret := c.GetConfigFromJSON(&cla.MetaArgs)
|
||||
if ret != 0 {
|
||||
c.Ui.Error(fmt.Sprintf("Failed to get config from JSON"))
|
||||
return 1
|
||||
}
|
||||
|
||||
core := hdl.(*CoreWrapper).Core
|
||||
|
|
|
|||
|
|
@ -17,9 +17,10 @@ func Test_hcl2_upgrade(t *testing.T) {
|
|||
_ = cwd
|
||||
|
||||
tc := []struct {
|
||||
folder string
|
||||
flags []string
|
||||
exitCode int
|
||||
folder string
|
||||
flags []string
|
||||
exitCode int
|
||||
exitEarly bool
|
||||
}{
|
||||
{folder: "unknown_builder", flags: []string{}, exitCode: 1},
|
||||
{folder: "complete", flags: []string{"-with-annotations"}},
|
||||
|
|
@ -32,6 +33,7 @@ func Test_hcl2_upgrade(t *testing.T) {
|
|||
{folder: "variables-with-variables", flags: []string{}},
|
||||
{folder: "complete-variables-with-template-engine", flags: []string{}},
|
||||
{folder: "escaping", flags: []string{}},
|
||||
{folder: "inexistent", flags: []string{}, exitCode: 1, exitEarly: true},
|
||||
}
|
||||
|
||||
for _, tc := range tc {
|
||||
|
|
@ -46,20 +48,23 @@ func Test_hcl2_upgrade(t *testing.T) {
|
|||
args = append(args, inputPath)
|
||||
p := helperCommand(t, args...)
|
||||
err := p.Run()
|
||||
defer os.Remove(outputPath)
|
||||
if err != nil {
|
||||
t.Logf("run returned an error: %s", err)
|
||||
}
|
||||
actualExitCode := p.ProcessState.ExitCode()
|
||||
if tc.exitCode != actualExitCode {
|
||||
t.Fatalf("unexpected exit code: %d found; expected %d ", actualExitCode, tc.exitCode)
|
||||
}
|
||||
if tc.exitEarly {
|
||||
return
|
||||
}
|
||||
expected := string(mustBytes(ioutil.ReadFile(expectedPath)))
|
||||
actual := string(mustBytes(ioutil.ReadFile(outputPath)))
|
||||
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
t.Fatalf("unexpected output: %s", diff)
|
||||
}
|
||||
actualExitCode := p.ProcessState.ExitCode()
|
||||
if tc.exitCode != actualExitCode {
|
||||
t.Fatalf("unexpected exit code: %d found; expected %d ", actualExitCode, tc.exitCode)
|
||||
}
|
||||
os.Remove(outputPath)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue