Skip to content

Commit 215bc26

Browse files
committed
Fix Panics and PanicMatches tests to work with go1.21 nil panics
1 parent 10cb982 commit 215bc26

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

.github/workflows/test.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ jobs:
2323
run: go build .
2424
- name: Test
2525
run: go test -v ./...
26+
env:
27+
GODEBUG: panicnil=0
28+
- name: Test
29+
run: go test -v ./...
30+
env:
31+
GODEBUG: panicnil=1
2632

2733
gopath:
2834
runs-on: ubuntu-latest

checkers_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package check_test
22

33
import (
44
"errors"
5+
"fmt"
56
"reflect"
67
"runtime"
78

@@ -222,7 +223,7 @@ func (s *CheckersS) TestPanics(c *check.C) {
222223
c.Assert(names[0], check.Equals, "panic")
223224

224225
// Verify a nil panic
225-
testCheck(c, check.Panics, true, "", func() { panic(nil) }, nil)
226+
testCheck(c, check.Panics, true, "", func() { panic(nil) }, propagatedNilPanicValue())
226227
testCheck(c, check.Panics, false, "", func() { panic(nil) }, "NOPE")
227228
}
228229

@@ -247,8 +248,21 @@ func (s *CheckersS) TestPanicMatches(c *check.C) {
247248
c.Assert(params[0], check.Equals, "KABOOM")
248249
c.Assert(names[0], check.Equals, "panic")
249250

250-
// Verify a nil panic
251-
testCheck(c, check.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
251+
if v := propagatedNilPanicValue(); v == nil {
252+
// Verify a propagated nil panic
253+
testCheck(c, check.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
254+
} else {
255+
// Verify a non-nil propagation from a nil panic
256+
testCheck(c, check.PanicMatches, true, "", func() { panic(nil) }, fmt.Sprintf("%s", v))
257+
}
258+
}
259+
260+
// propagatedNilPanicValue returns the value propagated when a nil panic occurs.
261+
// Prior to go1.21, this is always nil.
262+
// In go1.21+, this is unless GODEBUG=panicnil=1 is set
263+
func propagatedNilPanicValue() (v interface{}) {
264+
defer func() { v = recover() }()
265+
panic(nil)
252266
}
253267

254268
func (s *CheckersS) TestFitsTypeOf(c *check.C) {

0 commit comments

Comments
 (0)