1
0
mirror of https://github.com/golang/go synced 2024-11-24 08:10:09 -07:00

cmd/go/internal/test: ensure that build.ToolDir is accurate in tests

This fixes a build failure due to inability to locate the "vet" tool
when the test binary is built with -trimpath.

Updates #51461

Change-Id: I81838cc8842e4ff7900cab81af60501ebba86ff1
Reviewed-on: https://go-review.googlesource.com/c/go/+/391808
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2022-03-10 23:38:25 -05:00 committed by Bryan Mills
parent 6378c0e753
commit d68615f64b
2 changed files with 39 additions and 17 deletions

View File

@ -87,16 +87,6 @@ func defaultContext() build.Context {
ctxt.JoinPath = filepath.Join // back door to say "do not use go command"
ctxt.GOROOT = findGOROOT()
if runtime.Compiler != "gccgo" {
// Note that we must use runtime.GOOS and runtime.GOARCH here,
// as the tool directory does not move based on environment
// variables. This matches the initialization of ToolDir in
// go/build, except for using ctxt.GOROOT rather than
// runtime.GOROOT.
build.ToolDir = filepath.Join(ctxt.GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
}
// Override defaults computed in go/build with defaults
// from go environment configuration file, if known.
ctxt.GOPATH = envOr("GOPATH", gopath(ctxt))
@ -146,10 +136,36 @@ func defaultContext() build.Context {
}
func init() {
SetGOROOT(findGOROOT())
BuildToolchainCompiler = func() string { return "missing-compiler" }
BuildToolchainLinker = func() string { return "missing-linker" }
}
func SetGOROOT(goroot string) {
BuildContext.GOROOT = goroot
GOROOT = goroot
if goroot == "" {
GOROOTbin = ""
GOROOTpkg = ""
GOROOTsrc = ""
} else {
GOROOTbin = filepath.Join(goroot, "bin")
GOROOTpkg = filepath.Join(goroot, "pkg")
GOROOTsrc = filepath.Join(goroot, "src")
}
GOROOT_FINAL = findGOROOT_FINAL(goroot)
if runtime.Compiler != "gccgo" && goroot != "" {
// Note that we must use runtime.GOOS and runtime.GOARCH here,
// as the tool directory does not move based on environment
// variables. This matches the initialization of ToolDir in
// go/build, except for using BuildContext.GOROOT rather than
// runtime.GOROOT.
build.ToolDir = filepath.Join(goroot, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
}
}
// Experiment configuration.
var (
// RawGOEXPERIMENT is the GOEXPERIMENT value set by the user.
@ -279,12 +295,12 @@ func CanGetenv(key string) bool {
}
var (
GOROOT = BuildContext.GOROOT
GOROOT string
GOROOTbin string
GOROOTpkg string
GOROOTsrc string
GOROOT_FINAL string
GOBIN = Getenv("GOBIN")
GOROOTbin = filepath.Join(GOROOT, "bin")
GOROOTpkg = filepath.Join(GOROOT, "pkg")
GOROOTsrc = filepath.Join(GOROOT, "src")
GOROOT_FINAL = findGOROOT_FINAL()
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
// Used in envcmd.MkEnv and build ID computations.
@ -386,10 +402,10 @@ func findGOROOT() string {
return def
}
func findGOROOT_FINAL() string {
func findGOROOT_FINAL(goroot string) string {
// $GOROOT_FINAL is only for use during make.bash
// so it is not settable using go/env, so we use os.Getenv here.
def := GOROOT
def := goroot
if env := os.Getenv("GOROOT_FINAL"); env != "" {
def = filepath.Clean(env)
}

View File

@ -5,13 +5,19 @@
package test
import (
"cmd/go/internal/cfg"
"cmd/go/internal/test/internal/genflags"
"flag"
"internal/testenv"
"reflect"
"strings"
"testing"
)
func TestMain(m *testing.M) {
cfg.SetGOROOT(testenv.GOROOT(nil))
}
func TestPassFlagToTestIncludesAllTestFlags(t *testing.T) {
flag.VisitAll(func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, "test.") {