aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorKir Kolyshkin <kolyshkin@gmail.com>2023-09-07 17:08:56 -0700
committerGopher Robot <gobot@golang.org>2024-08-16 23:48:50 +0000
commit79ca434ac608d0817a5807d1c7b2138912ed55ce (patch)
tree763cfea963ab46503845d9333d8b5c18c65c539f /src/testing/testing.go
parentff271cd391bdb8963b5409c6f4cca40af8501b51 (diff)
downloadgo-79ca434ac608d0817a5807d1c7b2138912ed55ce.tar.xz
testing: add Chdir
Some tests need to use os.Chdir, but the use is complicated because - they must change back to the old working directory; - they must not use t.Parallel. Add Chdir that covers these cases, and sets PWD environment variable to the new directory for the duration of the test for Unix platforms. Unify the panic message when t.Parallel is used together with t.Setenv or t.Chdir. Add some tests. For #62516. Change-Id: Ib050d173b26eb28a27dba5a206b2d0d877d761c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/529895 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go95
1 files changed, 75 insertions, 20 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 526cba39f8..49d14f5f66 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -379,6 +379,7 @@ import (
"io"
"math/rand"
"os"
+ "path/filepath"
"reflect"
"runtime"
"runtime/debug"
@@ -891,6 +892,7 @@ type TB interface {
Logf(format string, args ...any)
Name() string
Setenv(key, value string)
+ Chdir(dir string)
Skip(args ...any)
SkipNow()
Skipf(format string, args ...any)
@@ -917,8 +919,8 @@ var _ TB = (*B)(nil)
// may be called simultaneously from multiple goroutines.
type T struct {
common
- isEnvSet bool
- context *testContext // For running tests and subtests.
+ denyParallel bool
+ context *testContext // For running tests and subtests.
}
func (c *common) private() {}
@@ -1307,6 +1309,48 @@ func (c *common) Setenv(key, value string) {
}
}
+// Chdir calls os.Chdir(dir) and uses Cleanup to restore the current
+// working directory to its original value after the test. On Unix, it
+// also sets PWD environment variable for the duration of the test.
+//
+// Because Chdir affects the whole process, it cannot be used
+// in parallel tests or tests with parallel ancestors.
+func (c *common) Chdir(dir string) {
+ c.checkFuzzFn("Chdir")
+ oldwd, err := os.Open(".")
+ if err != nil {
+ c.Fatal(err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ c.Fatal(err)
+ }
+ // On POSIX platforms, PWD represents “an absolute pathname of the
+ // current working directory.” Since we are changing the working
+ // directory, we should also set or update PWD to reflect that.
+ switch runtime.GOOS {
+ case "windows", "plan9":
+ // Windows and Plan 9 do not use the PWD variable.
+ default:
+ if !filepath.IsAbs(dir) {
+ dir, err = os.Getwd()
+ if err != nil {
+ c.Fatal(err)
+ }
+ }
+ c.Setenv("PWD", dir)
+ }
+ c.Cleanup(func() {
+ err := oldwd.Chdir()
+ oldwd.Close()
+ if err != nil {
+ // It's not safe to continue with tests if we can't
+ // get back to the original working directory. Since
+ // we are holding a dirfd, this is highly unlikely.
+ panic("testing.Chdir: " + err.Error())
+ }
+ })
+}
+
// panicHandling controls the panic handling used by runCleanup.
type panicHandling int
@@ -1436,6 +1480,8 @@ func pcToName(pc uintptr) string {
return frame.Function
}
+const parallelConflict = `testing: test using t.Setenv or t.Chdir can not use t.Parallel`
+
// Parallel signals that this test is to be run in parallel with (and only with)
// other parallel tests. When a test is run multiple times due to use of
// -test.count or -test.cpu, multiple instances of a single test never run in
@@ -1444,8 +1490,8 @@ 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")
+ if t.denyParallel {
+ panic(parallelConflict)
}
t.isParallel = true
if t.parent.barrier == nil {
@@ -1500,34 +1546,43 @@ func (t *T) Parallel() {
t.lastRaceErrors.Store(int64(race.Errors()))
}
-// Setenv calls os.Setenv(key, value) and uses Cleanup to
-// restore the environment variable to its original value
-// after the test.
-//
-// Because Setenv affects the whole process, it cannot be used
-// in parallel tests or tests with parallel ancestors.
-func (t *T) Setenv(key, value string) {
+func (t *T) checkParallel() {
// Non-parallel subtests that have parallel ancestors may still
// run in parallel with other tests: they are only non-parallel
// with respect to the other subtests of the same parent.
- // Since SetEnv affects the whole process, we need to disallow it
- // if the current test or any parent is parallel.
- isParallel := false
+ // Since calls like SetEnv or Chdir affects the whole process, we need
+ // to deny those if the current test or any parent is parallel.
for c := &t.common; c != nil; c = c.parent {
if c.isParallel {
- isParallel = true
- break
+ panic(parallelConflict)
}
}
- if isParallel {
- panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests")
- }
- t.isEnvSet = true
+ t.denyParallel = true
+}
+// Setenv calls os.Setenv(key, value) and uses Cleanup to
+// restore the environment variable to its original value
+// after the test.
+//
+// Because Setenv affects the whole process, it cannot be used
+// in parallel tests or tests with parallel ancestors.
+func (t *T) Setenv(key, value string) {
+ t.checkParallel()
t.common.Setenv(key, value)
}
+// Chdir calls os.Chdir(dir) and uses Cleanup to restore the current
+// working directory to its original value after the test. On Unix, it
+// also sets PWD environment variable for the duration of the test.
+//
+// Because Chdir affects the whole process, it cannot be used
+// in parallel tests or tests with parallel ancestors.
+func (t *T) Chdir(dir string) {
+ t.checkParallel()
+ t.common.Chdir(dir)
+}
+
// 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 {