diff options
author | Bryan C. Mills <bcmills@google.com> | 2022-04-20 17:07:14 -0400 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-04-21 17:37:05 +0000 |
commit | b34838913da606087b0f3141891f7d0fb2254eef (patch) | |
tree | e82ffa5fd4b077814e4d2fe55ca399325f6b81ff /src/os/exec/exec_test.go | |
parent | 115852077f45141b293727558e61c0804661d328 (diff) | |
download | go-b34838913da606087b0f3141891f7d0fb2254eef.tar.gz go-b34838913da606087b0f3141891f7d0fb2254eef.zip |
os/exec: set PWD implicitly if Dir is non-empty and Env is nil
Fixes #50599.
Change-Id: I4e5dbb3972cdf21ede049567bfb98f2c992c5849
Reviewed-on: https://go-review.googlesource.com/c/go/+/401340
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r-- | src/os/exec/exec_test.go | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 73aa35f1ae..f90066cea3 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -57,12 +57,20 @@ func init() { func helperCommandContext(t *testing.T, ctx context.Context, s ...string) (cmd *exec.Cmd) { testenv.MustHaveExec(t) + // Use os.Executable instead of os.Args[0] in case the caller modifies + // cmd.Dir: if the test binary is invoked like "./exec.test", it should + // not fail spuriously. + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + cs := []string{"-test.run=TestHelperProcess", "--"} cs = append(cs, s...) if ctx != nil { - cmd = exec.CommandContext(ctx, os.Args[0], cs...) + cmd = exec.CommandContext(ctx, exe, cs...) } else { - cmd = exec.Command(os.Args[0], cs...) + cmd = exec.Command(exe, cs...) } cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1") return cmd @@ -831,6 +839,14 @@ func TestHelperProcess(*testing.T) { } pipe.Close() os.Exit(0) + case "pwd": + pwd, err := os.Getwd() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + fmt.Println(pwd) + os.Exit(0) default: fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd) os.Exit(2) |