From c564aebce99fb92b8dc26b203f4f32e4977c0aed Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 8 Sep 2016 18:50:59 -0700 Subject: [PATCH] internal/testenv: add GoTool GoToolPath requires a *testing.T to handle errors. GoTool provides a variant that returns errors for clients without a *testing.T, such as that found in CL 27811. Change-Id: I7ac8b7ec9d472894c37223c5f7b121ec823e7f61 Reviewed-on: https://go-review.googlesource.com/28787 Reviewed-by: Brad Fitzpatrick Reviewed-by: Rob Pike Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot --- src/internal/testenv/testenv.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go index f99ec26557..a8aa2c7464 100644 --- a/src/internal/testenv/testenv.go +++ b/src/internal/testenv/testenv.go @@ -11,6 +11,7 @@ package testenv import ( + "errors" "flag" "os" "os/exec" @@ -67,26 +68,36 @@ func MustHaveGoRun(t *testing.T) { } // GoToolPath reports the path to the Go tool. +// It is a convenience wrapper around GoTool. // If the tool is unavailable GoToolPath calls t.Skip. // If the tool should be available and isn't, GoToolPath calls t.Fatal. func GoToolPath(t *testing.T) string { MustHaveGoBuild(t) + path, err := GoTool() + if err != nil { + t.Fatal(err) + } + return path +} +// GoTool reports the path to the Go tool. +func GoTool() (string, error) { + if !HasGoBuild() { + return "", errors.New("platform cannot run go tool") + } var exeSuffix string if runtime.GOOS == "windows" { exeSuffix = ".exe" } - path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix) if _, err := os.Stat(path); err == nil { - return path + return path, nil } - goBin, err := exec.LookPath("go" + exeSuffix) if err != nil { - t.Fatalf("cannot find go tool: %v", err) + return "", errors.New("cannot find go tool: " + err.Error()) } - return goBin + return goBin, nil } // HasExec reports whether the current system can start new processes -- 2.48.1