...
1# This test checks basic "go build -cover" functionality.
2
3[short] skip
4
5# Hard-wire new coverage for this test.
6env GOEXPERIMENT=coverageredesign
7
8# Build for coverage.
9go build -gcflags=-m -o example.exe -cover example/main &
10[race] go build -o examplewithrace.exe -race -cover example/main &
11wait
12
13# First execute without GOCOVERDIR set...
14env GOCOVERDIR=
15exec ./example.exe normal
16stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
17
18# ... then with GOCOVERDIR set.
19env GOCOVERDIR=data/normal
20exec ./example.exe normal
21! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
22go tool covdata percent -i=data/normal
23stdout 'coverage:.*[1-9][0-9.]+%'
24
25# Program makes a direct call to os.Exit(0).
26env GOCOVERDIR=data/goodexit
27exec ./example.exe goodexit
28! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
29go tool covdata percent -i=data/goodexit
30stdout 'coverage:.*[1-9][0-9.]+%'
31
32# Program makes a direct call to os.Exit(1).
33env GOCOVERDIR=data/badexit
34! exec ./example.exe badexit
35! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
36go tool covdata percent -i=data/badexit
37stdout 'coverage:.*[1-9][0-9.]+%'
38
39# Program invokes panic.
40env GOCOVERDIR=data/panic
41! exec ./example.exe panic
42! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
43go tool covdata percent -i=data/panic
44stdout 'coverage:.*[0-9.]+%'
45
46# Skip remainder if no race detector support.
47[!race] skip
48
49env GOCOVERDIR=data2/normal
50exec ./examplewithrace.exe normal
51! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
52go tool covdata percent -i=data2/normal
53stdout 'coverage:.*[1-9][0-9.]+%'
54
55# Program makes a direct call to os.Exit(0).
56env GOCOVERDIR=data2/goodexit
57exec ./examplewithrace.exe goodexit
58! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
59go tool covdata percent -i=data2/goodexit
60stdout 'coverage:.*[1-9][0-9.]+%'
61
62# Program makes a direct call to os.Exit(1).
63env GOCOVERDIR=data2/badexit
64! exec ./examplewithrace.exe badexit
65! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
66go tool covdata percent -i=data2/badexit
67stdout 'coverage:.*[1-9][0-9.]+%'
68
69# Program invokes panic.
70env GOCOVERDIR=data2/panic
71! exec ./examplewithrace.exe panic
72! stderr '^warning: GOCOVERDIR not set, no coverage data emitted'
73go tool covdata percent -i=data2/panic
74stdout 'coverage:.*[0-9.]+%'
75
76# end of test cmds, start of harness and related files.
77
78-- go.mod --
79module example
80
81go 1.18
82
83-- main/example.go --
84package main
85
86import "example/sub"
87
88func main() {
89 sub.S()
90}
91
92-- sub/sub.go --
93
94package sub
95
96import "os"
97
98func S() {
99 switch os.Args[1] {
100 case "normal":
101 println("hi")
102 case "goodexit":
103 os.Exit(0)
104 case "badexit":
105 os.Exit(1)
106 case "panic":
107 panic("something bad happened")
108 }
109}
110
111-- data/README.txt --
112
113Just a location where we can write coverage profiles.
114
115-- data/normal/f.txt --
116
117X
118
119-- data/goodexit/f.txt --
120
121X
122
123-- data/badexit/f.txt --
124
125X
126
127-- data/panic/f.txt --
128
129X
130
131-- data2/README.txt --
132
133Just a location where we can write coverage profiles.
134
135-- data2/normal/f.txt --
136
137X
138
139-- data2/goodexit/f.txt --
140
141X
142
143-- data2/badexit/f.txt --
144
145X
146
147-- data2/panic/f.txt --
148
149X
View as plain text