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