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-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) {
|
|
|
|
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-25 16:05:14 +00:00
|
|
|
exitErr := Exit("galactic perimeter breach", 9)
|
|
|
|
err := newMultiError(errors.New("wowsa"), errors.New("egad"), exitErr)
|
2016-05-02 15:25:37 +00:00
|
|
|
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(), "")
|
|
|
|
}
|