From b34838913da606087b0f3141891f7d0fb2254eef Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 20 Apr 2022 17:07:14 -0400 Subject: 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 TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor --- src/os/exec/exec_test.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/os/exec/exec_test.go') 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) -- cgit v1.3