Skip to content

Commit dc66b32

Browse files
authored
testscript: print background command output on failure (#148)
When a script has failed, the output of the commands running in the background can be useful to see, so display it then and also in verbose mode.
1 parent 115ce09 commit dc66b32

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

cmd/testscript/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os/exec"
1414
"path/filepath"
1515
"strings"
16+
"sync/atomic"
1617

1718
"github.com/rogpeppe/go-internal/goproxytest"
1819
"github.com/rogpeppe/go-internal/gotooltest"
@@ -335,6 +336,7 @@ func renderFilename(filename string) string {
335336
// runT implements testscript.T and is used in the call to testscript.Run
336337
type runT struct {
337338
verbose bool
339+
failed int32
338340
}
339341

340342
func (r runT) Skip(is ...interface{}) {
@@ -356,9 +358,14 @@ func (r runT) Log(is ...interface{}) {
356358
}
357359

358360
func (r runT) FailNow() {
361+
atomic.StoreInt32(&r.failed, 1)
359362
panic(failedRun)
360363
}
361364

365+
func (r runT) Failed() bool {
366+
return atomic.LoadInt32(&r.failed) != 0
367+
}
368+
362369
func (r runT) Run(n string, f func(t testscript.T)) {
363370
// For now we we don't top/tail the run of a subtest. We are currently only
364371
// running a single script in a testscript instance, which means that we

testscript/cmd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ func (ts *TestScript) cmdWait(neg bool, args []string) {
455455
if len(args) > 0 {
456456
ts.Fatalf("usage: wait")
457457
}
458+
ts.waitBackground(true, neg)
459+
}
458460

461+
func (ts *TestScript) waitBackground(checkStatus bool, neg bool) {
459462
var stdouts, stderrs []string
460463
for _, bg := range ts.background {
461464
<-bg.wait
@@ -475,6 +478,9 @@ func (ts *TestScript) cmdWait(neg bool, args []string) {
475478
stderrs = append(stderrs, cmdStderr)
476479
}
477480

481+
if !checkStatus {
482+
continue
483+
}
478484
if bg.cmd.ProcessState.Success() {
479485
if bg.neg {
480486
ts.Fatalf("unexpected command success")

testscript/testscript.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ type T interface {
174174
Verbose() bool
175175
}
176176

177+
// TFailed holds optional extra methods implemented on T.
178+
// It's defined as a separate type for backward compatibility reasons.
179+
type TFailed interface {
180+
Failed() bool
181+
}
182+
177183
type tshim struct {
178184
*testing.T
179185
}
@@ -387,10 +393,16 @@ func (ts *TestScript) run() {
387393
for _, bg := range ts.background {
388394
interruptProcess(bg.cmd.Process)
389395
}
390-
for _, bg := range ts.background {
391-
<-bg.wait
396+
if ts.t.Verbose() || hasFailed(ts.t) {
397+
// In verbose mode or on test failure, we want to see what happened in the background
398+
// processes too.
399+
ts.waitBackground(false, false)
400+
} else {
401+
for _, bg := range ts.background {
402+
<-bg.wait
403+
}
404+
ts.background = nil
392405
}
393-
ts.background = nil
394406

395407
markTime()
396408
// Flush testScript log to testing.T log.
@@ -516,6 +528,13 @@ Script:
516528
}
517529
}
518530

531+
func hasFailed(t T) bool {
532+
if t, ok := t.(TFailed); ok {
533+
return t.Failed()
534+
}
535+
return false
536+
}
537+
519538
func (ts *TestScript) applyScriptUpdates() {
520539
if len(ts.scriptUpdates) == 0 {
521540
return

testscript/testscript_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ type fakeT struct {
389389
log bytes.Buffer
390390
failMsgs []string
391391
verbose bool
392+
failed bool
392393
}
393394

394395
var errAbort = errors.New("abort test")
@@ -398,6 +399,7 @@ func (t *fakeT) Skip(args ...interface{}) {
398399
}
399400

400401
func (t *fakeT) Fatal(args ...interface{}) {
402+
t.failed = true
401403
t.failMsgs = append(t.failMsgs, fmt.Sprint(args...))
402404
panic(errAbort)
403405
}
@@ -419,3 +421,7 @@ func (t *fakeT) Run(name string, f func(T)) {
419421
func (t *fakeT) Verbose() bool {
420422
return t.verbose
421423
}
424+
425+
func (t *fakeT) Failed() bool {
426+
return t.failed
427+
}

0 commit comments

Comments
 (0)