Close #558: detect FormattedError and print their stack trace

This commit is contained in:
mh-cbon
2016-11-05 10:27:46 +01:00
parent d86a009f5e
commit 79591889a9
2 changed files with 40 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package cli
import (
"bytes"
"errors"
"fmt"
"testing"
)
@@ -104,3 +105,32 @@ func TestHandleExitCoder_ErrorWithoutMessage(t *testing.T) {
expect(t, called, true)
expect(t, ErrWriter.(*bytes.Buffer).String(), "")
}
// make a stub to not import pkg/errors
type ErrorWithFormat struct {
error
}
func (f *ErrorWithFormat) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, "This the format: %v", f.error)
}
func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
called := false
OsExiter = func(rc int) {
called = true
}
ErrWriter = &bytes.Buffer{}
defer func() {
OsExiter = fakeOsExiter
ErrWriter = fakeErrWriter
}()
err := &ErrorWithFormat{error: errors.New("I am formatted")}
HandleExitCoder(err)
expect(t, called, true)
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
}