aboutsummaryrefslogtreecommitdiffstats
path: root/internal/cmdtest/test_cmd.go
diff options
context:
space:
mode:
authorJavier Peletier <jpeletier@users.noreply.github.com>2018-11-20 15:23:43 +0800
committerViktor TrĂ³n <viktor.tron@gmail.com>2018-11-20 15:23:43 +0800
commit7bf7bd2f5088d8abb21daef42b8dfce27b194bc7 (patch)
treeb3cfd89b51b96823b6d1b72e3ff12899f543a68f /internal/cmdtest/test_cmd.go
parentd31f1f4fdb905acb3d7c766683e584bb17f8df0d (diff)
downloaddexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar.gz
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar.bz2
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar.lz
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar.xz
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.tar.zst
dexon-7bf7bd2f5088d8abb21daef42b8dfce27b194bc7.zip
internal/cmdtest: Expose process exit status and errors (#18046)
Diffstat (limited to 'internal/cmdtest/test_cmd.go')
-rw-r--r--internal/cmdtest/test_cmd.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/internal/cmdtest/test_cmd.go b/internal/cmdtest/test_cmd.go
index 20e82ec2a..f8b1b43c0 100644
--- a/internal/cmdtest/test_cmd.go
+++ b/internal/cmdtest/test_cmd.go
@@ -27,6 +27,7 @@ import (
"regexp"
"strings"
"sync"
+ "syscall"
"testing"
"text/template"
"time"
@@ -50,6 +51,8 @@ type TestCmd struct {
stdout *bufio.Reader
stdin io.WriteCloser
stderr *testlogger
+ // Err will contain the process exit error or interrupt signal error
+ Err error
}
// Run exec's the current binary using name as argv[0] which will trigger the
@@ -182,11 +185,25 @@ func (tt *TestCmd) ExpectExit() {
}
func (tt *TestCmd) WaitExit() {
- tt.cmd.Wait()
+ tt.Err = tt.cmd.Wait()
}
func (tt *TestCmd) Interrupt() {
- tt.cmd.Process.Signal(os.Interrupt)
+ tt.Err = tt.cmd.Process.Signal(os.Interrupt)
+}
+
+// ExitStatus exposes the process' OS exit code
+// It will only return a valid value after the process has finished.
+func (tt *TestCmd) ExitStatus() int {
+ if tt.Err != nil {
+ exitErr := tt.Err.(*exec.ExitError)
+ if exitErr != nil {
+ if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
+ return status.ExitStatus()
+ }
+ }
+ }
+ return 0
}
// StderrText returns any stderr output written so far.