From 12e48f3bbf7a28a6340aee5d9a5eacf3a98e869c Mon Sep 17 00:00:00 2001 From: Silke Hofstra Date: Wed, 20 Apr 2022 15:02:35 +0000 Subject: [PATCH] os: look up hostname from PATH in test When running TestHostname, the location of the hostname binary is hardcoded as /bin/hostname. However, on some systems the actual location is /usr/bin/hostname. Change this behaviour to perform a lookup for hostname in PATH, and skip the test when it cannot be found there. Fixes #52402 Change-Id: I5418bf77258f5ffb2a9f834b8c68d8a7b7a452d7 GitHub-Last-Rev: 750f36fcf9d4b26b75e91b895d6f7c6a275536ee GitHub-Pull-Request: golang/go#52403 Reviewed-on: https://go-review.googlesource.com/c/go/+/400794 Run-TryBot: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: Brad Fitzpatrick Auto-Submit: Ian Lance Taylor --- src/os/os_test.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/os/os_test.go b/src/os/os_test.go index 8e2b4f3aaa..ea935d3295 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -1699,16 +1699,21 @@ func runBinHostname(t *testing.T) string { t.Fatal(err) } defer r.Close() - const path = "/bin/hostname" + + path, err := osexec.LookPath("hostname") + if err != nil { + if errors.Is(err, osexec.ErrNotFound) { + t.Skip("skipping test; test requires hostname but it does not exist") + } + t.Fatal(err) + } + argv := []string{"hostname"} if runtime.GOOS == "aix" { argv = []string{"hostname", "-s"} } p, err := StartProcess(path, argv, &ProcAttr{Files: []*File{nil, w, Stderr}}) if err != nil { - if _, err := Stat(path); IsNotExist(err) { - t.Skipf("skipping test; test requires %s but it does not exist", path) - } t.Fatal(err) } w.Close() -- 2.50.0