From d6ca24477afa85a3ab559935faa4fed917911e4f Mon Sep 17 00:00:00 2001 From: Nobuki Fujii Date: Sun, 18 Sep 2022 11:52:07 +0900 Subject: testing: fail if T.Setenv is called via T.Run in a parallel test The existing implementation can call to T.Setenv in T.Run even after calling to T.Parallel, so I changed it to cause a panic in that case. Fixes #55128 Change-Id: Ib89d998ff56f00f96a5ca218af071bd35fdae53a Reviewed-on: https://go-review.googlesource.com/c/go/+/431101 Run-TryBot: Bryan Mills Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills --- src/testing/testing_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/testing/testing_test.go') diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go index 08ae23991f..3616f04d5f 100644 --- a/src/testing/testing_test.go +++ b/src/testing/testing_test.go @@ -200,3 +200,35 @@ func TestSetenvWithParallelBeforeSetenv(t *testing.T) { t.Setenv("GO_TEST_KEY_1", "value") } + +func TestSetenvWithParallelParentBeforeSetenv(t *testing.T) { + t.Parallel() + + t.Run("child", func(t *testing.T) { + defer func() { + want := "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests" + if got := recover(); got != want { + t.Fatalf("expected panic; got %#v want %q", got, want) + } + }() + + t.Setenv("GO_TEST_KEY_1", "value") + }) +} + +func TestSetenvWithParallelGrandParentBeforeSetenv(t *testing.T) { + t.Parallel() + + t.Run("child", func(t *testing.T) { + t.Run("grand-child", func(t *testing.T) { + defer func() { + want := "testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests" + if got := recover(); got != want { + t.Fatalf("expected panic; got %#v want %q", got, want) + } + }() + + t.Setenv("GO_TEST_KEY_1", "value") + }) + }) +} -- cgit v1.3-5-g9baa