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