aboutsummaryrefslogtreecommitdiff
path: root/src/internal/testenv
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2019-03-05 08:51:44 -0500
committerBryan C. Mills <bcmills@google.com>2019-03-05 18:21:29 +0000
commit9670e81c2e776b4781bc4cecddf45052ebf0afe6 (patch)
tree39085167c8c83b96e423334cfa67c62d973e96ab /src/internal/testenv
parente44a031651d042107d446b4038a70c6da763e2d5 (diff)
downloadgo-9670e81c2e776b4781bc4cecddf45052ebf0afe6.tar.xz
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: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/internal/testenv')
-rw-r--r--src/internal/testenv/testenv.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/internal/testenv/testenv.go b/src/internal/testenv/testenv.go
index 8f69fe0da5..72e4d803cb 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.