aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorAlexey Vilenski <bynovhack@gmail.com>2021-03-05 11:37:54 +0000
committerDaniel Martí <mvdan@mvdan.cc>2021-03-05 11:58:31 +0000
commit2e794c2bb1302af764670dba894bbfe537bd63f0 (patch)
treed31089c269521fc6da735a6538235ae8954855a8 /src/testing/testing.go
parent2217e89ba326875470a856cd0da79f3ec9a896b8 (diff)
downloadgo-2e794c2bb1302af764670dba894bbfe537bd63f0.tar.xz
testing: add TB.Setenv
Add a new method TB.Setenv that'll set environment variables only for the isolated lifetime of the test, and will clean up and unset these variables when the test ends. This method disables the test or benchmark from running in parallel. Fixes #41260 Change-Id: I0a18f094ec1c6ec3157b4b12993ea3075e2e9867 GitHub-Last-Rev: 0ca12fa565318f350b927e2ef94f3b4f792c75c2 GitHub-Pull-Request: golang/go#41857 Reviewed-on: https://go-review.googlesource.com/c/go/+/260577 Trust: Daniel Martí <mvdan@mvdan.cc> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: roger peppe <rogpeppe@gmail.com>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 466dd96981..fc52f3c547 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -667,6 +667,7 @@ var _ TB = (*B)(nil)
type T struct {
common
isParallel bool
+ isEnvSet bool
context *testContext // For running tests and subtests.
}
@@ -964,6 +965,29 @@ func (c *common) TempDir() string {
return dir
}
+// Setenv calls os.Setenv(key, value) and uses Cleanup to
+// restore the environment variable to its original value
+// after the test.
+//
+// This cannot be used in parallel tests.
+func (c *common) Setenv(key, value string) {
+ prevValue, ok := os.LookupEnv(key)
+
+ if err := os.Setenv(key, value); err != nil {
+ c.Fatalf("cannot set environment variable: %v", err)
+ }
+
+ if ok {
+ c.Cleanup(func() {
+ os.Setenv(key, prevValue)
+ })
+ } else {
+ c.Cleanup(func() {
+ os.Unsetenv(key)
+ })
+ }
+}
+
// panicHanding is an argument to runCleanup.
type panicHandling int
@@ -1035,6 +1059,9 @@ func (t *T) Parallel() {
if t.isParallel {
panic("testing: t.Parallel called multiple times")
}
+ if t.isEnvSet {
+ panic("testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests")
+ }
t.isParallel = true
// We don't want to include the time we spend waiting for serial tests
@@ -1068,6 +1095,21 @@ func (t *T) Parallel() {
t.raceErrors += -race.Errors()
}
+// Setenv calls os.Setenv(key, value) and uses Cleanup to
+// restore the environment variable to its original value
+// after the test.
+//
+// This cannot be used in parallel tests.
+func (t *T) Setenv(key, value string) {
+ if t.isParallel {
+ panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests")
+ }
+
+ t.isEnvSet = true
+
+ t.common.Setenv(key, value)
+}
+
// InternalTest is an internal type but exported because it is cross-package;
// it is part of the implementation of the "go test" command.
type InternalTest struct {