Skip to content

Commit

Permalink
Mark VerifyNone as a test Helper (#75)
Browse files Browse the repository at this point in the history
Mark `VerifyNone(...)` as a test `Helper` function, so that it reports the error at the call site, rather than `leaks.go:78:`.

From the [go docs](https://pkg.go.dev/testing#T.Helper):

> Helper marks the calling function as a test helper function. When printing file and line information, that function will be skipped. Helper may be called simultaneously from multiple goroutines.

This is useful for tests that verify go routines at multiple points, so failures can be more easily distinguished.
  • Loading branch information
tallclair authored Aug 30, 2022
1 parent 4e045fd commit 2dfebe8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions leaks.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,21 @@ func Find(options ...Option) error {
return fmt.Errorf("found unexpected goroutines:\n%s", stacks)
}

type testHelper interface {
Helper()
}

// VerifyNone marks the given TestingT as failed if any extra goroutines are
// found by Find. This is a helper method to make it easier to integrate in
// tests by doing:
//
// defer VerifyNone(t)
func VerifyNone(t TestingT, options ...Option) {
if h, ok := t.(testHelper); ok {
// Mark this function as a test helper, if available.
h.Helper()
}

if err := Find(options...); err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 2dfebe8

Please sign in to comment.