-
Notifications
You must be signed in to change notification settings - Fork 180
Fix Panics and PanicMatches checks to work with go1.21 nil panics #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v1
Are you sure you want to change the base?
Conversation
cc @niemeyer |
Result before this PR:
Result with this PR:
|
Is this still necessary? Looking at the Go 1.21 release notes, they ended up making the behaviour dependent on the go version in the main package's Using Go 1.22, go-check's tests pass for me. If I edit the go.mod to bump the minimum Go version to 1.21 and run Or are there cases where one module's tests will be run against another module's |
Yes, this is still needed. This is fixing the checker to work properly with nil panics, not just fixing unit tests. |
What I mean is that in a Go 1.21+ module, why wouldn't you just write the following?
That is correctly testing the behaviour of the closure under the semantics the module has opted into. The only case I can think of where you could run into problems is where you have a test suite in one module call a helper from a second module that performs a Are there other cases where there's ambiguity that I'm not thinking of? |
This change was trying to accomplish two things:
|
The release notes say that the old behaviour "is enabled automatically when compiling a program whose main package is in a module that declares go 1.20 or earlier". In the case of testing the "main package" is going to be the package containing the tests, so it is in control of the behaviour it will see when it's tests are run. If I write a program that uses go-check for testing (or a library that uses go-check for testing), the go-check tests are not run in the context of my And looking at it from the other end: if I write a test that asserts that some code I've written causes a nil panic and then upgrade the minimum Go version in my |
To answer this question specifically: yes, it is possible to test packages contained in modules other than the main module, although it's likely less frequently done than testing only the packages contained in the main module. For example, as documented at https://pkg.go.dev/cmd/go#hdr-Package_lists_and_patterns, the package pattern "all" expands to all packages in the main module and their dependencies, including dependencies needed by tests of any of those. So if a new module is created with a go directive at 1.21 or higher and has $ go version
go version go1.22.4 darwin/arm64
$ cd $(mktemp -d)
$ go mod init example
go: creating new go.mod: module example
$ echo 'package example_test
import "testing"
import . "gopkg.in/check.v1"
func Test(t *testing.T) { TestingT(t) }; func init() { Suite(MySuite{}) }
type MySuite struct{}
func (MySuite) TestHello(c *C) { c.Check(123, Equals, 123) }' > example_test.go
$ go get -t
go: added github.com/kr/pretty v0.2.1
go: added github.com/kr/text v0.1.0
go: added gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
$ go list -m all
example
github.com/kr/pretty v0.2.1
github.com/kr/pty v1.1.1
github.com/kr/text v0.1.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
$ go test -short all
[…]
ok example 0.185s
[…]
OOPS: 136 passed, 2 FAILED
--- FAIL: Test (0.26s)
FAIL
FAIL gopkg.in/check.v1 0.456s
[…]
FAIL
$ echo $?
1 |
@dmitshur, thanks for the example
That's a good point. I reworked this PR to limit the change to This ensures the checkers won't mask assertions callers are relying on, but makes it so that internal gocheck tests pass with go 1.21+ panicnil on and off. go version
go version go1.22.4 darwin/arm64
GODEBUG=panicnil=0 go test . -count=1
ok gopkg.in/check.v1 0.259s
GODEBUG=panicnil=1 go test . -count=1
ok gopkg.in/check.v1 0.222s |
ce2056f
to
215bc26
Compare
@jhenstridge, any feedback on #136 (comment)? |
go1.21 changes
panic
to propagate an instance of&runtime.PanicNilError{}
ifpanic(nil)
is called. See golang/go#25448 and https://go.dev/cl/461956 for details.This library exercises behavior of
panic(nil)
in tests, which means this library no longer passes unit tests at go tip.This PR adjusts the
Panics
andPanicMatches
tests to work with bothnil
and&runtime.NilPanicError{}
propagation for nil panics.