diff --git a/src/cmd/addr2line/addr2line_test.go b/src/cmd/addr2line/addr2line_test.go index 22bf1379bb72147c6ccbf72603e4fc8654356478..183a22f8f378ff188c4646c0fc237ef0d5315118 100644 --- a/src/cmd/addr2line/addr2line_test.go +++ b/src/cmd/addr2line/addr2line_test.go @@ -115,3 +115,5 @@ func TestAddr2Line(t *testing.T) { testAddr2Line(t, exepath, syms[symName]) testAddr2Line(t, exepath, "0x"+syms[symName]) } + +func init() { testenv.SetModVendor() } diff --git a/src/cmd/compile/internal/gc/scope_test.go b/src/cmd/compile/internal/gc/scope_test.go index e327dc02af2df236e93d5c3eafe54aff21b10ac1..e4861c686cdf4e1a67f57c809f0b5141f0bb34bc 100644 --- a/src/cmd/compile/internal/gc/scope_test.go +++ b/src/cmd/compile/internal/gc/scope_test.go @@ -202,6 +202,8 @@ var testfile = []testline{ const detailOutput = false +func init() { testenv.SetModVendor() } + // Compiles testfile checks that the description of lexical blocks emitted // by the linker in debug_info, for each function in the main package, // corresponds to what we expect it to be. diff --git a/src/cmd/cover/cover_test.go b/src/cmd/cover/cover_test.go index f002442b638fb758edb5beaf23c37192a297ff02..d7e6ca99b7fd63af3b7ee26cc2e17e89ac9855a0 100644 --- a/src/cmd/cover/cover_test.go +++ b/src/cmd/cover/cover_test.go @@ -77,6 +77,8 @@ var debug = flag.Bool("debug", false, "keep rewritten files for debugging") // We use TestMain to set up a temporary directory and remove it when // the tests are done. func TestMain(m *testing.M) { + testenv.SetModVendor() + dir, err := ioutil.TempDir("", "gotestcover") if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index dfada6c806a6c7edf44a0a068d6850c05083f4ab..9ba52e609e1ef67492a522df20fd267586224f85 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -212,7 +212,9 @@ func TestMain(m *testing.M) { return } - out, err := exec.Command(gotool, args...).CombinedOutput() + buildCmd := exec.Command(gotool, args...) + buildCmd.Env = append(os.Environ(), "GOFLAGS=-mod=vendor") + out, err := buildCmd.CombinedOutput() if err != nil { fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out) os.Exit(2) diff --git a/src/cmd/internal/obj/x86/obj6_test.go b/src/cmd/internal/obj/x86/obj6_test.go index c5399744f2d18761e4a03af33018d6b715051fca..2b1a729c8fcfc89d9e33d446147ebd15e08ec145 100644 --- a/src/cmd/internal/obj/x86/obj6_test.go +++ b/src/cmd/internal/obj/x86/obj6_test.go @@ -134,6 +134,8 @@ func parseOutput(t *testing.T, td *ParsedTestData, asmout []byte) { } } +func init() { testenv.SetModVendor() } + func TestDynlink(t *testing.T) { testenv.MustHaveGoBuild(t) diff --git a/src/cmd/link/internal/ld/ld_test.go b/src/cmd/link/internal/ld/ld_test.go index 081642931652bafe9a34750acfd8677cd2108445..219b2a63cacc060e6df9ae88483e4cd0c4ff2781 100644 --- a/src/cmd/link/internal/ld/ld_test.go +++ b/src/cmd/link/internal/ld/ld_test.go @@ -13,6 +13,8 @@ import ( "testing" ) +func init() { testenv.SetModVendor() } + func TestUndefinedRelocErrors(t *testing.T) { t.Parallel() testenv.MustHaveGoBuild(t) diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go index 5200c3a6f084ca4f7a320eeb6ccb452db01de191..5043a778ca9edfcac8b3db1f0c8104a07a4265cb 100644 --- a/src/cmd/link/link_test.go +++ b/src/cmd/link/link_test.go @@ -38,6 +38,8 @@ func TestLargeSymName(t *testing.T) { _ = AuthorPaidByTheColumnInch } +func init() { testenv.SetModVendor() } + func TestIssue21703(t *testing.T) { t.Parallel() diff --git a/src/cmd/nm/nm_test.go b/src/cmd/nm/nm_test.go index 8176ddd7f458706065e33e910dd05860695f602a..82f4235510ecf251466f32c9deef30a0048ddd74 100644 --- a/src/cmd/nm/nm_test.go +++ b/src/cmd/nm/nm_test.go @@ -30,6 +30,8 @@ func testMain(m *testing.M) int { return 0 } + testenv.SetModVendor() + tmpDir, err := ioutil.TempDir("", "TestNM") if err != nil { fmt.Println("TempDir failed:", err) diff --git a/src/cmd/objdump/objdump_test.go b/src/cmd/objdump/objdump_test.go index a2ca32960900ed8e86a0cf5292445600134ae539..7c874e853b92b459fce7d9247908f16e1099c50d 100644 --- a/src/cmd/objdump/objdump_test.go +++ b/src/cmd/objdump/objdump_test.go @@ -24,6 +24,8 @@ func TestMain(m *testing.M) { if !testenv.HasGoBuild() { return } + testenv.SetModVendor() + var exitcode int if err := buildObjdump(); err == nil { exitcode = m.Run() diff --git a/src/cmd/vet/all/main.go b/src/cmd/vet/all/main.go index 2500c690bfd6ebf2ab9c20906f0c1ac8ae0aa292..8cc4140e6e6bf53db666cd99dc69a6a63a1b5087 100644 --- a/src/cmd/vet/all/main.go +++ b/src/cmd/vet/all/main.go @@ -44,6 +44,7 @@ func main() { log.SetPrefix("vet/all: ") log.SetFlags(0) + testenv.SetModVendor() var err error cmdGoPath, err = testenv.GoTool() if err != nil { diff --git a/src/cmd/vet/vet_test.go b/src/cmd/vet/vet_test.go index 5d8139d977b81393c0d7d8db8e269ef219f27509..62c28fb9a21ccce7ab910b21a8db59bb5a0fed9b 100644 --- a/src/cmd/vet/vet_test.go +++ b/src/cmd/vet/vet_test.go @@ -32,6 +32,8 @@ func TestMain(m *testing.M) { } func testMain(m *testing.M) int { + testenv.SetModVendor() + dir, err := ioutil.TempDir("", "vet_test") if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/src/crypto/x509/x509_test.go b/src/crypto/x509/x509_test.go index f5851f1f11b79fe03dbb8e5e038e2a3fd6f05d83..fbcdb7b58e75afb71a6751848e48f8fcd047650c 100644 --- a/src/crypto/x509/x509_test.go +++ b/src/crypto/x509/x509_test.go @@ -1146,6 +1146,8 @@ func TestParsePEMCRL(t *testing.T) { // Can't check the signature here without a package cycle. } +func init() { testenv.SetModVendor() } + func TestImports(t *testing.T) { testenv.MustHaveGoRun(t) diff --git a/src/debug/gosym/pclntab_test.go b/src/debug/gosym/pclntab_test.go index d21f0e24a8324030d4297d3b7a6697807bb20468..c67fb66f0d843caf102ff6d35da996ffa6a66bce 100644 --- a/src/debug/gosym/pclntab_test.go +++ b/src/debug/gosym/pclntab_test.go @@ -21,6 +21,8 @@ var ( pclinetestBinary string ) +func init() { testenv.SetModVendor() } + func dotest(t *testing.T) { testenv.MustHaveGoBuild(t) // For now, only works on amd64 platforms. diff --git a/src/go/importer/importer_test.go b/src/go/importer/importer_test.go index ff6e12c0da5233a5d340058e353a3768273a8722..2887ec6ea5b46b625ba166ff1b215ce31f22b32a 100644 --- a/src/go/importer/importer_test.go +++ b/src/go/importer/importer_test.go @@ -16,6 +16,8 @@ import ( "testing" ) +func init() { testenv.SetModVendor() } + func TestForCompiler(t *testing.T) { testenv.MustHaveGoBuild(t) diff --git a/src/go/internal/srcimporter/srcimporter_test.go b/src/go/internal/srcimporter/srcimporter_test.go index f8e1c323b32bb6adab817b1033a9712518a68613..06472447a635dc88f5fd83440e8761546dd5e2d3 100644 --- a/src/go/internal/srcimporter/srcimporter_test.go +++ b/src/go/internal/srcimporter/srcimporter_test.go @@ -10,7 +10,6 @@ import ( "go/types" "internal/testenv" "io/ioutil" - "os" "path" "path/filepath" "runtime" @@ -19,22 +18,7 @@ import ( "time" ) -func TestMain(m *testing.M) { - // Add -mod=vendor to GOFLAGS to ensure that we don't fetch modules while importing std or cmd. - // - // TODO(golang.org/issue/30240): If we load go.mod files from vendor/ - // automatically, this will probably no longer be necessary. - var goflags []string - for _, f := range strings.Fields(os.Getenv("GOFLAGS")) { - if !strings.HasPrefix(f, "-mod=") && !strings.HasPrefix(f, "--mod=") { - goflags = append(goflags, f) - } - } - goflags = append(goflags, "-mod=vendor") - os.Setenv("GOFLAGS", strings.Join(goflags, " ")) - - os.Exit(m.Run()) -} +func init() { testenv.SetModVendor() } const maxTime = 2 * time.Second diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go index 8f69fe0da5fbda3180502907fab9f11c47f18e6f..72e4d803cb0ba9062da609e340e0ced5dc9260c3 100644 --- a/src/internal/testenv/testenv.go +++ b/src/internal/testenv/testenv.go @@ -19,6 +19,7 @@ import ( "runtime" "strconv" "strings" + "sync" "testing" ) @@ -77,6 +78,31 @@ func MustHaveGoRun(t testing.TB) { } } +var modVendorOnce sync.Once + +// SetModVendor adds the "-mod=vendor" flag to the GOFLAGS environment variable. +// This allows tests whose working directories are within the cmd and std +// modules to run ``go'' commands without accessing the network to load +// dependencies modules. +// +// SetModVendor must be called before any test may read the GOFLAGS environment +// variable. +// +// TODO(golang.org/issue/30240): If we load go.mod files from vendor/ +// automatically, this will probably no longer be necessary. +func SetModVendor() { + modVendorOnce.Do(func() { + var goflags []string + for _, f := range strings.Fields(os.Getenv("GOFLAGS")) { + if !strings.HasPrefix(f, "-mod=") && !strings.HasPrefix(f, "--mod=") { + goflags = append(goflags, f) + } + } + goflags = append(goflags, "-mod=vendor") + os.Setenv("GOFLAGS", strings.Join(goflags, " ")) + }) +} + // GoToolPath reports the path to the Go tool. // It is a convenience wrapper around GoTool. // If the tool is unavailable GoToolPath calls t.Skip. diff --git a/src/runtime/crash_test.go b/src/runtime/crash_test.go index 03ebf022a6ef8d02aa94e4c9ff3be219f88090f5..3a27b269a126919c0b1cc719f84accb5c7bcad63 100644 --- a/src/runtime/crash_test.go +++ b/src/runtime/crash_test.go @@ -25,6 +25,8 @@ import ( var toRemove []string func TestMain(m *testing.M) { + testenv.SetModVendor() + status := m.Run() for _, file := range toRemove { os.RemoveAll(file) diff --git a/src/runtime/pprof/proto_test.go b/src/runtime/pprof/proto_test.go index 4452d5123158e5c40af264536d92a21624000ddc..a276d81c491234f7b9ab95af1cbabe04bb3d5193 100644 --- a/src/runtime/pprof/proto_test.go +++ b/src/runtime/pprof/proto_test.go @@ -301,6 +301,8 @@ func TestProcSelfMaps(t *testing.T) { }) } +func init() { testenv.SetModVendor() } + // TestMapping checkes the mapping section of CPU profiles // has the HasFunctions field set correctly. If all PCs included // in the samples are successfully symbolized, the corresponding