Skip to content

Commit 9f4aeb3

Browse files
author
Bryan C. Mills
committed
all: use a hermetic "go" tool in standard-library tests
The go/build package uses the "go" tool from the user's environment, but its tests should not assume that that tool is in any particular state, let alone appropriate for running the test. Instead, explicitly use testenv.GoTool, adding it to $PATH in a TestMain when necessary. Fixes #39199 Fixes #39198 Change-Id: I56618a55ced473e75dd96eeb3a8f7084e2e64d02 Reviewed-on: https://go-review.googlesource.com/c/go/+/234880 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alexander Rakoczy <[email protected]>
1 parent 11b3730 commit 9f4aeb3

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/go/build/build_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package build
66

77
import (
8+
"flag"
89
"internal/testenv"
910
"io"
1011
"io/ioutil"
@@ -16,6 +17,14 @@ import (
1617
"testing"
1718
)
1819

20+
func TestMain(m *testing.M) {
21+
flag.Parse()
22+
if goTool, err := testenv.GoTool(); err == nil {
23+
os.Setenv("PATH", filepath.Dir(goTool)+string(os.PathListSeparator)+os.Getenv("PATH"))
24+
}
25+
os.Exit(m.Run())
26+
}
27+
1928
func TestMatch(t *testing.T) {
2029
ctxt := Default
2130
what := "default"

src/go/internal/srcimporter/srcimporter_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package srcimporter
66

77
import (
8+
"flag"
89
"go/build"
910
"go/token"
1011
"go/types"
1112
"internal/testenv"
1213
"io/ioutil"
14+
"os"
1315
"path"
1416
"path/filepath"
1517
"runtime"
@@ -18,6 +20,14 @@ import (
1820
"time"
1921
)
2022

23+
func TestMain(m *testing.M) {
24+
flag.Parse()
25+
if goTool, err := testenv.GoTool(); err == nil {
26+
os.Setenv("PATH", filepath.Dir(goTool)+string(os.PathListSeparator)+os.Getenv("PATH"))
27+
}
28+
os.Exit(m.Run())
29+
}
30+
2131
const maxTime = 2 * time.Second
2232

2333
var importer = New(&build.Default, token.NewFileSet(), make(map[string]*types.Package))

src/text/template/link_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
if err := ioutil.WriteFile(filepath.Join(td, "x.go"), []byte(prog), 0644); err != nil {
5050
t.Fatal(err)
5151
}
52-
cmd := exec.Command("go", "build", "-o", "x.exe", "x.go")
52+
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "x.exe", "x.go")
5353
cmd.Dir = td
5454
if out, err := cmd.CombinedOutput(); err != nil {
5555
t.Fatalf("go build: %v, %s", err, out)

0 commit comments

Comments
 (0)