diff --git a/command/hcl2_upgrade.go b/command/hcl2_upgrade.go index 7f2b0e137..07eddaf07 100644 --- a/command/hcl2_upgrade.go +++ b/command/hcl2_upgrade.go @@ -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 diff --git a/command/hcl2_upgrade_test.go b/command/hcl2_upgrade_test.go index e3ca4a0a6..fc5292f7c 100644 --- a/command/hcl2_upgrade_test.go +++ b/command/hcl2_upgrade_test.go @@ -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) }) } }