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