From c5fd45a58807701e2c6ea10b9f7499d6a5e31d2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 8 Oct 2020 10:53:06 +0100 Subject: [PATCH] gotooltest: proxy the new GOMODCACHE env var too Added in 1.15, it's like GOCACHE but for the module download cache instead of the build cache. Also add a minimal test to check that the added env vars look like what we expect. A couple of other packages needed to be updated. txtar-addmod did not support GOMODCACHE yet, and a cmd/testscript test assumed that GOMODCACHE was always thrown away between test runs. --- cmd/testscript/testdata/simple.txt | 3 +++ cmd/txtar-addmod/addmod.go | 19 ++++++++++++++----- gotooltest/setup.go | 5 ++++- gotooltest/testdata/env.txt | 6 ++++++ 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 gotooltest/testdata/env.txt diff --git a/cmd/testscript/testdata/simple.txt b/cmd/testscript/testdata/simple.txt index 03514580..347569c6 100644 --- a/cmd/testscript/testdata/simple.txt +++ b/cmd/testscript/testdata/simple.txt @@ -1,5 +1,7 @@ # With .gomodproxy supporting files, any GOPROXY from the # environment should be overridden by the test proxy. +# Note that we want an empty GOMODCACHE, to ensure we have to download modules +# from our proxy. env GOPROXY=0.1.2.3 unquote file.txt @@ -8,6 +10,7 @@ stdout 'example.com/mod' ! stderr .+ -- file.txt -- +>env GOMODCACHE=$WORK >go list -m >stdout 'example.com/mod' >go list fruit.com/... diff --git a/cmd/txtar-addmod/addmod.go b/cmd/txtar-addmod/addmod.go index 663f1ab0..67a4a52e 100644 --- a/cmd/txtar-addmod/addmod.go +++ b/cmd/txtar-addmod/addmod.go @@ -20,6 +20,7 @@ package main import ( "bytes" + "encoding/json" "flag" "fmt" "io/ioutil" @@ -100,9 +101,17 @@ func main1() int { return string(out) } - gopath := strings.TrimSpace(run("go", "env", "GOPATH")) - if gopath == "" { - fatalf("cannot find GOPATH") + var goEnv struct { + GOPATH string + GOMODCACHE string + } + if err := json.Unmarshal([]byte(run("go", "env", "-json", "GOPATH", "GOMODCACHE")), &goEnv); err != nil { + fatalf("cannot unmarshal 'go env -json': %v", err) + } + modCache := goEnv.GOMODCACHE + if modCache == "" { + // For Go 1.14 and older. + modCache = filepath.Join(goEnv.GOPATH, "pkg", "mod") } exitCode := 0 @@ -131,13 +140,13 @@ func main1() int { } path = encpath - mod, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod")) + mod, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".mod")) if err != nil { log.Printf("%s: %v", arg, err) exitCode = 1 continue } - info, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info")) + info, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".info")) if err != nil { log.Printf("%s: %v", arg, err) exitCode = 1 diff --git a/gotooltest/setup.go b/gotooltest/setup.go index 84705cfa..eee59faa 100644 --- a/gotooltest/setup.go +++ b/gotooltest/setup.go @@ -27,6 +27,7 @@ var ( goEnv struct { GOROOT string GOCACHE string + GOMODCACHE string GOPROXY string goversion string releaseTags []string @@ -59,13 +60,14 @@ func initGoEnv() error { eout, stderr, err := run("go", "env", "-json", "GOROOT", "GOCACHE", + "GOMODCACHE", "GOPROXY", ) if err != nil { return fmt.Errorf("failed to determine environment from go command: %v\n%v", err, stderr) } if err := json.Unmarshal(eout.Bytes(), &goEnv); err != nil { - return fmt.Errorf("failed to unmarshal GOROOT and GOCACHE tags from go command out: %v\n%v", err, eout) + return fmt.Errorf("failed to unmarshal 'go env -json' output: %v\n%v", err, eout) } version := goEnv.releaseTags[len(goEnv.releaseTags)-1] @@ -138,6 +140,7 @@ func goEnviron(env0 []string) []string { "GOOS=" + runtime.GOOS, "GOROOT=" + goEnv.GOROOT, "GOCACHE=" + goEnv.GOCACHE, + "GOMODCACHE=" + goEnv.GOMODCACHE, "GOPROXY=" + goEnv.GOPROXY, "goversion=" + goEnv.goversion, }...) diff --git a/gotooltest/testdata/env.txt b/gotooltest/testdata/env.txt new file mode 100644 index 00000000..73436307 --- /dev/null +++ b/gotooltest/testdata/env.txt @@ -0,0 +1,6 @@ +# GOPATH should point inside the temporary work directory, but GOCACHE and +# GOMODCACHE should not, as they should reuse the host's. +go env +stdout GOPATH=.*${WORK@R} +! stdout GOCACHE=.*${WORK@R} +! stdout GOMODCACHE=.*${WORK@R}