From 978ce7e252b0d6ff0a19f66206ed5f3eca281059 Mon Sep 17 00:00:00 2001 From: Changkun Ou Date: Mon, 27 Sep 2021 11:37:56 +0200 Subject: testing: reject calls to Run within Cleanup callbacks Calling t.Run inside t.Cleanup can mess up the execution order of registered Cleanup callbacks. Reject calls to Run within Cleanup callbacks. Fixes #48515 Change-Id: I61e4cb35253db1a8bbe3351d59055433030aa289 Reviewed-on: https://go-review.googlesource.com/c/go/+/352349 TryBot-Result: Gopher Robot Reviewed-by: Joedian Reid Run-TryBot: Changkun Ou Auto-Submit: Bryan Mills Reviewed-by: Bryan Mills --- src/testing/panic_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/testing/panic_test.go') diff --git a/src/testing/panic_test.go b/src/testing/panic_test.go index fafcff790e..4648057b77 100644 --- a/src/testing/panic_test.go +++ b/src/testing/panic_test.go @@ -224,6 +224,11 @@ func TestMorePanic(t *testing.T) { want: `panic: die panic: test executed panic(nil) or runtime.Goexit`, }, + { + desc: "Issue 48515: call t.Run in t.Cleanup should trigger panic", + flags: []string{"-test.run=TestCallRunInCleanupHelper"}, + want: `panic: testing: t.Run is called during t.Cleanup`, + }, } for _, tc := range testCases { @@ -239,6 +244,18 @@ func TestMorePanic(t *testing.T) { } } +func TestCallRunInCleanupHelper(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + + t.Cleanup(func() { + t.Run("in-cleanup", func(t *testing.T) { + t.Log("must not be executed") + }) + }) +} + func TestGoexitInCleanupAfterPanicHelper(t *testing.T) { if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { return -- cgit v1.3-5-g9baa