| 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 '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. |
| 16 |
go test -vet=off -coverprofile=cover.out -v |
| 17 |
stdout 'PASS' |
| 18 |
stdout 'coverage: 100\.0%' |
| 19 |
! stdout 'malformed coverage' # written by "go test" if cover.out is invalid |
| 20 |
exists cover.out |
| 21 |
|
| 22 |
-- go.mod -- |
| 23 |
module test |
| 24 |
|
| 25 |
go 1.15 |
| 26 |
-- foo.go -- |
| 27 |
package foo |
| 28 |
|
| 29 |
import "os" |
| 30 |
|
| 31 |
func 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 -- |
| 43 |
package foo |
| 44 |
|
| 45 |
import ( |
| 46 |
"os" |
| 47 |
"testing" |
| 48 |
|
| 49 |
"github.com/rogpeppe/go-internal/gotooltest" |
| 50 |
"github.com/rogpeppe/go-internal/testscript" |
| 51 |
) |
| 52 |
|
| 53 |
func TestMain(m *testing.M) { |
| 54 |
// Note that here we still use RunMain, which is the older deprecated API, |
| 55 |
// to sanity check that it works OK. |
| 56 |
os.Exit(testscript.RunMain(m, map[string] func() int{ |
| 57 |
"foo": foo1, |
| 58 |
})) |
| 59 |
} |
| 60 |
|
| 61 |
func TestFoo(t *testing.T) { |
| 62 |
p := testscript.Params{ |
| 63 |
Dir: "scripts", |
| 64 |
} |
| 65 |
if err := gotooltest.Setup(&p); err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
testscript.Run(t, p) |
| 69 |
} |
| 70 |
-- scripts/exec.txt -- |
| 71 |
># Note that foo always fails, to prevent "go build" from doing anything. |
| 72 |
> |
| 73 |
># Running the command directly; trigger the first path. |
| 74 |
>! foo 1 |
| 75 |
> |
| 76 |
># Running the command via exec; trigger the second path. |
| 77 |
>! exec foo 2 |
| 78 |
> |
| 79 |
># Running the command indirectly, via toolexec; trigger the third path. |
| 80 |
>! go build -a -toolexec=foo runtime |