Skip to content

Commit dc4b495

Browse files
committed
testscript: merge coverage from all test binary executions
First, consolidate how top-level commands registered via RunMain are executed. Before, they could only be run directly, such as "foo <args>". This worked because the command would run the test binary with a special TESTSCRIPT_COMMAND env variable, telling the sub-process what command to run. The unfortunate side effect was that the commands only worked when called directly. They wouldn't work when called indirectly, such as "exec foo" or "go build -toolexec=foo". To fix that inconsistency, we now set up a directory in $PATH with all the commands as copies of the test binary. The test binary sub-process knows what command it should execute thanks to os.Args[0]. This also lets us get rid of the TESTSCRIPT_COMMAND dance. Second, make all top-level command executions write coverage profiles if the -coverprofile test flag was used. Similar to the case before, this only used to work for direct command executions, not indirect ones. Now they all get merged into the final coverage profile. This is accomplished by having them all write coverage profiles under a shared directory. Once all scripts have run, the parent process walks that directory and merges all profiles found in it. Third, add a test that ensures both of the refactors above work well. It lives under gotooltest, since it runs the real "go test -coverprofile". It checks all three ways mentioned above to run a top-level command, as well as checking that all three count towards the total coverage. Note that some tests needed to be amended to keep working after the refactor. For example, some tests used a custom "echo" command as well as the system's "exec echo". Since now both of those would execute the testscript command, rename that command to fprintargs, which is also clearer and less ambiguous. Similarly, dropgofrompath had to be used in one more test, and had to be adapted to actually do the intended thing on Windows rather than just emptying the entire PATH variable. Also swap Go's 1.16beta1 for 1.16rc1 in CI, since the former is now failing due to a bug in setup-go.
1 parent d773b4c commit dc4b495

File tree

14 files changed

+286
-97
lines changed

14 files changed

+286
-97
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
go-version: [1.14.x, 1.15.x, 1.16.0-beta1]
14+
go-version: [1.14.x, 1.15.x, 1.16.0-rc1]
1515
os: [ubuntu-latest, macos-latest, windows-latest]
1616
runs-on: ${{ matrix.os }}
1717
steps:

