Source file
src/testing/panic_test.go
1
2
3
4
5 package testing_test
6
7 import (
8 "flag"
9 "fmt"
10 "internal/testenv"
11 "os"
12 "os/exec"
13 "regexp"
14 "runtime"
15 "strings"
16 "testing"
17 )
18
19 var testPanicTest = flag.String("test_panic_test", "", "TestPanic: indicates which test should panic")
20 var testPanicParallel = flag.Bool("test_panic_parallel", false, "TestPanic: run subtests in parallel")
21 var testPanicCleanup = flag.Bool("test_panic_cleanup", false, "TestPanic: indicates whether test should call Cleanup")
22 var testPanicCleanupPanic = flag.String("test_panic_cleanup_panic", "", "TestPanic: indicate whether test should call Cleanup function that panics")
23
24 func TestPanic(t *testing.T) {
25 testenv.MustHaveExec(t)
26
27 testCases := []struct {
28 desc string
29 flags []string
30 want string
31 }{{
32 desc: "root test panics",
33 flags: []string{"-test_panic_test=TestPanicHelper"},
34 want: `
35 --- FAIL: TestPanicHelper (N.NNs)
36 panic_test.go:NNN: TestPanicHelper
37 TestPanicHelper
38 `,
39 }, {
40 desc: "subtest panics",
41 flags: []string{"-test_panic_test=TestPanicHelper/1"},
42 want: `
43 --- FAIL: TestPanicHelper (N.NNs)
44 panic_test.go:NNN: TestPanicHelper
45 TestPanicHelper
46 --- FAIL: TestPanicHelper/1 (N.NNs)
47 panic_test.go:NNN: TestPanicHelper/1
48 TestPanicHelper/1
49 `,
50 }, {
51 desc: "subtest panics with cleanup",
52 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup"},
53 want: `
54 ran inner cleanup 1
55 ran middle cleanup 1
56 ran outer cleanup
57 --- FAIL: TestPanicHelper (N.NNs)
58 panic_test.go:NNN: TestPanicHelper
59 TestPanicHelper
60 --- FAIL: TestPanicHelper/1 (N.NNs)
61 panic_test.go:NNN: TestPanicHelper/1
62 TestPanicHelper/1
63 `,
64 }, {
65 desc: "subtest panics with outer cleanup panic",
66 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=outer"},
67 want: `
68 ran inner cleanup 1
69 ran middle cleanup 1
70 ran outer cleanup
71 --- FAIL: TestPanicHelper (N.NNs)
72 panic_test.go:NNN: TestPanicHelper
73 TestPanicHelper
74 `,
75 }, {
76 desc: "subtest panics with middle cleanup panic",
77 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=middle"},
78 want: `
79 ran inner cleanup 1
80 ran middle cleanup 1
81 ran outer cleanup
82 --- FAIL: TestPanicHelper (N.NNs)
83 panic_test.go:NNN: TestPanicHelper
84 TestPanicHelper
85 --- FAIL: TestPanicHelper/1 (N.NNs)
86 panic_test.go:NNN: TestPanicHelper/1
87 TestPanicHelper/1
88 `,
89 }, {
90 desc: "subtest panics with inner cleanup panic",
91 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=inner"},
92 want: `
93 ran inner cleanup 1
94 ran middle cleanup 1
95 ran outer cleanup
96 --- FAIL: TestPanicHelper (N.NNs)
97 panic_test.go:NNN: TestPanicHelper
98 TestPanicHelper
99 --- FAIL: TestPanicHelper/1 (N.NNs)
100 panic_test.go:NNN: TestPanicHelper/1
101 TestPanicHelper/1
102 `,
103 }, {
104 desc: "parallel subtest panics with cleanup",
105 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_parallel"},
106 want: `
107 ran inner cleanup 1
108 ran middle cleanup 1
109 ran outer cleanup
110 --- FAIL: TestPanicHelper (N.NNs)
111 panic_test.go:NNN: TestPanicHelper
112 TestPanicHelper
113 --- FAIL: TestPanicHelper/1 (N.NNs)
114 panic_test.go:NNN: TestPanicHelper/1
115 TestPanicHelper/1
116 `,
117 }, {
118 desc: "parallel subtest panics with outer cleanup panic",
119 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=outer", "-test_panic_parallel"},
120 want: `
121 ran inner cleanup 1
122 ran middle cleanup 1
123 ran outer cleanup
124 --- FAIL: TestPanicHelper (N.NNs)
125 panic_test.go:NNN: TestPanicHelper
126 TestPanicHelper
127 `,
128 }, {
129 desc: "parallel subtest panics with middle cleanup panic",
130 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=middle", "-test_panic_parallel"},
131 want: `
132 ran inner cleanup 1
133 ran middle cleanup 1
134 ran outer cleanup
135 --- FAIL: TestPanicHelper (N.NNs)
136 panic_test.go:NNN: TestPanicHelper
137 TestPanicHelper
138 --- FAIL: TestPanicHelper/1 (N.NNs)
139 panic_test.go:NNN: TestPanicHelper/1
140 TestPanicHelper/1
141 `,
142 }, {
143 desc: "parallel subtest panics with inner cleanup panic",
144 flags: []string{"-test_panic_test=TestPanicHelper/1", "-test_panic_cleanup", "-test_panic_cleanup_panic=inner", "-test_panic_parallel"},
145 want: `
146 ran inner cleanup 1
147 ran middle cleanup 1
148 ran outer cleanup
149 --- FAIL: TestPanicHelper (N.NNs)
150 panic_test.go:NNN: TestPanicHelper
151 TestPanicHelper
152 --- FAIL: TestPanicHelper/1 (N.NNs)
153 panic_test.go:NNN: TestPanicHelper/1
154 TestPanicHelper/1
155 `,
156 }}
157 for _, tc := range testCases {
158 t.Run(tc.desc, func(t *testing.T) {
159 cmd := exec.Command(testenv.Executable(t), "-test.run=^TestPanicHelper$")
160 cmd.Args = append(cmd.Args, tc.flags...)
161 cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
162 b, _ := cmd.CombinedOutput()
163 got := string(b)
164 want := strings.TrimSpace(tc.want)
165 re := makeRegexp(want)
166 if ok, err := regexp.MatchString(re, got); !ok || err != nil {
167 t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want)
168 }
169 })
170 }
171 }
172
173 func makeRegexp(s string) string {
174 s = regexp.QuoteMeta(s)
175 s = strings.ReplaceAll(s, ":NNN:", `:\d+:`)
176 s = strings.ReplaceAll(s, "N\\.NNs", `\d*\.\d*s`)
177 return s
178 }
179
180 func TestPanicHelper(t *testing.T) {
181 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
182 return
183 }
184 t.Log(t.Name())
185 t.Output().Write([]byte(t.Name()))
186 if t.Name() == *testPanicTest {
187 panic("panic")
188 }
189 switch *testPanicCleanupPanic {
190 case "", "outer", "middle", "inner":
191 default:
192 t.Fatalf("bad -test_panic_cleanup_panic: %s", *testPanicCleanupPanic)
193 }
194 t.Cleanup(func() {
195 fmt.Println("ran outer cleanup")
196 if *testPanicCleanupPanic == "outer" {
197 panic("outer cleanup")
198 }
199 })
200 for i := 0; i < 3; i++ {
201 t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
202 chosen := t.Name() == *testPanicTest
203 if chosen && *testPanicCleanup {
204 t.Cleanup(func() {
205 fmt.Printf("ran middle cleanup %d\n", i)
206 if *testPanicCleanupPanic == "middle" {
207 panic("middle cleanup")
208 }
209 })
210 }
211 if chosen && *testPanicParallel {
212 t.Parallel()
213 }
214 t.Log(t.Name())
215 t.Output().Write([]byte(t.Name()))
216 if chosen {
217 if *testPanicCleanup {
218 t.Cleanup(func() {
219 fmt.Printf("ran inner cleanup %d\n", i)
220 if *testPanicCleanupPanic == "inner" {
221 panic("inner cleanup")
222 }
223 })
224 }
225 panic("panic")
226 }
227 })
228 }
229 }
230
231 func TestMorePanic(t *testing.T) {
232 testenv.MustHaveExec(t)
233
234 testCases := []struct {
235 desc string
236 flags []string
237 want string
238 }{
239 {
240 desc: "Issue 48502: call runtime.Goexit in t.Cleanup after panic",
241 flags: []string{"-test.run=^TestGoexitInCleanupAfterPanicHelper$"},
242 want: `panic: die
243 panic: test executed panic(nil) or runtime.Goexit`,
244 },
245 {
246 desc: "Issue 48515: call t.Run in t.Cleanup should trigger panic",
247 flags: []string{"-test.run=^TestCallRunInCleanupHelper$"},
248 want: `panic: testing: t.Run called during t.Cleanup`,
249 },
250 }
251
252 for _, tc := range testCases {
253 cmd := exec.Command(testenv.Executable(t), tc.flags...)
254 cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1")
255 b, _ := cmd.CombinedOutput()
256 got := string(b)
257 want := tc.want
258 re := makeRegexp(want)
259 if ok, err := regexp.MatchString(re, got); !ok || err != nil {
260 t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want)
261 }
262 }
263 }
264
265 func TestCallRunInCleanupHelper(t *testing.T) {
266 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
267 return
268 }
269
270 t.Cleanup(func() {
271 t.Run("in-cleanup", func(t *testing.T) {
272 t.Log("must not be executed")
273 })
274 })
275 }
276
277 func TestGoexitInCleanupAfterPanicHelper(t *testing.T) {
278 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
279 return
280 }
281
282 t.Cleanup(func() { runtime.Goexit() })
283 t.Parallel()
284 panic("die")
285 }
286
View as plain text