]> Cypherpunks repositories - gostls13.git/commitdiff
internal/testenv: add GoTool
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 9 Sep 2016 01:50:59 +0000 (18:50 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Sat, 10 Sep 2016 20:50:12 +0000 (20:50 +0000)
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 <bradfitz@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/internal/testenv/testenv.go

index f99ec26557ffaad3e6a0a3fde550f6d9f58c7257..a8aa2c74648c4611e23b27c04ad5e213c66b88a2 100644 (file)
@@ -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