Commit 9670e81c authored by Bryan C. Mills's avatar Bryan C. Mills

all: add -mod=vendor to GOFLAGS in tests that execute 'go' commands within std or cmd

Updates #30228
Updates #30240
Updates #30241

Change-Id: Idc311ba77e99909318b5b86f8ef82d4878f73e47
Reviewed-on: https://go-review.googlesource.com/c/go/+/165378
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent e44a0316
...@@ -115,3 +115,5 @@ func TestAddr2Line(t *testing.T) { ...@@ -115,3 +115,5 @@ func TestAddr2Line(t *testing.T) {
testAddr2Line(t, exepath, syms[symName]) testAddr2Line(t, exepath, syms[symName])
testAddr2Line(t, exepath, "0x"+syms[symName]) testAddr2Line(t, exepath, "0x"+syms[symName])
} }
func init() { testenv.SetModVendor() }
...@@ -202,6 +202,8 @@ var testfile = []testline{ ...@@ -202,6 +202,8 @@ var testfile = []testline{
const detailOutput = false const detailOutput = false
func init() { testenv.SetModVendor() }
// Compiles testfile checks that the description of lexical blocks emitted // Compiles testfile checks that the description of lexical blocks emitted
// by the linker in debug_info, for each function in the main package, // by the linker in debug_info, for each function in the main package,
// corresponds to what we expect it to be. // corresponds to what we expect it to be.
......
...@@ -77,6 +77,8 @@ var debug = flag.Bool("debug", false, "keep rewritten files for debugging") ...@@ -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 // We use TestMain to set up a temporary directory and remove it when
// the tests are done. // the tests are done.
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testenv.SetModVendor()
dir, err := ioutil.TempDir("", "gotestcover") dir, err := ioutil.TempDir("", "gotestcover")
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
......
...@@ -212,7 +212,9 @@ func TestMain(m *testing.M) { ...@@ -212,7 +212,9 @@ func TestMain(m *testing.M) {
return 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 { if err != nil {
fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out) fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out)
os.Exit(2) os.Exit(2)
......
...@@ -134,6 +134,8 @@ func parseOutput(t *testing.T, td *ParsedTestData, asmout []byte) { ...@@ -134,6 +134,8 @@ func parseOutput(t *testing.T, td *ParsedTestData, asmout []byte) {
} }
} }
func init() { testenv.SetModVendor() }
func TestDynlink(t *testing.T) { func TestDynlink(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
......
...@@ -13,6 +13,8 @@ import ( ...@@ -13,6 +13,8 @@ import (
"testing" "testing"
) )
func init() { testenv.SetModVendor() }
func TestUndefinedRelocErrors(t *testing.T) { func TestUndefinedRelocErrors(t *testing.T) {
t.Parallel() t.Parallel()
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
......
...@@ -38,6 +38,8 @@ func TestLargeSymName(t *testing.T) { ...@@ -38,6 +38,8 @@ func TestLargeSymName(t *testing.T) {
_ = AuthorPaidByTheColumnInch _ = AuthorPaidByTheColumnInch
} }
func init() { testenv.SetModVendor() }
func TestIssue21703(t *testing.T) { func TestIssue21703(t *testing.T) {
t.Parallel() t.Parallel()
......
...@@ -30,6 +30,8 @@ func testMain(m *testing.M) int { ...@@ -30,6 +30,8 @@ func testMain(m *testing.M) int {
return 0 return 0
} }
testenv.SetModVendor()
tmpDir, err := ioutil.TempDir("", "TestNM") tmpDir, err := ioutil.TempDir("", "TestNM")
if err != nil { if err != nil {
fmt.Println("TempDir failed:", err) fmt.Println("TempDir failed:", err)
......
...@@ -24,6 +24,8 @@ func TestMain(m *testing.M) { ...@@ -24,6 +24,8 @@ func TestMain(m *testing.M) {
if !testenv.HasGoBuild() { if !testenv.HasGoBuild() {
return return
} }
testenv.SetModVendor()
var exitcode int var exitcode int
if err := buildObjdump(); err == nil { if err := buildObjdump(); err == nil {
exitcode = m.Run() exitcode = m.Run()
......
...@@ -44,6 +44,7 @@ func main() { ...@@ -44,6 +44,7 @@ func main() {
log.SetPrefix("vet/all: ") log.SetPrefix("vet/all: ")
log.SetFlags(0) log.SetFlags(0)
testenv.SetModVendor()
var err error var err error
cmdGoPath, err = testenv.GoTool() cmdGoPath, err = testenv.GoTool()
if err != nil { if err != nil {
......
...@@ -32,6 +32,8 @@ func TestMain(m *testing.M) { ...@@ -32,6 +32,8 @@ func TestMain(m *testing.M) {
} }
func testMain(m *testing.M) int { func testMain(m *testing.M) int {
testenv.SetModVendor()
dir, err := ioutil.TempDir("", "vet_test") dir, err := ioutil.TempDir("", "vet_test")
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
......
...@@ -1146,6 +1146,8 @@ func TestParsePEMCRL(t *testing.T) { ...@@ -1146,6 +1146,8 @@ func TestParsePEMCRL(t *testing.T) {
// Can't check the signature here without a package cycle. // Can't check the signature here without a package cycle.
} }
func init() { testenv.SetModVendor() }
func TestImports(t *testing.T) { func TestImports(t *testing.T) {
testenv.MustHaveGoRun(t) testenv.MustHaveGoRun(t)
......
...@@ -21,6 +21,8 @@ var ( ...@@ -21,6 +21,8 @@ var (
pclinetestBinary string pclinetestBinary string
) )
func init() { testenv.SetModVendor() }
func dotest(t *testing.T) { func dotest(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
// For now, only works on amd64 platforms. // For now, only works on amd64 platforms.
......
...@@ -16,6 +16,8 @@ import ( ...@@ -16,6 +16,8 @@ import (
"testing" "testing"
) )
func init() { testenv.SetModVendor() }
func TestForCompiler(t *testing.T) { func TestForCompiler(t *testing.T) {
testenv.MustHaveGoBuild(t) testenv.MustHaveGoBuild(t)
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"go/types" "go/types"
"internal/testenv" "internal/testenv"
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
...@@ -19,22 +18,7 @@ import ( ...@@ -19,22 +18,7 @@ import (
"time" "time"
) )
func TestMain(m *testing.M) { func init() { testenv.SetModVendor() }
// 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())
}
const maxTime = 2 * time.Second const maxTime = 2 * time.Second
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"testing" "testing"
) )
...@@ -77,6 +78,31 @@ func MustHaveGoRun(t testing.TB) { ...@@ -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. // GoToolPath reports the path to the Go tool.
// It is a convenience wrapper around GoTool. // It is a convenience wrapper around GoTool.
// If the tool is unavailable GoToolPath calls t.Skip. // If the tool is unavailable GoToolPath calls t.Skip.
......
...@@ -25,6 +25,8 @@ import ( ...@@ -25,6 +25,8 @@ import (
var toRemove []string var toRemove []string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
testenv.SetModVendor()
status := m.Run() status := m.Run()
for _, file := range toRemove { for _, file := range toRemove {
os.RemoveAll(file) os.RemoveAll(file)
......
...@@ -301,6 +301,8 @@ func TestProcSelfMaps(t *testing.T) { ...@@ -301,6 +301,8 @@ func TestProcSelfMaps(t *testing.T) {
}) })
} }
func init() { testenv.SetModVendor() }
// TestMapping checkes the mapping section of CPU profiles // TestMapping checkes the mapping section of CPU profiles
// has the HasFunctions field set correctly. If all PCs included // has the HasFunctions field set correctly. If all PCs included
// in the samples are successfully symbolized, the corresponding // in the samples are successfully symbolized, the corresponding
......
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