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"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleExitCoder_nil(t *testing.T) {
|
|
|
|
exitCode := 0
|
|
|
|
called := false
|
|
|
|
|
|
|
|
OsExiter = func(rc int) {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-05-02 15:25:37 +00:00
|
|
|
|
2016-05-03 10:54:05 +00:00
|
|
|
HandleExitCoder(NewExitError("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) {
|
|
|
|
exitCode = rc
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
|
2016-07-24 15:57:51 +00:00
|
|
|
defer func() { OsExiter = fakeOsExiter }()
|
2016-05-02 15:25:37 +00:00
|
|
|
|
2016-05-03 10:54:05 +00:00
|
|
|
exitErr := NewExitError("galactic perimeter breach", 9)
|
2016-05-02 15:25:37 +00:00
|
|
|
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr)
|
|
|
|
HandleExitCoder(err)
|
|
|
|
|
|
|
|
expect(t, exitCode, 9)
|
|
|
|
expect(t, called, true)
|
|
|
|
}
|
2016-07-24 15:57:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-07-24 16:09:05 +00:00
|
|
|
expect(t, exitCode, 1)
|
|
|
|
expect(t, called, true)
|
2016-07-24 15:57:51 +00:00
|
|
|
expect(t, ErrWriter.(*bytes.Buffer).String(), "")
|
|
|
|
}
|