Skip to content

Commit 9fb5a2f

Browse files
author
Bryan C. Mills
committed
all: set GO111MODULE=off for tests that use GOPATHs in testdata.
Some users may set GO111MODULE=on, and we will eventually want to be able to build x/tools itself in module mode. Updates golang/go#27858 Updates golang/go#27852 Change-Id: Iaf488b2a89e6526471530245cb580f1f0391a770 Reviewed-on: https://go-review.googlesource.com/137815 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
1 parent 9599141 commit 9fb5a2f

File tree

7 files changed

+90
-16
lines changed

7 files changed

+90
-16
lines changed

cmd/callgraph/main_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,25 @@ package main
1212
import (
1313
"bytes"
1414
"fmt"
15+
"log"
16+
"os"
1517
"path/filepath"
1618
"strings"
1719
"testing"
1820
)
1921

22+
func init() {
23+
// This test currently requires GOPATH mode.
24+
// Explicitly disabling module mode should suffix, but
25+
// we'll also turn off GOPROXY just for good measure.
26+
if err := os.Setenv("GO111MODULE", "off"); err != nil {
27+
log.Fatal(err)
28+
}
29+
if err := os.Setenv("GOPROXY", "off"); err != nil {
30+
log.Fatal(err)
31+
}
32+
}
33+
2034
func TestCallgraph(t *testing.T) {
2135
gopath, err := filepath.Abs("testdata")
2236
if err != nil {

cmd/fiximports/main.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,18 @@ func list(args ...string) ([]*listPackage, error) {
491491
return pkgs, nil
492492
}
493493

494-
var cwd string
495-
496-
func init() {
497-
var err error
498-
cwd, err = os.Getwd()
494+
// cwd contains the current working directory of the tool.
495+
//
496+
// It is initialized directly so that its value will be set for any other
497+
// package variables or init functions that depend on it, such as the gopath
498+
// variable in main_test.go.
499+
var cwd string = func() string {
500+
cwd, err := os.Getwd()
499501
if err != nil {
500502
log.Fatalf("os.Getwd: %v", err)
501503
}
502-
}
504+
return cwd
505+
}()
503506

504507
// shortPath returns an absolute or relative name for path, whatever is shorter.
505508
// Plundered from $GOROOT/src/cmd/go/build.go.

cmd/fiximports/main_test.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package main
1010

1111
import (
1212
"bytes"
13+
"log"
1314
"os"
1415
"path/filepath"
1516
"runtime"
@@ -32,11 +33,25 @@ import (
3233
// titanic.biz/bar -- domain is sinking; package has jumped ship to new.com/bar
3334
// titanic.biz/foo -- domain is sinking but package has no import comment yet
3435

35-
func TestFixImports(t *testing.T) {
36-
gopath := filepath.Join(cwd, "testdata")
36+
var gopath = filepath.Join(cwd, "testdata")
37+
38+
func init() {
3739
if err := os.Setenv("GOPATH", gopath); err != nil {
38-
t.Fatalf("os.Setenv: %v", err)
40+
log.Fatal(err)
41+
}
42+
43+
// This test currently requires GOPATH mode.
44+
// Explicitly disabling module mode should suffix, but
45+
// we'll also turn off GOPROXY just for good measure.
46+
if err := os.Setenv("GO111MODULE", "off"); err != nil {
47+
log.Fatal(err)
48+
}
49+
if err := os.Setenv("GOPROXY", "off"); err != nil {
50+
log.Fatal(err)
3951
}
52+
}
53+
54+
func TestFixImports(t *testing.T) {
4055
defer func() {
4156
stderr = os.Stderr
4257
*badDomains = "code.google.com"
@@ -224,11 +239,6 @@ import (
224239

225240
// TestDryRun tests that the -n flag suppresses calls to writeFile.
226241
func TestDryRun(t *testing.T) {
227-
gopath := filepath.Join(cwd, "testdata")
228-
if err := os.Setenv("GOPATH", gopath); err != nil {
229-
t.Fatalf("os.Setenv: %v", err)
230-
}
231-
232242
*dryrun = true
233243
defer func() { *dryrun = false }() // restore
234244
stderr = new(bytes.Buffer)

cmd/godoc/godoc_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,14 @@ func testWeb(t *testing.T, withIndex bool) {
233233
cmd.Stderr = os.Stderr
234234
cmd.Args[0] = "godoc"
235235

236-
// Set GOPATH variable to non-existing path.
236+
// Set GOPATH variable to non-existing path
237+
// and GOPROXY=off to disable module fetches.
237238
// We cannot just unset GOPATH variable because godoc would default it to ~/go.
238239
// (We don't want the indexer looking at the local workspace during tests.)
239-
cmd.Env = append(os.Environ(), "GOPATH=does_not_exist")
240+
cmd.Env = append(os.Environ(),
241+
"GOPATH=does_not_exist",
242+
"GOPROXY=off",
243+
"GO111MODULE=off")
240244

241245
if err := cmd.Start(); err != nil {
242246
t.Fatalf("failed to start godoc: %s", err)
@@ -448,6 +452,8 @@ func main() { print(lib.V) }
448452
cmd.Env = os.Environ()
449453
cmd.Env = append(cmd.Env, fmt.Sprintf("GOROOT=%s", filepath.Join(tmpdir, "goroot")))
450454
cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%s", filepath.Join(tmpdir, "gopath")))
455+
cmd.Env = append(cmd.Env, "GO111MODULE=off")
456+
cmd.Env = append(cmd.Env, "GOPROXY=off")
451457
cmd.Stdout = os.Stderr
452458
stderr, err := cmd.StderrPipe()
453459
if err != nil {

cmd/guru/guru_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"go/token"
3636
"io"
3737
"io/ioutil"
38+
"log"
3839
"os"
3940
"os/exec"
4041
"path/filepath"
@@ -49,6 +50,18 @@ import (
4950
guru "golang.org/x/tools/cmd/guru"
5051
)
5152

53+
func init() {
54+
// This test currently requires GOPATH mode.
55+
// Explicitly disabling module mode should suffix, but
56+
// we'll also turn off GOPROXY just for good measure.
57+
if err := os.Setenv("GO111MODULE", "off"); err != nil {
58+
log.Fatal(err)
59+
}
60+
if err := os.Setenv("GOPROXY", "off"); err != nil {
61+
log.Fatal(err)
62+
}
63+
}
64+
5265
var updateFlag = flag.Bool("update", false, "Update the golden files.")
5366

5467
type query struct {

go/analysis/passes/findcall/findcall_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package findcall_test
22

33
import (
4+
"log"
5+
"os"
46
"testing"
57

68
"golang.org/x/tools/go/analysis/analysistest"
79
"golang.org/x/tools/go/analysis/passes/findcall"
810
)
911

12+
func init() {
13+
// This test currently requires GOPATH mode.
14+
// Explicitly disabling module mode should suffix, but
15+
// we'll also turn off GOPROXY just for good measure.
16+
if err := os.Setenv("GO111MODULE", "off"); err != nil {
17+
log.Fatal(err)
18+
}
19+
if err := os.Setenv("GOPROXY", "off"); err != nil {
20+
log.Fatal(err)
21+
}
22+
}
23+
1024
// TestFromStringLiterals demonstrates how to test an analysis using
1125
// a table of string literals for each test case.
1226
//

go/analysis/passes/pkgfact/pkgfact_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package pkgfact_test
22

33
import (
4+
"log"
5+
"os"
46
"testing"
57

68
"golang.org/x/tools/go/analysis/analysistest"
79
"golang.org/x/tools/go/analysis/passes/pkgfact"
810
)
911

12+
func init() {
13+
// This test currently requires GOPATH mode.
14+
// Explicitly disabling module mode should suffix, but
15+
// we'll also turn off GOPROXY just for good measure.
16+
if err := os.Setenv("GO111MODULE", "off"); err != nil {
17+
log.Fatal(err)
18+
}
19+
if err := os.Setenv("GOPROXY", "off"); err != nil {
20+
log.Fatal(err)
21+
}
22+
}
23+
1024
func Test(t *testing.T) {
1125
testdata := analysistest.TestData()
1226
analysistest.Run(t, testdata, pkgfact.Analyzer,

0 commit comments

Comments
 (0)