diff options
author | Nobuki Fujii <scofni@gmail.com> | 2022-09-18 11:52:07 +0900 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-09-28 17:18:17 +0000 |
commit | d6ca24477afa85a3ab559935faa4fed917911e4f (patch) | |
tree | 0e988ed0c2132fcb1fb47fed3150863090c08996 /src/testing/testing_test.go | |
parent | 7f7f27f992850a06551c2798a3b874f5d5356ae9 (diff) | |
download | go-git-d6ca24477afa85a3ab559935faa4fed917911e4f.tar.gz |
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 <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/testing/testing_test.go')
-rw-r--r-- | src/testing/testing_test.go | 32 |
1 files changed, 32 insertions, 0 deletions
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") + }) + }) +} |