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