Skip to content

Commit 6ac6b82

Browse files
authored
testscript: add Params.ContinueOnError (#192)
This fixes a few issues around the existing `testscript -continue` flag. Notably: - causing `T.Fatal` and `T.FailNow` to return normally meant that some logic inside the testscript package that was expecting the old behaviour would fail to work correctly (for example, a non-existent command would cause a panic) - the logging logic was erroneously assuming that if we got to the end of a script, all sections had passed therefore we could omit the logged messages. We fix the above by making "continue on error" a first class part of `testscript.Params`, so the testscript logic actually knows about it. To test it properly, we need a couple of hacks to make the output predictable. Unfortunately, those hacks are internal only, so it's hard to add similar tests to the `cmd/testscript` command to end-to-end test that, but the existing test should act as sufficient "smoke test" that the continue logic is wired up OK.
1 parent 9701106 commit 6ac6b82

File tree

4 files changed

+231
-80
lines changed

4 files changed

+231
-80
lines changed

cmd/testscript/main.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ func (tr *testRunner) run(runDir, filename string) error {
209209
}
210210

211211
p := testscript.Params{
212-
Dir: runDir,
213-
UpdateScripts: tr.update,
212+
Dir: runDir,
213+
UpdateScripts: tr.update,
214+
ContinueOnError: tr.continueOnError,
214215
}
215216

216217
if _, err := exec.LookPath("go"); err == nil {
@@ -278,8 +279,7 @@ func (tr *testRunner) run(runDir, filename string) error {
278279
}
279280

280281
r := &runT{
281-
continueOnError: tr.continueOnError,
282-
verbose: tr.verbose,
282+
verbose: tr.verbose,
283283
}
284284

285285
func() {
@@ -347,9 +347,8 @@ func renderFilename(filename string) string {
347347

348348
// runT implements testscript.T and is used in the call to testscript.Run
349349
type runT struct {
350-
verbose bool
351-
continueOnError bool
352-
failed int32
350+
verbose bool
351+
failed int32
353352
}
354353

355354
func (r *runT) Skip(is ...interface{}) {
@@ -372,9 +371,7 @@ func (r *runT) Log(is ...interface{}) {
372371

373372
func (r *runT) FailNow() {
374373
atomic.StoreInt32(&r.failed, 1)
375-
if !r.continueOnError {
376-
panic(failedRun)
377-
}
374+
panic(failedRun)
378375
}
379376

380377
func (r *runT) Failed() bool {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# non-verbose, non-continue
2+
! testscript scripts
3+
cmpenv stdout expect-stdout.txt
4+
5+
# verbose
6+
! testscript -v scripts
7+
cmpenv stdout expect-stdout-v.txt
8+
9+
# continue
10+
! testscript -continue scripts
11+
cmpenv stdout expect-stdout-c.txt
12+
13+
# verbose, continue
14+
! testscript -v -continue scripts
15+
cmpenv stdout expect-stdout-vc.txt
16+
17+
-- scripts/testscript.txt --
18+
# comment 1
19+
printargs section1
20+
21+
# comment 2
22+
printargs section2
23+
24+
# comment 3
25+
printargs section3
26+
status 1
27+
28+
# comment 4
29+
printargs section3
30+
31+
# comment 5
32+
printargs section5
33+
status 1
34+
35+
-- expect-stdout.txt --
36+
# comment 1 (0.000s)
37+
# comment 2 (0.000s)
38+
# comment 3 (0.000s)
39+
> printargs section3
40+
[stdout]
41+
["printargs" "section3"]
42+
> status 1
43+
[exit status 1]
44+
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
45+
-- expect-stdout-v.txt --
46+
# comment 1 (0.000s)
47+
> printargs section1
48+
[stdout]
49+
["printargs" "section1"]
50+
# comment 2 (0.000s)
51+
> printargs section2
52+
[stdout]
53+
["printargs" "section2"]
54+
# comment 3 (0.000s)
55+
> printargs section3
56+
[stdout]
57+
["printargs" "section3"]
58+
> status 1
59+
[exit status 1]
60+
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
61+
-- expect-stdout-c.txt --
62+
# comment 1 (0.000s)
63+
# comment 2 (0.000s)
64+
# comment 3 (0.000s)
65+
> printargs section3
66+
[stdout]
67+
["printargs" "section3"]
68+
> status 1
69+
[exit status 1]
70+
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
71+
# comment 4 (0.000s)
72+
> printargs section3
73+
[stdout]
74+
["printargs" "section3"]
75+
# comment 5 (0.000s)
76+
> printargs section5
77+
[stdout]
78+
["printargs" "section5"]
79+
> status 1
80+
[exit status 1]
81+
FAIL: $$WORK${/}scripts${/}testscript.txt:16: unexpected command failure
82+
-- expect-stdout-vc.txt --
83+
# comment 1 (0.000s)
84+
> printargs section1
85+
[stdout]
86+
["printargs" "section1"]
87+
# comment 2 (0.000s)
88+
> printargs section2
89+
[stdout]
90+
["printargs" "section2"]
91+
# comment 3 (0.000s)
92+
> printargs section3
93+
[stdout]
94+
["printargs" "section3"]
95+
> status 1
96+
[exit status 1]
97+
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
98+
# comment 4 (0.000s)
99+
> printargs section3
100+
[stdout]
101+
["printargs" "section3"]
102+
# comment 5 (0.000s)
103+
> printargs section5
104+
[stdout]
105+
["printargs" "section5"]
106+
> status 1
107+
[exit status 1]
108+
FAIL: $$WORK${/}scripts${/}testscript.txt:16: unexpected command failure

0 commit comments

Comments
 (0)