Skip to content

Commit 7bf7bd2

Browse files
jpeletierzelig
authored andcommitted
internal/cmdtest: Expose process exit status and errors (#18046)
1 parent d31f1f4 commit 7bf7bd2

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

internal/cmdtest/test_cmd.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"regexp"
2828
"strings"
2929
"sync"
30+
"syscall"
3031
"testing"
3132
"text/template"
3233
"time"
@@ -50,6 +51,8 @@ type TestCmd struct {
5051
stdout *bufio.Reader
5152
stdin io.WriteCloser
5253
stderr *testlogger
54+
// Err will contain the process exit error or interrupt signal error
55+
Err error
5356
}
5457

5558
// Run exec's the current binary using name as argv[0] which will trigger the
@@ -182,11 +185,25 @@ func (tt *TestCmd) ExpectExit() {
182185
}
183186

184187
func (tt *TestCmd) WaitExit() {
185-
tt.cmd.Wait()
188+
tt.Err = tt.cmd.Wait()
186189
}
187190

188191
func (tt *TestCmd) Interrupt() {
189-
tt.cmd.Process.Signal(os.Interrupt)
192+
tt.Err = tt.cmd.Process.Signal(os.Interrupt)
193+
}
194+
195+
// ExitStatus exposes the process' OS exit code
196+
// It will only return a valid value after the process has finished.
197+
func (tt *TestCmd) ExitStatus() int {
198+
if tt.Err != nil {
199+
exitErr := tt.Err.(*exec.ExitError)
200+
if exitErr != nil {
201+
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
202+
return status.ExitStatus()
203+
}
204+
}
205+
}
206+
return 0
190207
}
191208

192209
// StderrText returns any stderr output written so far.

0 commit comments

Comments
 (0)