From c605950e940d9046b4bbfeca271c367e32cc87cc Mon Sep 17 00:00:00 2001 From: machine424 Date: Thu, 10 Oct 2024 11:15:29 +0200 Subject: [PATCH] fix: ensure the appropriate error message is produced when the RunOnFailure() option is used. adjust RunOnFailure() doc. follow up to https://github.com/uber-go/goleak/pull/129 --- leaks.go | 3 --- options.go | 5 +++-- testmain.go | 2 +- testmain_test.go | 8 ++++++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/leaks.go b/leaks.go index e0384f8..cc206f1 100644 --- a/leaks.go +++ b/leaks.go @@ -59,9 +59,6 @@ func Find(options ...Option) error { if opts.cleanup != nil { return errors.New("Cleanup can only be passed to VerifyNone or VerifyTestMain") } - if opts.runOnFailure { - return errors.New("RunOnFailure can only be passed to VerifyTestMain") - } var stacks []stack.Stack retry := true for i := 0; retry; i++ { diff --git a/options.go b/options.go index 2f76c05..cd2e3d5 100644 --- a/options.go +++ b/options.go @@ -109,8 +109,9 @@ func IgnoreCurrent() Option { }) } -// RunOnFailure makes goleak look for leaking goroutines upon test failures. -// By default goleak only looks for leaking goroutines when tests succeed. +// RunOnFailure makes [VerifyTestMain] look for leaking goroutines upon test failures as well. +// By default [VerifyTestMain] only looks for leaking goroutines when tests succeed. +// This has no effect on [VerifyNone], which always checks for leaking goroutines. func RunOnFailure() Option { return optionFunc(func(opts *opts) { opts.runOnFailure = true diff --git a/testmain.go b/testmain.go index 7185ef4..8f4dfc3 100644 --- a/testmain.go +++ b/testmain.go @@ -65,7 +65,7 @@ func VerifyTestMain(m TestingM, options ...Option) { errorMsg string ) - if !opts.runOnFailure && exitCode == 0 { + if exitCode == 0 { errorMsg = "goleak: Errors on successful test run:%v\n" run = true } else if opts.runOnFailure { diff --git a/testmain_test.go b/testmain_test.go index 7b7dd00..b418b27 100644 --- a/testmain_test.go +++ b/testmain_test.go @@ -69,11 +69,15 @@ func TestVerifyTestMain(t *testing.T) { VerifyTestMain(dummyTestMain(7), RunOnFailure()) assert.Equal(t, 7, <-exitCode, "Exit code should not be modified") - assert.Contains(t, <-stderr, "goleak: Errors", "Find leaks on unsuccessful runs with RunOnFailure specified") + assert.Contains(t, <-stderr, "goleak: Errors on unsuccessful test run", "Find leaks on unsuccessful runs with RunOnFailure specified") VerifyTestMain(dummyTestMain(0)) assert.Equal(t, 1, <-exitCode, "Expect error due to leaks on successful runs") - assert.Contains(t, <-stderr, "goleak: Errors", "Find leaks on successful runs") + assert.Contains(t, <-stderr, "goleak: Errors on successful test run", "Find leaks on successful runs") + + VerifyTestMain(dummyTestMain(0), RunOnFailure()) + assert.Equal(t, 1, <-exitCode, "Expect error due to leaks on successful runs") + assert.Contains(t, <-stderr, "goleak: Errors on successful test run", "Find leaks on successful runs") blocked.unblock() VerifyTestMain(dummyTestMain(0))