Commit 414c1d45 authored by Bryan C. Mills's avatar Bryan C. Mills

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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent f0390ffc
...@@ -4687,23 +4687,19 @@ func copyFile(src, dst string, perm os.FileMode) error { ...@@ -4687,23 +4687,19 @@ func copyFile(src, dst string, perm os.FileMode) error {
return err2 return err2
} }
// TestExecutableGOROOT verifies that the cmd/go binary itself uses
// os.Executable (when available) to locate GOROOT.
func TestExecutableGOROOT(t *testing.T) { func TestExecutableGOROOT(t *testing.T) {
skipIfGccgo(t, "gccgo has no GOROOT") skipIfGccgo(t, "gccgo has no GOROOT")
if runtime.GOOS == "openbsd" {
t.Skipf("test case does not work on %s, missing os.Executable", runtime.GOOS)
}
// Env with no GOROOT. // Note: Must not call tg methods inside subtests: tg is attached to outer t.
var env []string tg := testgo(t)
for _, e := range os.Environ() { tg.unsetenv("GOROOT")
if !strings.HasPrefix(e, "GOROOT=") { defer tg.cleanup()
env = append(env, e)
}
}
check := func(t *testing.T, exe, want string) { check := func(t *testing.T, exe, want string) {
cmd := exec.Command(exe, "env", "GOROOT") cmd := exec.Command(exe, "env", "GOROOT")
cmd.Env = env cmd.Env = tg.env
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
t.Fatalf("%s env GOROOT: %v, %s", exe, err, out) t.Fatalf("%s env GOROOT: %v, %s", exe, err, out)
...@@ -4723,10 +4719,6 @@ func TestExecutableGOROOT(t *testing.T) { ...@@ -4723,10 +4719,6 @@ func TestExecutableGOROOT(t *testing.T) {
} }
} }
// Note: Must not call tg methods inside subtests: tg is attached to outer t.
tg := testgo(t)
defer tg.cleanup()
tg.makeTempdir() tg.makeTempdir()
tg.tempDir("new/bin") tg.tempDir("new/bin")
newGoTool := tg.path("new/bin/go" + exeSuffix) newGoTool := tg.path("new/bin/go" + exeSuffix)
...@@ -4773,8 +4765,9 @@ func TestExecutableGOROOT(t *testing.T) { ...@@ -4773,8 +4765,9 @@ func TestExecutableGOROOT(t *testing.T) {
} }
cmd := exec.Command(newGoTool, "run", "testdata/print_goroot.go") cmd := exec.Command(newGoTool, "run", "testdata/print_goroot.go")
cmd.Env = env cmd.Env = tg.env
out, err := cmd.CombinedOutput() cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil { if err != nil {
t.Fatalf("%s run testdata/print_goroot.go: %v, %s", newGoTool, err, out) t.Fatalf("%s run testdata/print_goroot.go: %v, %s", newGoTool, err, out)
} }
......
...@@ -4,8 +4,11 @@ ...@@ -4,8 +4,11 @@
package main package main
import "runtime" import (
"fmt"
"runtime"
)
func main() { func main() {
println(runtime.GOROOT()) fmt.Println(runtime.GOROOT())
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment