From dd253d122c836210efb731095e85876228f41708 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sun, 24 Jul 2016 11:57:51 -0400 Subject: [PATCH] Write err to stderr, exit 1 if err != "" Closes #475 --- app_test.go | 13 ++++++++++++ errors.go | 6 ++++++ errors_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/app_test.go b/app_test.go index b0b02e6..23c8aa6 100644 --- a/app_test.go +++ b/app_test.go @@ -13,6 +13,19 @@ import ( "testing" ) +var ( + lastExitCode = 0 + fakeOsExiter = func(rc int) { + lastExitCode = rc + } + fakeErrWriter = &bytes.Buffer{} +) + +func init() { + OsExiter = fakeOsExiter + ErrWriter = fakeErrWriter +} + type opCounts struct { Total, BashComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int } diff --git a/errors.go b/errors.go index ea551be..15ac790 100644 --- a/errors.go +++ b/errors.go @@ -88,5 +88,11 @@ func HandleExitCoder(err error) { for _, merr := range multiErr.Errors { HandleExitCoder(merr) } + return + } + + if err.Error() != "" { + fmt.Fprintln(ErrWriter, err) + OsExiter(1) } } diff --git a/errors_test.go b/errors_test.go index 8f5f284..e357dc4 100644 --- a/errors_test.go +++ b/errors_test.go @@ -1,8 +1,8 @@ package cli import ( + "bytes" "errors" - "os" "testing" ) @@ -15,7 +15,7 @@ func TestHandleExitCoder_nil(t *testing.T) { called = true } - defer func() { OsExiter = os.Exit }() + defer func() { OsExiter = fakeOsExiter }() HandleExitCoder(nil) @@ -32,7 +32,7 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) { called = true } - defer func() { OsExiter = os.Exit }() + defer func() { OsExiter = fakeOsExiter }() HandleExitCoder(NewExitError("galactic perimeter breach", 9)) @@ -49,7 +49,7 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) { called = true } - defer func() { OsExiter = os.Exit }() + defer func() { OsExiter = fakeOsExiter }() exitErr := NewExitError("galactic perimeter breach", 9) err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr) @@ -58,3 +58,49 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) { expect(t, exitCode, 9) expect(t, called, true) } + +func TestHandleExitCoder_ErrorWithMessage(t *testing.T) { + exitCode := 0 + called := false + + OsExiter = func(rc int) { + exitCode = rc + called = true + } + ErrWriter = &bytes.Buffer{} + + defer func() { + OsExiter = fakeOsExiter + ErrWriter = fakeErrWriter + }() + + err := errors.New("gourd havens") + HandleExitCoder(err) + + expect(t, exitCode, 1) + expect(t, called, true) + expect(t, ErrWriter.(*bytes.Buffer).String(), "gourd havens\n") +} + +func TestHandleExitCoder_ErrorWithoutMessage(t *testing.T) { + exitCode := 0 + called := false + + OsExiter = func(rc int) { + exitCode = rc + called = true + } + ErrWriter = &bytes.Buffer{} + + defer func() { + OsExiter = fakeOsExiter + ErrWriter = fakeErrWriter + }() + + err := errors.New("") + HandleExitCoder(err) + + expect(t, exitCode, 0) + expect(t, called, false) + expect(t, ErrWriter.(*bytes.Buffer).String(), "") +}