1
2 # This test is intended to verify that coverage reporting is consistent
3 # between "go test -cover" and "go build -cover" with respect to how
4 # the "main" package is handled. See issue 57169 for details.
5
6 [short] skip
7
8 # Build this program with -cover and run to collect a profile.
9
10 go build -cover -o $WORK/prog.exe .
11
12 # Save off old GOCOVERDIR setting
13 env SAVEGOCOVERDIR=$GOCOVERDIR
14
15 mkdir $WORK/covdata
16 env GOCOVERDIR=$WORK/covdata
17 exec $WORK/prog.exe
18
19 # Restore previous GOCOVERDIR setting
20 env GOCOVERDIR=$SAVEGOCOVERDIR
21
22 # Report percent lines covered.
23 go tool covdata percent -i=$WORK/covdata
24 stdout '\s*mainwithtest\s+coverage:'
25 ! stdout 'main\s+coverage:'
26
27 # Go test -cover should behave the same way.
28 go test -cover .
29 stdout 'ok\s+mainwithtest\s+\S+\s+coverage:'
30 ! stdout 'ok\s+main\s+.*'
31
32
33 -- go.mod --
34 module mainwithtest
35
36 go 1.20
37 -- mymain.go --
38 package main
39
40 func main() {
41 println("hi mom")
42 }
43
44 func Mainer() int {
45 return 42
46 }
47 -- main_test.go --
48 package main
49
50 import "testing"
51
52 func TestCoverage(t *testing.T) {
53 println(Mainer())
54 }
55
View as plain text