Skip to content

Commit c564aeb

Browse files
committed
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 <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>
1 parent 3d562de commit c564aeb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/internal/testenv/testenv.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package testenv
1212

1313
import (
14+
"errors"
1415
"flag"
1516
"os"
1617
"os/exec"
@@ -67,26 +68,36 @@ func MustHaveGoRun(t *testing.T) {
6768
}
6869

6970
// GoToolPath reports the path to the Go tool.
71+
// It is a convenience wrapper around GoTool.
7072
// If the tool is unavailable GoToolPath calls t.Skip.
7173
// If the tool should be available and isn't, GoToolPath calls t.Fatal.
7274
func GoToolPath(t *testing.T) string {
7375
MustHaveGoBuild(t)
76+
path, err := GoTool()
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
return path
81+
}
7482

83+
// GoTool reports the path to the Go tool.
84+
func GoTool() (string, error) {
85+
if !HasGoBuild() {
86+
return "", errors.New("platform cannot run go tool")
87+
}
7588
var exeSuffix string
7689
if runtime.GOOS == "windows" {
7790
exeSuffix = ".exe"
7891
}
79-
8092
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
8193
if _, err := os.Stat(path); err == nil {
82-
return path
94+
return path, nil
8395
}
84-
8596
goBin, err := exec.LookPath("go" + exeSuffix)
8697
if err != nil {
87-
t.Fatalf("cannot find go tool: %v", err)
98+
return "", errors.New("cannot find go tool: " + err.Error())
8899
}
89-
return goBin
100+
return goBin, nil
90101
}
91102

92103
// HasExec reports whether the current system can start new processes

0 commit comments

Comments
 (0)