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