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