1 # This test checks more of the "go build -cover" functionality,
2 # specifically which packages get selected when building.
3
4 [short] skip
5
6 # Skip if new coverage is not enabled.
7 [!GOEXPERIMENT:coverageredesign] skip
8
9 #-------------------------------------------
10
11 # Build for coverage.
12 go build -mod=mod -o $WORK/modex.exe -cover mod.example/main
13
14 # Save off old GOCOVERDIR setting
15 env SAVEGOCOVERDIR=$GOCOVERDIR
16
17 # Execute.
18 mkdir $WORK/covdata
19 env GOCOVERDIR=$WORK/covdata
20 exec $WORK/modex.exe
21
22 # Restore previous GOCOVERDIR setting
23 env GOCOVERDIR=$SAVEGOCOVERDIR
24
25 # Examine the result.
26 go tool covdata percent -i=$WORK/covdata
27 stdout 'coverage: 100.0% of statements'
28
29 # By default we want to see packages resident in the module covered,
30 # but not dependencies.
31 go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
32 grep 'mode: set' $WORK/covdata/out.txt
33 grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
34 grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
35 ! grep 'rsc.io' $WORK/covdata/out.txt
36
37 rm $WORK/covdata
38 rm $WORK/modex.exe
39
40 #-------------------------------------------
41
42 # Repeat the build but with -coverpkg=all
43
44 go build -mod=mod -coverpkg=all -o $WORK/modex.exe -cover mod.example/main
45
46 # Execute.
47 mkdir $WORK/covdata
48 env GOCOVERDIR=$WORK/covdata
49 exec $WORK/modex.exe
50
51 # Restore previous GOCOVERDIR setting
52 env GOCOVERDIR=$SAVEGOCOVERDIR
53
54 # Examine the result.
55 go tool covdata percent -i=$WORK/covdata
56 stdout 'coverage:.*[1-9][0-9.]+%'
57
58 # The whole enchilada.
59 go tool covdata textfmt -i=$WORK/covdata -o=$WORK/covdata/out.txt
60 grep 'mode: set' $WORK/covdata/out.txt
61 grep 'mod.example/main/main.go:' $WORK/covdata/out.txt
62 grep 'mod.example/sub/sub.go:' $WORK/covdata/out.txt
63 grep 'rsc.io' $WORK/covdata/out.txt
64 grep 'bufio/bufio.go:' $WORK/covdata/out.txt
65
66 # Use the covdata tool to select a specific set of module paths
67 mkdir $WORK/covdata2
68 go tool covdata merge -pkg=rsc.io/quote -i=$WORK/covdata -o=$WORK/covdata2
69
70 # Examine the result.
71 go tool covdata percent -i=$WORK/covdata2
72 stdout 'coverage:.*[1-9][0-9.]+%'
73
74 # Check for expected packages + check that we don't see things from stdlib.
75 go tool covdata textfmt -i=$WORK/covdata2 -o=$WORK/covdata2/out.txt
76 grep 'mode: set' $WORK/covdata2/out.txt
77 ! grep 'mod.example/main/main.go:' $WORK/covdata2/out.txt
78 ! grep 'mod.example/sub/sub.go:' $WORK/covdata2/out.txt
79 grep 'rsc.io' $WORK/covdata2/out.txt
80 ! grep 'bufio/bufio.go:' $WORK/covdata2/out.txt
81
82 #-------------------------------------------
83 # end of test cmds, start of harness and related files.
84
85 -- go.mod --
86 module mod.example
87
88 go 1.20
89
90 require rsc.io/quote/v3 v3.0.0
91
92 -- main/main.go --
93 package main
94
95 import (
96 "fmt"
97 "mod.example/sub"
98
99 "rsc.io/quote"
100 )
101
102 func main() {
103 fmt.Println(quote.Go(), sub.F())
104 }
105
106 -- sub/sub.go --
107
108 package sub
109
110 func F() int {
111 return 42
112 }
113
114
115
View as plain text