Skip to content

Commit

Permalink
gotooltest: proxy the new GOMODCACHE env var too
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan committed Oct 8, 2020
1 parent 85830ee commit c5fd45a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions 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
Expand All @@ -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/...
Expand Down
19 changes: 14 additions & 5 deletions cmd/txtar-addmod/addmod.go
Expand Up @@ -20,6 +20,7 @@ package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion gotooltest/setup.go
Expand Up @@ -27,6 +27,7 @@ var (
goEnv struct {
GOROOT string
GOCACHE string
GOMODCACHE string
GOPROXY string
goversion string
releaseTags []string
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
}...)
Expand Down
6 changes: 6 additions & 0 deletions 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}

0 comments on commit c5fd45a

Please sign in to comment.