2016-05-02 15:25:37 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2016-07-24 15:57:51 +00:00
|
|
|
"bytes"
|
2016-05-02 15:25:37 +00:00
|
|
|
"errors"
|
2016-11-05 09:27:46 +00:00
|
|
|
"fmt"
|
2016-05-02 15:25:37 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleExitCoder_nil(t *testing.T) {
|
|
|
|
exitCode := 0
|
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
2017-01-12 09:59:38 +00:00
|
|
|
if !called {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
2016-05-02 15:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-05-02 15:25:37 +00:00
|
|
|
|
|
|
|
HandleExitCoder(nil)
|
|
|
|
|
|
|
|
expect(t, exitCode, 0)
|
|
|
|
expect(t, called, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleExitCoder_ExitCoder(t *testing.T) {
|
|
|
|
exitCode := 0
|
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
2017-01-12 09:59:38 +00:00
|
|
|
if !called {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
2016-05-02 15:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-05-02 15:25:37 +00:00
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
HandleExitCoder(Exit("galactic perimeter breach", 9))
|
2016-05-02 15:25:37 +00:00
|
|
|
|
|
|
|
expect(t, exitCode, 9)
|
|
|
|
expect(t, called, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
|
|
|
|
exitCode := 0
|
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
2017-01-12 09:59:38 +00:00
|
|
|
if !called {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
2016-05-02 15:25:37 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-05-02 15:25:37 +00:00
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
exitErr := Exit("galactic perimeter breach", 9)
|
2017-08-04 15:59:27 +00:00
|
|
|
exitErr2 := Exit("last ExitCoder", 11)
|
|
|
|
err := newMultiError(errors.New("wowsa"), errors.New("egad"), exitErr, exitErr2)
|
2016-05-02 15:25:37 +00:00
|
|
|
HandleExitCoder(err)
|
|
|
|
|
2017-01-13 06:37:09 +00:00
|
|
|
expect(t, exitCode, 11)
|
2016-05-02 15:25:37 +00:00
|
|
|
expect(t, called, true)
|
|
|
|
}
|
2016-07-24 15:57:51 +00:00
|
|
|
|
2020-03-25 00:32:39 +00:00
|
|
|
func TestHandleExitCoder_MultiErrorWithoutExitCoder(t *testing.T) {
|
|
|
|
exitCode := 0
|
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
|
|
|
if !called {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
|
|
|
|
|
|
|
err := newMultiError(errors.New("wowsa"), errors.New("egad"))
|
|
|
|
HandleExitCoder(err)
|
|
|
|
|
|
|
|
expect(t, exitCode, 1)
|
|
|
|
expect(t, called, true)
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:27:46 +00:00
|
|
|
// make a stub to not import pkg/errors
|
|
|
|
type ErrorWithFormat struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:20:13 +00:00
|
|
|
func NewErrorWithFormat(m string) *ErrorWithFormat {
|
|
|
|
return &ErrorWithFormat{error: errors.New(m)}
|
|
|
|
}
|
|
|
|
|
2016-11-05 09:27:46 +00:00
|
|
|
func (f *ErrorWithFormat) Format(s fmt.State, verb rune) {
|
|
|
|
fmt.Fprintf(s, "This the format: %v", f.error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
|
2016-07-24 15:57:51 +00:00
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
2017-01-12 09:59:38 +00:00
|
|
|
if !called {
|
|
|
|
called = true
|
|
|
|
}
|
2016-07-24 15:57:51 +00:00
|
|
|
}
|
|
|
|
ErrWriter = &bytes.Buffer{}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
OsExiter = fakeOsExiter
|
|
|
|
ErrWriter = fakeErrWriter
|
|
|
|
}()
|
|
|
|
|
2017-08-04 16:00:22 +00:00
|
|
|
err := Exit(NewErrorWithFormat("I am formatted"), 1)
|
2016-07-24 15:57:51 +00:00
|
|
|
HandleExitCoder(err)
|
|
|
|
|
|
|
|
expect(t, called, true)
|
2016-11-05 09:27:46 +00:00
|
|
|
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: I am formatted\n")
|
2016-07-24 15:57:51 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 21:20:13 +00:00
|
|
|
func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
|
2016-07-24 15:57:51 +00:00
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
2017-01-12 09:59:38 +00:00
|
|
|
if !called {
|
|
|
|
called = true
|
|
|
|
}
|
2016-07-24 15:57:51 +00:00
|
|
|
}
|
|
|
|
ErrWriter = &bytes.Buffer{}
|
|
|
|
|
2016-11-13 21:20:13 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-07-24 15:57:51 +00:00
|
|
|
|
2017-08-04 16:00:22 +00:00
|
|
|
err := newMultiError(NewErrorWithFormat("err1"), NewErrorWithFormat("err2"))
|
2016-07-24 15:57:51 +00:00
|
|
|
HandleExitCoder(err)
|
|
|
|
|
2017-08-04 16:43:16 +00:00
|
|
|
expect(t, called, true)
|
|
|
|
expect(t, ErrWriter.(*bytes.Buffer).String(), "This the format: err1\nThis the format: err2\n")
|
2016-07-24 15:57:51 +00:00
|
|
|
}
|