aboutsummaryrefslogtreecommitdiff
path: root/src/internal/testenv/exec.go
diff options
context:
space:
mode:
authorCherry Mui <cherryyz@google.com>2023-10-27 12:30:53 -0400
committerCherry Mui <cherryyz@google.com>2023-10-27 17:53:23 +0000
commit5613882df7555484680ecabc0462b7c23c6f5205 (patch)
tree1d805a3a5174ed4fcbc7a455d43d34a789c5cd4e /src/internal/testenv/exec.go
parentb46aec07655bb5536fd9b9dbff0680de805214e8 (diff)
downloadgo-5613882df7555484680ecabc0462b7c23c6f5205.tar.xz
internal/testenv: use cmd.Environ in CleanCmdEnv
In CleanCmdEnv, use cmd.Environ instead of os.Environ, so it sets the PWD environment variable if cmd.Dir is set. This ensures the child process sees a canonical path for its working directory. Change-Id: Ia769552a488dc909eaf6bb7d21937adba06d1072 Reviewed-on: https://go-review.googlesource.com/c/go/+/538215 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/internal/testenv/exec.go')
-rw-r--r--src/internal/testenv/exec.go5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/internal/testenv/exec.go b/src/internal/testenv/exec.go
index 50d3b0dc73..7f6ad5cac4 100644
--- a/src/internal/testenv/exec.go
+++ b/src/internal/testenv/exec.go
@@ -100,11 +100,14 @@ func MustHaveExecPath(t testing.TB, path string) {
// CleanCmdEnv will fill cmd.Env with the environment, excluding certain
// variables that could modify the behavior of the Go tools such as
// GODEBUG and GOTRACEBACK.
+//
+// If the caller wants to set cmd.Dir, set it before calling this function,
+// so PWD will be set correctly in the environment.
func CleanCmdEnv(cmd *exec.Cmd) *exec.Cmd {
if cmd.Env != nil {
panic("environment already set")
}
- for _, env := range os.Environ() {
+ for _, env := range cmd.Environ() {
// Exclude GODEBUG from the environment to prevent its output
// from breaking tests that are trying to parse other command output.
if strings.HasPrefix(env, "GODEBUG=") {