From c3f346a485f2fa97a7bdee82d587419b3823a1ba Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 29 Aug 2024 19:51:22 -0700 Subject: math,os,os/*: use testenv.Executable As some callers don't have a testing context, modify testenv.Executable to accept nil (similar to how testenv.GOROOT works). Change-Id: I39112a7869933785a26b5cb6520055b3cc42b847 Reviewed-on: https://go-review.googlesource.com/c/go/+/609835 Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Dmitri Shuralyov --- src/os/exec/exec_test.go | 34 ++++------------------------------ src/os/exec/lp_windows_test.go | 11 +++-------- 2 files changed, 7 insertions(+), 38 deletions(-) (limited to 'src/os/exec') diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index a0bb89e203..fd185cadcf 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -159,42 +159,16 @@ func helperCommandContext(t *testing.T, ctx context.Context, name string, args . helperCommandUsed.LoadOrStore(name, true) t.Helper() - testenv.MustHaveExec(t) - + exe := testenv.Executable(t) cs := append([]string{name}, args...) if ctx != nil { - cmd = exec.CommandContext(ctx, exePath(t), cs...) + cmd = exec.CommandContext(ctx, exe, cs...) } else { - cmd = exec.Command(exePath(t), cs...) + cmd = exec.Command(exe, cs...) } return cmd } -// exePath returns the path to the running executable. -func exePath(t testing.TB) string { - exeOnce.Do(func() { - // 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. - exeOnce.path, exeOnce.err = os.Executable() - }) - - if exeOnce.err != nil { - if t == nil { - panic(exeOnce.err) - } - t.Fatal(exeOnce.err) - } - - return exeOnce.path -} - -var exeOnce struct { - path string - err error - sync.Once -} - func chdir(t *testing.T, dir string) { t.Helper() @@ -1201,7 +1175,7 @@ func cmdHang(args ...string) { pid := os.Getpid() if *subsleep != 0 { - cmd := exec.Command(exePath(nil), "hang", subsleep.String(), "-read=true", "-probe="+probe.String()) + cmd := exec.Command(testenv.Executable(nil), "hang", subsleep.String(), "-read=true", "-probe="+probe.String()) cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr out, err := cmd.StdoutPipe() diff --git a/src/os/exec/lp_windows_test.go b/src/os/exec/lp_windows_test.go index a92a29799f..15b2a0032f 100644 --- a/src/os/exec/lp_windows_test.go +++ b/src/os/exec/lp_windows_test.go @@ -25,13 +25,8 @@ func init() { registerHelperCommand("printpath", cmdPrintPath) } -func cmdPrintPath(args ...string) { - exe, err := os.Executable() - if err != nil { - fmt.Fprintf(os.Stderr, "Executable: %v\n", err) - os.Exit(1) - } - fmt.Println(exe) +func cmdPrintPath(_ ...string) { + fmt.Println(testenv.Executable(nil)) } // makePATH returns a PATH variable referring to the @@ -82,7 +77,7 @@ func installProgs(t *testing.T, root string, files []string) { // (We use a copy instead of just a symlink to ensure that os.Executable // always reports an unambiguous path, regardless of how it is implemented.) func installExe(t *testing.T, dstPath string) { - src, err := os.Open(exePath(t)) + src, err := os.Open(testenv.Executable(t)) if err != nil { t.Fatal(err) } -- cgit v1.3-5-g9baa