diff --git a/commands/commands.go b/commands/commands.go index f5d1c1ed41..b56c71a597 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -164,13 +164,15 @@ func runCommand(command func(commandLine CommandLine, api libmachine.API) error) if crashErr, ok := err.(crashreport.CrashError); ok { crashReporter := crashreport.NewCrashReporter(mcndirs.GetBaseDir(), context.GlobalString("bugsnag-api-token")) crashReporter.Send(crashErr) + + if _, ok := crashErr.Cause.(mcnerror.ErrDuringPreCreate); ok { + osExit(3) + return + } } - if _, ok := err.(mcnerror.ErrDuringPreCreate); ok { - osExit(3) - } else { - osExit(1) - } + osExit(1) + return } } } diff --git a/commands/commands_test.go b/commands/commands_test.go index 8a8c21944b..42f647c0d4 100644 --- a/commands/commands_test.go +++ b/commands/commands_test.go @@ -200,31 +200,38 @@ func TestReturnExitCode1onError(t *testing.T) { exitCode := checkErrorCodeForCommand(command) - assert.Equal(t, exitCode, 1) + assert.Equal(t, 1, exitCode) } func TestReturnExitCode3onErrorDuringPreCreate(t *testing.T) { command := func(commandLine CommandLine, api libmachine.API) error { - return mcnerror.ErrDuringPreCreate{ - Cause: errors.New("foo is not bar"), + return crashreport.CrashError{ + Cause: mcnerror.ErrDuringPreCreate{ + Cause: errors.New("foo is not bar"), + }, } } exitCode := checkErrorCodeForCommand(command) - assert.Equal(t, exitCode, 3) + assert.Equal(t, 3, exitCode) } func checkErrorCodeForCommand(command func(commandLine CommandLine, api libmachine.API) error) int { - var exitCode int + var setExitCode int + + originalOSExit := osExit + + defer func() { + osExit = originalOSExit + }() - defer func(fnOsExit func(code int)) { osExit = fnOsExit }(osExit) osExit = func(code int) { - exitCode = code + setExitCode = code } context := cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil) runCommand(command)(context) - return exitCode + return setExitCode }