Skip to content

Commit 414c1d4

Browse files
author
Bryan C. Mills
committed
cmd/go: derive TestExecutableGOROOT environment from tg.env instead of os.Environ()
TestExecutableGOROOT, unlike most other tests in go_test.go, was running subcommands in a process with an environment derived directly from os.Environ(), rather than using tg.env on its testgoData object. Since tg.env is what sets GO111MODULE=off for GOPATH-mode tests, that caused TestExecutableGOROOT to unexpectedly run in module mode instead of GOPATH mode. If the user's environment included 'GOFLAGS=-mod=mod', that would cause the test to spuriously fail due to the inability to download modules to $HOME (which in this test binary is hard-coded to "/test-go-home-does-not-exist"). Updates #33848 Change-Id: I2f343008dd9e38cd76b9919eafd5a3181d0cbd6f Reviewed-on: https://go-review.googlesource.com/c/go/+/205064 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent f0390ff commit 414c1d4

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/cmd/go/go_test.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,23 +4687,19 @@ func copyFile(src, dst string, perm os.FileMode) error {
46874687
return err2
46884688
}
46894689

4690+
// TestExecutableGOROOT verifies that the cmd/go binary itself uses
4691+
// os.Executable (when available) to locate GOROOT.
46904692
func TestExecutableGOROOT(t *testing.T) {
46914693
skipIfGccgo(t, "gccgo has no GOROOT")
4692-
if runtime.GOOS == "openbsd" {
4693-
t.Skipf("test case does not work on %s, missing os.Executable", runtime.GOOS)
4694-
}
46954694

4696-
// Env with no GOROOT.
4697-
var env []string
4698-
for _, e := range os.Environ() {
4699-
if !strings.HasPrefix(e, "GOROOT=") {
4700-
env = append(env, e)
4701-
}
4702-
}
4695+
// Note: Must not call tg methods inside subtests: tg is attached to outer t.
4696+
tg := testgo(t)
4697+
tg.unsetenv("GOROOT")
4698+
defer tg.cleanup()
47034699

47044700
check := func(t *testing.T, exe, want string) {
47054701
cmd := exec.Command(exe, "env", "GOROOT")
4706-
cmd.Env = env
4702+
cmd.Env = tg.env
47074703
out, err := cmd.CombinedOutput()
47084704
if err != nil {
47094705
t.Fatalf("%s env GOROOT: %v, %s", exe, err, out)
@@ -4723,10 +4719,6 @@ func TestExecutableGOROOT(t *testing.T) {
47234719
}
47244720
}
47254721

4726-
// Note: Must not call tg methods inside subtests: tg is attached to outer t.
4727-
tg := testgo(t)
4728-
defer tg.cleanup()
4729-
47304722
tg.makeTempdir()
47314723
tg.tempDir("new/bin")
47324724
newGoTool := tg.path("new/bin/go" + exeSuffix)
@@ -4773,8 +4765,9 @@ func TestExecutableGOROOT(t *testing.T) {
47734765
}
47744766

47754767
cmd := exec.Command(newGoTool, "run", "testdata/print_goroot.go")
4776-
cmd.Env = env
4777-
out, err := cmd.CombinedOutput()
4768+
cmd.Env = tg.env
4769+
cmd.Stderr = os.Stderr
4770+
out, err := cmd.Output()
47784771
if err != nil {
47794772
t.Fatalf("%s run testdata/print_goroot.go: %v, %s", newGoTool, err, out)
47804773
}

src/cmd/go/testdata/print_goroot.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
package main
66

7-
import "runtime"
7+
import (
8+
"fmt"
9+
"runtime"
10+
)
811

912
func main() {
10-
println(runtime.GOROOT())
13+
fmt.Println(runtime.GOROOT())
1114
}

0 commit comments

Comments
 (0)