github.com/reconquest/executil-go@v0.0.0-20181110204642-1f5c2d67813f/exit_status_test.go (about) 1 package executil 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 "unsafe" 8 9 "golang.org/x/crypto/ssh" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestGetExitStatus_ReturnsExitStatusOfActualError( 15 t *testing.T, 16 ) { 17 test := assert.New(t) 18 19 errExit1 := getCommandWithStdoutAndStderrAndExitStatus1().Run() 20 test.Equal(1, GetExitStatus(errExit1)) 21 22 errExit2 := getCommandWithStdoutAndStderrAndExitStatus2().Run() 23 test.Equal(2, GetExitStatus(errExit2)) 24 } 25 26 func TestGetExitStatus_ReturnsExitStatusOfExecutilErrorWithActualError( 27 t *testing.T, 28 ) { 29 test := assert.New(t) 30 31 _, _, errExit1 := Run(getCommandWithStdoutAndStderrAndExitStatus1()) 32 test.Equal(1, GetExitStatus(errExit1)) 33 34 _, _, errExit2 := Run(getCommandWithStdoutAndStderrAndExitStatus2()) 35 test.Equal(2, GetExitStatus(errExit2)) 36 } 37 38 func TestGetExitStatus_ReturnsZeroForNil(t *testing.T) { 39 test := assert.New(t) 40 41 test.Equal(0, GetExitStatus(nil)) 42 } 43 44 func TestGetExitStatus_ReturnsZeroForExecutilErrorWithNil(t *testing.T) { 45 test := assert.New(t) 46 47 test.Equal(0, GetExitStatus(&Error{RunErr: nil})) 48 } 49 50 func TestGetExitStatus_ReturnsZeroForExecutilErrorWithNonExitError( 51 t *testing.T, 52 ) { 53 test := assert.New(t) 54 55 test.Equal(0, GetExitStatus(&Error{RunErr: errors.New("blah")})) 56 } 57 58 func TestGetExitStatus_ReturnsZeroForNonExitError( 59 t *testing.T, 60 ) { 61 test := assert.New(t) 62 63 test.Equal(0, GetExitStatus(errors.New("blah"))) 64 } 65 66 func TestGetExitStatus_ReturnsExitStatusOfSshExitError( 67 t *testing.T, 68 ) { 69 test := assert.New(t) 70 71 err := new(ssh.ExitError) 72 73 pointer := reflect.Indirect(reflect.ValueOf(err)). 74 FieldByName("Waitmsg").FieldByName("status").UnsafeAddr() 75 76 exitcode := 8 77 *(*int)(unsafe.Pointer(pointer)) = *(*int)(unsafe.Pointer(&exitcode)) 78 79 test.Equal(8, GetExitStatus(err)) 80 }