Skip to content

Commit c5fd45a

Browse files
committed
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.
1 parent 85830ee commit c5fd45a

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

cmd/testscript/testdata/simple.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# With .gomodproxy supporting files, any GOPROXY from the
22
# environment should be overridden by the test proxy.
3+
# Note that we want an empty GOMODCACHE, to ensure we have to download modules
4+
# from our proxy.
35
env GOPROXY=0.1.2.3
46

57
unquote file.txt
@@ -8,6 +10,7 @@ stdout 'example.com/mod'
810
! stderr .+
911

1012
-- file.txt --
13+
>env GOMODCACHE=$WORK
1114
>go list -m
1215
>stdout 'example.com/mod'
1316
>go list fruit.com/...

cmd/txtar-addmod/addmod.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package main
2020

2121
import (
2222
"bytes"
23+
"encoding/json"
2324
"flag"
2425
"fmt"
2526
"io/ioutil"
@@ -100,9 +101,17 @@ func main1() int {
100101
return string(out)
101102
}
102103

103-
gopath := strings.TrimSpace(run("go", "env", "GOPATH"))
104-
if gopath == "" {
105-
fatalf("cannot find GOPATH")
104+
var goEnv struct {
105+
GOPATH string
106+
GOMODCACHE string
107+
}
108+
if err := json.Unmarshal([]byte(run("go", "env", "-json", "GOPATH", "GOMODCACHE")), &goEnv); err != nil {
109+
fatalf("cannot unmarshal 'go env -json': %v", err)
110+
}
111+
modCache := goEnv.GOMODCACHE
112+
if modCache == "" {
113+
// For Go 1.14 and older.
114+
modCache = filepath.Join(goEnv.GOPATH, "pkg", "mod")
106115
}
107116

108117
exitCode := 0
@@ -131,13 +140,13 @@ func main1() int {
131140
}
132141
path = encpath
133142

134-
mod, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
143+
mod, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".mod"))
135144
if err != nil {
136145
log.Printf("%s: %v", arg, err)
137146
exitCode = 1
138147
continue
139148
}
140-
info, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
149+
info, err := ioutil.ReadFile(filepath.Join(modCache, "cache", "download", path, "@v", vers+".info"))
141150
if err != nil {
142151
log.Printf("%s: %v", arg, err)
143152
exitCode = 1

gotooltest/setup.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
goEnv struct {
2828
GOROOT string
2929
GOCACHE string
30+
GOMODCACHE string
3031
GOPROXY string
3132
goversion string
3233
releaseTags []string
@@ -59,13 +60,14 @@ func initGoEnv() error {
5960
eout, stderr, err := run("go", "env", "-json",
6061
"GOROOT",
6162
"GOCACHE",
63+
"GOMODCACHE",
6264
"GOPROXY",
6365
)
6466
if err != nil {
6567
return fmt.Errorf("failed to determine environment from go command: %v\n%v", err, stderr)
6668
}
6769
if err := json.Unmarshal(eout.Bytes(), &goEnv); err != nil {
68-
return fmt.Errorf("failed to unmarshal GOROOT and GOCACHE tags from go command out: %v\n%v", err, eout)
70+
return fmt.Errorf("failed to unmarshal 'go env -json' output: %v\n%v", err, eout)
6971
}
7072

7173
version := goEnv.releaseTags[len(goEnv.releaseTags)-1]
@@ -138,6 +140,7 @@ func goEnviron(env0 []string) []string {
138140
"GOOS=" + runtime.GOOS,
139141
"GOROOT=" + goEnv.GOROOT,
140142
"GOCACHE=" + goEnv.GOCACHE,
143+
"GOMODCACHE=" + goEnv.GOMODCACHE,
141144
"GOPROXY=" + goEnv.GOPROXY,
142145
"goversion=" + goEnv.goversion,
143146
}...)

gotooltest/testdata/env.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GOPATH should point inside the temporary work directory, but GOCACHE and
2+
# GOMODCACHE should not, as they should reuse the host's.
3+
go env
4+
stdout GOPATH=.*${WORK@R}
5+
! stdout GOCACHE=.*${WORK@R}
6+
! stdout GOMODCACHE=.*${WORK@R}

0 commit comments

Comments
 (0)