Skip to content
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

Merged
merged 2 commits into from
Feb 24, 2025
Merged

feat: Add T.Cleanup #79

merged 2 commits into from
Feb 24, 2025

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Feb 20, 2025

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

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
Comment on lines +254 to +261
// 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() {}
Copy link
Contributor Author

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()
Copy link
Owner

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?

Copy link
Contributor Author

@abhinav abhinav Feb 20, 2025

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.

@abhinav
Copy link
Contributor Author

abhinav commented Feb 24, 2025

Took another pass over the documentation for both functions.

@flyingmutant flyingmutant merged commit 7adc026 into flyingmutant:master Feb 24, 2025
14 checks passed
@flyingmutant
Copy link
Owner

Looks good, merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Would like for *rapid.T to implement the Cleanup function like *testing.T
2 participants