From 2dfebe88ddf19de216c4ab15a1189fc640b1ea9f Mon Sep 17 00:00:00 2001 From: Tim Allclair Date: Mon, 29 Aug 2022 17:01:54 -0700 Subject: [PATCH] Mark VerifyNone as a test Helper (#75) 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. --- leaks.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/leaks.go b/leaks.go index f44dc25..ace2e2b 100644 --- a/leaks.go +++ b/leaks.go @@ -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) }