-
Notifications
You must be signed in to change notification settings - Fork 26
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
feat: Add T.Cleanup #79
Conversation
Adds a new T.Cleanup method to register cleanup functions. These will run after each iteration of a Check. The implementation of how cleanup functions are tracked and run is borrowed heavily from testing.T's own implementation. Namely: - [T.Cleanup puts functions in a slice](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1214-1216) - [cleanups are run in reverse order](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1433-1446) popping functions from the slice one by one - [panics are not allowed to interrupt cleanup](https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1420-1427) As with `testing.T`, cleanup runs after the context is canceled. Because Rapid's context is lazily initialized, it needs an additional check to avoid providing a valid context during cleanup. Resolves flyingmutant#62
// ignoreErrorsTB is a TB that ignores all errors posted to it. | ||
type ignoreErrorsTB struct{ TB } | ||
|
||
func (ignoreErrorsTB) Error(...interface{}) {} | ||
func (ignoreErrorsTB) Errorf(string, ...interface{}) {} | ||
func (ignoreErrorsTB) Fatal(...interface{}) {} | ||
func (ignoreErrorsTB) Fatalf(string, ...interface{}) {} | ||
func (ignoreErrorsTB) Fail() {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needed to ignore test failure from panic
t.mu.Unlock() | ||
|
||
if recurse { | ||
t.cleanup() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this black magic taken from testing
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, from here: https://cs.opensource.google/go/go/+/refs/tags/go1.24.0:src/testing/testing.go;l=1420-1427
(Minus the two recovery modes. We don't need divergent behavior in this case.)
This is will basically still let the panic propagate outwards as a regular panic (we don't recover), but it'll make sure all cleanup functions are at least invoked.
Took another pass over the documentation for both functions. |
Looks good, merged! |
Adds a new T.Cleanup method to register cleanup functions.
These will run after each iteration of a Check or custom generator.
The implementation of how cleanup functions are tracked and run
is borrowed heavily from testing.T's own implementation.
Namely:
As with
testing.T
, cleanup runs after the context is canceled.Because Rapid's Context is lazily initialized,
it needs an additional check to avoid providing a valid context
during cleanup.
Resolves #62