Skip to content

Commit

Permalink
testscript: add Params.ContinueOnError (#192)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rogpeppe authored Jan 9, 2023
1 parent 9701106 commit 6ac6b82
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 80 deletions.
17 changes: 7 additions & 10 deletions cmd/testscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ func (tr *testRunner) run(runDir, filename string) error {
}

p := testscript.Params{
Dir: runDir,
UpdateScripts: tr.update,
Dir: runDir,
UpdateScripts: tr.update,
ContinueOnError: tr.continueOnError,
}

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

r := &runT{
continueOnError: tr.continueOnError,
verbose: tr.verbose,
verbose: tr.verbose,
}

func() {
Expand Down Expand Up @@ -347,9 +347,8 @@ func renderFilename(filename string) string {

// runT implements testscript.T and is used in the call to testscript.Run
type runT struct {
verbose bool
continueOnError bool
failed int32
verbose bool
failed int32
}

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

func (r *runT) FailNow() {
atomic.StoreInt32(&r.failed, 1)
if !r.continueOnError {
panic(failedRun)
}
panic(failedRun)
}

func (r *runT) Failed() bool {
Expand Down
108 changes: 108 additions & 0 deletions testscript/testdata/testscript_logging.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# non-verbose, non-continue
! testscript scripts
cmpenv stdout expect-stdout.txt

# verbose
! testscript -v scripts
cmpenv stdout expect-stdout-v.txt

# continue
! testscript -continue scripts
cmpenv stdout expect-stdout-c.txt

# verbose, continue
! testscript -v -continue scripts
cmpenv stdout expect-stdout-vc.txt

-- scripts/testscript.txt --
# comment 1
printargs section1

# comment 2
printargs section2

# comment 3
printargs section3
status 1

# comment 4
printargs section3

# comment 5
printargs section5
status 1

-- expect-stdout.txt --
# comment 1 (0.000s)
# comment 2 (0.000s)
# comment 3 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
-- expect-stdout-v.txt --
# comment 1 (0.000s)
> printargs section1
[stdout]
["printargs" "section1"]
# comment 2 (0.000s)
> printargs section2
[stdout]
["printargs" "section2"]
# comment 3 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
-- expect-stdout-c.txt --
# comment 1 (0.000s)
# comment 2 (0.000s)
# comment 3 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
# comment 4 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
# comment 5 (0.000s)
> printargs section5
[stdout]
["printargs" "section5"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:16: unexpected command failure
-- expect-stdout-vc.txt --
# comment 1 (0.000s)
> printargs section1
[stdout]
["printargs" "section1"]
# comment 2 (0.000s)
> printargs section2
[stdout]
["printargs" "section2"]
# comment 3 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:9: unexpected command failure
# comment 4 (0.000s)
> printargs section3
[stdout]
["printargs" "section3"]
# comment 5 (0.000s)
> printargs section5
[stdout]
["printargs" "section5"]
> status 1
[exit status 1]
FAIL: $$WORK${/}scripts${/}testscript.txt:16: unexpected command failure
Loading

0 comments on commit 6ac6b82

Please sign in to comment.