cmd/testscript/main_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func dropgofrompath(ts *testscript.TestScript, neg bool, args []string) {
7070
var newPath []string
7171
for _, d := range filepath.SplitList(ts.Getenv("PATH")) {
7272
getenv := func(k string) string {
73-
if k == "PATH" {
73+
// Note that Windows and Plan9 use lowercase "path".
74+
if strings.ToUpper(k) == "PATH" {
7475
return d
7576
}
7677
return ts.Getenv(k)

cmd/testscript/testdata/nogo.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# should support skip
22
unquote file.txt
33

4-
env PATH=
4+
# We can't just set PATH to empty because we need the part of it that
5+
# contains the command names, so use a special builtin instead.
6+
dropgofrompath
7+
58
! testscript -v file.txt
69
stdout 'unknown command "go"'
710
stderr 'error running file.txt in'

gotooltest/script_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package gotooltest_test
66

77
import (
8+
"os"
9+
"path/filepath"
810
"testing"
911

1012
"github.com/rogpeppe/go-internal/gotooltest"
@@ -14,7 +16,19 @@ import (
1416
func TestSimple(t *testing.T) {
1517
p := testscript.Params{
1618
Dir: "testdata",
19+
Setup: func(env *testscript.Env) error {
20+
// cover.txt will need testscript as a dependency.
21+
// Tell it where our module is, via an absolute path.
22+
wd, err := os.Getwd()
23+
if err != nil {
24+
return err
25+
}
26+
modPath := filepath.Dir(wd)
27+
env.Setenv("GOINTERNAL_MODULE", modPath)
28+
return nil
29+
},
1730
}
31+
1832
if err := gotooltest.Setup(&p); err != nil {
1933
t.Fatal(err)
2034
}

gotooltest/testdata/cover.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
unquote scripts/exec.txt
2+
3+
# The module uses testscript itself.
4+
# Use the checked out module, based on where the test binary ran.
5+
go mod edit -replace=github.com/rogpeppe/go-internal=${GOINTERNAL_MODULE}
6+
go mod tidy
7+
8+
# First, a 'go test' run without coverage.
9+
go test -vet=off
10+
stdout 'PASS'
11+
! stdout 'total coverage'
12+
13+
# Then, a 'go test' run with -coverprofile.
14+
# Assuming testscript works well, this results in the basic coverage being 0%,
15+
# since the test binary does not directly run any non-test code.
16+
# The total coverage after merging profiles should end up being 100%,
17+
# as long as all three sub-profiles are accounted for.
18+
# Marking all printlns as covered requires all edge cases to work well.
19+
go test -vet=off -coverprofile=cover.out -v
20+
stdout 'PASS'
21+
stdout '^coverage: 0\.0%'
22+
stdout '^total coverage: 100\.0%'
23+
! stdout 'malformed coverage' # written by "go test" if cover.out is invalid
24+
exists cover.out
25+
26+
-- go.mod --
27+
module test
28+
29+
go 1.15
30+
-- foo.go --
31+
package foo
32+
33+
import "os"
34+
35+
func foo1() int {
36+
switch os.Args[1] {
37+
case "1":
38+
println("first path")
39+
case "2":
40+
println("second path")
41+
default:
42+
println("third path")
43+
}
44+
return 1
45+
}
46+
-- foo_test.go --
47+
package foo
48+
49+
import (
50+
"os"
51+
"testing"
52+
53+
"github.com/rogpeppe/go-internal/gotooltest"
54+
"github.com/rogpeppe/go-internal/testscript"
55+
)
56+
57+
func TestMain(m *testing.M) {
58+
os.Exit(testscript.RunMain(m, map[string] func() int{
59+
"foo": foo1,
60+
}))
61+
}
62+
63+
func TestFoo(t *testing.T) {
64+
p := testscript.Params{
65+
Dir: "scripts",
66+
}
67+
if err := gotooltest.Setup(&p); err != nil {
68+
t.Fatal(err)
69+
}
70+
testscript.Run(t, p)
71+
}
72+
-- scripts/exec.txt --
73+
># Note that foo always fails, to prevent "go build" from doing anything.
74+
>
75+
># Running the command directly; trigger the first path.
76+
>! foo 1
77+
>
78+
># Running the command via exec; trigger the second path.
79+
>! exec foo 2
80+
>
81+
># Running the command indirectly, via toolexec; trigger the third path.
82+
>! go build -a -toolexec=foo runtime

testscript/cover.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"bufio"
99
"fmt"
1010
"io"
11-
"log"
1211
"os"
12+
"path/filepath"
1313
"regexp"
1414
"strconv"
1515
"strings"
@@ -22,8 +22,13 @@ import (
2222
// mergeCoverProfile merges the coverage information in f into
2323
// cover. It assumes that the coverage information in f is
2424
// always produced from the same binary for every call.
25-
func mergeCoverProfile(cover *testing.Cover, r io.Reader) error {
26-
scanner, err := newProfileScanner(r)
25+
func mergeCoverProfile(cover *testing.Cover, path string) error {
26+
f, err := os.Open(path)
27+
if err != nil {
28+
return err
29+
}
30+
defer f.Close()
31+
scanner, err := newProfileScanner(f)
2732
if err != nil {
2833
return errors.Wrap(err)
2934
}
@@ -83,45 +88,45 @@ func mergeCoverProfile(cover *testing.Cover, r io.Reader) error {
8388
return nil
8489
}
8590

86-
var (
87-
coverChan chan *os.File
88-
coverDone chan testing.Cover
89-
)
90-
91-
func goCoverProfileMerge() {
92-
if coverChan != nil {
93-
panic("RunMain called twice!")
94-
}
95-
coverChan = make(chan *os.File)
96-
coverDone = make(chan testing.Cover)
97-
go mergeCoverProfiles()
98-
}
99-
100-
func mergeCoverProfiles() {
91+
func finalizeCoverProfile(dir string) error {
92+
// Merge all the coverage profiles written by test binary sub-processes,
93+
// such as those generated by executions of commands.
10194
var cover testing.Cover
102-
for f := range coverChan {
103-
if err := mergeCoverProfile(&cover, f); err != nil {
104-
log.Printf("cannot merge coverage profile from %v: %v", f.Name(), err)
95+
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
96+
if err != nil {
97+
return err
98+
}
99+
if !info.Mode().IsRegular() {
100+
return nil
101+
}
102+
if err := mergeCoverProfile(&cover, path); err != nil {
103+
return fmt.Errorf("cannot merge coverage profile from %v: %v", path, err)
105104
}
106-
f.Close()
107-
os.Remove(f.Name())
105+
return nil
106+
}); err != nil {
107+
return errors.Wrap(err)
108+
}
109+
if err := os.RemoveAll(dir); err != nil {
110+
// The RemoveAll seems to fail very rarely, with messages like
111+
// "directory not empty". It's unclear why.
112+
// For now, if it happens again, try to print a bit more info.
113+
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
114+
if err == nil && !info.IsDir() {
115+
fmt.Fprintln(os.Stderr, "non-directory found after RemoveAll:", path)
116+
}
117+
return nil
118+
})
119+
return errors.Wrap(err)
108120
}
109-
coverDone <- cover
110-
}
111121

112-
func finalizeCoverProfile() error {
122+
// We need to include our own top-level coverage profile too.
113123
cprof := coverProfile()
114-
if cprof == "" {
115-
return nil
124+
if err := mergeCoverProfile(&cover, cprof); err != nil {
125+
return fmt.Errorf("cannot merge coverage profile from %v: %v", cprof, err)
116126
}
117-
f, err := os.Open(cprof)
118-
if err != nil {
119-
return errors.Notef(err, nil, "cannot open existing cover profile")
120-
}
121-
coverChan <- f
122-
close(coverChan)
123-
cover := <-coverDone
124-
f, err = os.Create(cprof)
127+
128+
// Finally, write the resulting merged profile.
129+
f, err := os.Create(cprof)
125130
if err != nil {
126131
return errors.Notef(err, nil, "cannot create cover profile")
127132
}

0 commit comments

Comments
 (0)