Skip to content

Commit f0583b8

Browse files
committed
testscript: remove our code coverage mechanism thanks to Go 1.20
Our code was a fairly hacky version of what Go 1.20 does for us, since we had to externally reach into the testing internals to do the right thing before and after each program execution. With Go 1.20, all we actually need to do is ensure that the GOCOVERDIR environment variable is forwarded properly. With that, test binaries will know how to produce multiple coverage profiles, and "go test" will know how to collect and merge them. We could keep our hacky workaround for the sake of "deep" coverage information for Go 1.19, but that doesn't seem worthwhile. The old mechanism caused test flakes like #130, is incompatible with Go 1.20, and is overall worse than what Go 1.20 can do. If a user wants code coverage with the latest testscript version, they can use Go 1.20. If they are stuck on Go 1.19 and need code coverage, I imagine they can also stick to a slightly older testscript version. On a main package, the old testscript with Go 1.19 reports: PASS coverage: 8.0% of statements total coverage: 90.1% of statements ok mvdan.cc/sh/v3/cmd/shfmt 0.063s The new testscript with Go 1.20 reports: PASS mvdan.cc/sh/v3/cmd/shfmt coverage: 90.1% of statements ok mvdan.cc/sh/v3/cmd/shfmt 0.047s Fixes #130, as the RemoveAll call is no longer present. Fixes #161, as the API is now deprecated. Fixes #199, as "go test -coverprofile" now works on Go 1.20.
1 parent e3815af commit f0583b8

File tree

4 files changed

+11
-424
lines changed

4 files changed

+11
-424
lines changed

gotooltest/testdata/cover.txt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
unquote scripts/exec.txt
22

3-
[darwin] skip 'Pending a fix for github.com/rogpeppe/go-internal/issues/130'
4-
53
# The module uses testscript itself.
64
# Use the checked out module, based on where the test binary ran.
75
go mod edit -replace=github.com/rogpeppe/go-internal=${GOINTERNAL_MODULE}
@@ -10,18 +8,17 @@ go mod tidy
108
# First, a 'go test' run without coverage.
119
go test -vet=off
1210
stdout 'PASS'
13-
! stdout 'total coverage'
11+
! stdout 'coverage'
1412

1513
# Then, a 'go test' run with -coverprofile.
16-
# Assuming testscript works well, this results in the basic coverage being 0%,
17-
# since the test binary does not directly run any non-test code.
18-
# The total coverage after merging profiles should end up being 100%,
19-
# as long as all three sub-profiles are accounted for.
14+
# The total coverage after merging profiles should end up being 100%.
2015
# Marking all printlns as covered requires all edge cases to work well.
16+
# Go 1.20 learned to produce and merge multiple coverage profiles,
17+
# so versions before then report a shallow 0% coverage.
2118
go test -vet=off -coverprofile=cover.out -v
2219
stdout 'PASS'
23-
stdout '^coverage: 0\.0%'
24-
stdout '^total coverage: 100\.0%'
20+
[go1.20] stdout 'coverage: 100\.0%'
21+
[!go1.20] stdout 'coverage: 0\.0%'
2522
! stdout 'malformed coverage' # written by "go test" if cover.out is invalid
2623
exists cover.out
2724

testscript/cover.go

Lines changed: 0 additions & 280 deletions
This file was deleted.

0 commit comments

Comments
 (0)