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