Skip to content

Commit

Permalink
gotooltest: pass through GOPROXY (#48)
Browse files Browse the repository at this point in the history
This can make tests that use the go command
considerably faster. We override GOPROXY when
we're running a local Go test proxy.
  • Loading branch information
rogpeppe authored Jan 16, 2019
1 parent af6d342 commit 7113be1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
24 changes: 14 additions & 10 deletions cmd/testscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,34 @@ func run(runDir, fileName string, verbose bool) error {
Dir: runDir,
}

if _, err := exec.LookPath("go"); err == nil {
if err := gotooltest.Setup(&p); err != nil {
return fmt.Errorf("failed to setup go tool for %v run: %v", fileName, err)
}
}

if len(gomodProxy.Files) > 0 {
srv, err := goproxytest.NewServer(mods, "")
if err != nil {
return fmt.Errorf("cannot start proxy for %v: %v", fileName, err)
}
defer srv.Close()

currSetup := p.Setup
origSetup := p.Setup

p.Setup = func(env *testscript.Env) error {
env.Vars = append(env.Vars, "GOPROXY="+srv.URL)
if currSetup != nil {
return currSetup(env)
if origSetup != nil {
if err := origSetup(env); err != nil {
return err
}
}
// Add GOPROXY after calling the original setup
// so that it overrides any GOPROXY set there.
env.Vars = append(env.Vars, "GOPROXY="+srv.URL)
return nil
}
}

if _, err := exec.LookPath("go"); err == nil {
if err := gotooltest.Setup(&p); err != nil {
return fmt.Errorf("failed to setup go tool for %v run: %v", fileName, err)
}
}

r := runner{
verbose: verbose,
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/testscript/testdata/noproxy.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# with no .gomodproxy supporting files we should not have a GOPROXY set
# With no .gomodproxy supporting files, we use the GOPROXY from
# the environment.
env GOPROXY=0.1.2.3
unquote file.txt
testscript -v file.txt

-- file.txt --
>go env
>[!windows] stdout '^GOPROXY=""$'
>[windows] stdout '^set GOPROXY=$'
>[!windows] stdout '^GOPROXY="0.1.2.3"$'
>[windows] stdout '^set GOPROXY=0.1.2.3$'
4 changes: 4 additions & 0 deletions cmd/testscript/testdata/simple.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# With .gomodproxy supporting files, any GOPROXY from the
# environment should be overridden by the test proxy.
env GOPROXY=0.1.2.3

unquote file.txt
testscript -v file.txt
stdout 'example.com/mod'
Expand Down
10 changes: 8 additions & 2 deletions gotooltest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
goEnv struct {
GOROOT string
GOCACHE string
GOPROXY string
goversion string
releaseTags []string
once sync.Once
Expand Down Expand Up @@ -55,9 +56,13 @@ func initGoEnv() error {
tagStr = strings.Trim(tagStr, "[]")
goEnv.releaseTags = strings.Split(tagStr, " ")

eout, stderr, err := run("go", "env", "-json", "GOROOT", "GOCACHE")
eout, stderr, err := run("go", "env", "-json",
"GOROOT",
"GOCACHE",
"GOPROXY",
)
if err != nil {
return fmt.Errorf("failed to determine GOROOT and GOCACHE tags from go command: %v\n%v", err, stderr)
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)
Expand Down Expand Up @@ -133,6 +138,7 @@ func goEnviron(env0 []string) []string {
"GOOS=" + runtime.GOOS,
"GOROOT=" + goEnv.GOROOT,
"GOCACHE=" + goEnv.GOCACHE,
"GOPROXY=" + goEnv.GOPROXY,
"goversion=" + goEnv.goversion,
}...)
}
Expand Down

0 comments on commit 7113be1

Please sign in to comment.