From 29e55b2fd3f55c5c24c36d1995a5242f1ee34014 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 22 Aug 2024 09:09:15 -0700 Subject: [PATCH] unix: use os.Executable rather than os.Args[0] in tests Change-Id: I67a063d747c6e34dcd0292fdb3a9a0d965a6e133 Reviewed-on: https://go-review.googlesource.com/c/sys/+/607875 Commit-Queue: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Reviewed-by: Michael Knyszek Auto-Submit: Ian Lance Taylor --- unix/darwin_test.go | 6 +++++- unix/openbsd_test.go | 2 +- unix/syscall_freebsd_test.go | 2 +- unix/syscall_unix_test.go | 6 +++++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/unix/darwin_test.go b/unix/darwin_test.go index 8edde104b..f8fea55f1 100644 --- a/unix/darwin_test.go +++ b/unix/darwin_test.go @@ -35,10 +35,14 @@ func TestDarwinLoader(t *testing.T) { // // In an ideal world each syscall would have its own test, so this test // would be unnecessary. Unfortunately, we do not live in that world. + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } for _, test := range darwinTests { // Call the test binary recursively, giving it a magic argument // (see init below) and the name of the test to run. - cmd := exec.Command(os.Args[0], "testDarwinLoader", test.name) + cmd := exec.Command(exe, "testDarwinLoader", test.name) // Run subprocess, collect results. Note that we expect the subprocess // to fail somehow, so the error is irrelevant. diff --git a/unix/openbsd_test.go b/unix/openbsd_test.go index 8ca05245d..cd36c4375 100644 --- a/unix/openbsd_test.go +++ b/unix/openbsd_test.go @@ -43,7 +43,7 @@ func init() { // testCmd generates a proper command that, when executed, runs the test // corresponding to the given key. func testCmd(procName string) (*exec.Cmd, error) { - exe, err := filepath.Abs(os.Args[0]) + exe, err := os.Executable() if err != nil { return nil, err } diff --git a/unix/syscall_freebsd_test.go b/unix/syscall_freebsd_test.go index cbe3d3669..46b48d6f3 100644 --- a/unix/syscall_freebsd_test.go +++ b/unix/syscall_freebsd_test.go @@ -53,7 +53,7 @@ func init() { } func testCmd(procName string, procArg string) (*exec.Cmd, error) { - exe, err := filepath.Abs(os.Args[0]) + exe, err := os.Executable() if err != nil { return nil, err } diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go index 29c158fba..3bb5c7a10 100644 --- a/unix/syscall_unix_test.go +++ b/unix/syscall_unix_test.go @@ -205,7 +205,11 @@ func TestPassFD(t *testing.T) { defer writeFile.Close() defer readFile.Close() - cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", t.TempDir()) + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + cmd := exec.Command(exe, "-test.run=^TestPassFD$", "--", t.TempDir()) cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} if lp := os.Getenv("LD_LIBRARY_PATH"); lp != "" { cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+lp)