diff --git a/cmd/callgraph/main_test.go b/cmd/callgraph/main_test.go index 1d286193..54953c70 100644 --- a/cmd/callgraph/main_test.go +++ b/cmd/callgraph/main_test.go @@ -12,11 +12,25 @@ package main import ( "bytes" "fmt" + "log" + "os" "path/filepath" "strings" "testing" ) +func init() { + // This test currently requires GOPATH mode. + // Explicitly disabling module mode should suffix, but + // we'll also turn off GOPROXY just for good measure. + if err := os.Setenv("GO111MODULE", "off"); err != nil { + log.Fatal(err) + } + if err := os.Setenv("GOPROXY", "off"); err != nil { + log.Fatal(err) + } +} + func TestCallgraph(t *testing.T) { gopath, err := filepath.Abs("testdata") if err != nil { diff --git a/cmd/fiximports/main.go b/cmd/fiximports/main.go index 365a2614..7de018fd 100644 --- a/cmd/fiximports/main.go +++ b/cmd/fiximports/main.go @@ -491,15 +491,18 @@ func list(args ...string) ([]*listPackage, error) { return pkgs, nil } -var cwd string - -func init() { - var err error - cwd, err = os.Getwd() +// cwd contains the current working directory of the tool. +// +// It is initialized directly so that its value will be set for any other +// package variables or init functions that depend on it, such as the gopath +// variable in main_test.go. +var cwd string = func() string { + cwd, err := os.Getwd() if err != nil { log.Fatalf("os.Getwd: %v", err) } -} + return cwd +}() // shortPath returns an absolute or relative name for path, whatever is shorter. // Plundered from $GOROOT/src/cmd/go/build.go. diff --git a/cmd/fiximports/main_test.go b/cmd/fiximports/main_test.go index 75319a02..a2973a39 100644 --- a/cmd/fiximports/main_test.go +++ b/cmd/fiximports/main_test.go @@ -10,6 +10,7 @@ package main import ( "bytes" + "log" "os" "path/filepath" "runtime" @@ -32,11 +33,25 @@ import ( // titanic.biz/bar -- domain is sinking; package has jumped ship to new.com/bar // titanic.biz/foo -- domain is sinking but package has no import comment yet -func TestFixImports(t *testing.T) { - gopath := filepath.Join(cwd, "testdata") +var gopath = filepath.Join(cwd, "testdata") + +func init() { if err := os.Setenv("GOPATH", gopath); err != nil { - t.Fatalf("os.Setenv: %v", err) + log.Fatal(err) } + + // This test currently requires GOPATH mode. + // Explicitly disabling module mode should suffix, but + // we'll also turn off GOPROXY just for good measure. + if err := os.Setenv("GO111MODULE", "off"); err != nil { + log.Fatal(err) + } + if err := os.Setenv("GOPROXY", "off"); err != nil { + log.Fatal(err) + } +} + +func TestFixImports(t *testing.T) { defer func() { stderr = os.Stderr *badDomains = "code.google.com" @@ -224,11 +239,6 @@ import ( // TestDryRun tests that the -n flag suppresses calls to writeFile. func TestDryRun(t *testing.T) { - gopath := filepath.Join(cwd, "testdata") - if err := os.Setenv("GOPATH", gopath); err != nil { - t.Fatalf("os.Setenv: %v", err) - } - *dryrun = true defer func() { *dryrun = false }() // restore stderr = new(bytes.Buffer) diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index ab12353d..7200c917 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -233,10 +233,14 @@ func testWeb(t *testing.T, withIndex bool) { cmd.Stderr = os.Stderr cmd.Args[0] = "godoc" - // Set GOPATH variable to non-existing path. + // Set GOPATH variable to non-existing path + // and GOPROXY=off to disable module fetches. // We cannot just unset GOPATH variable because godoc would default it to ~/go. // (We don't want the indexer looking at the local workspace during tests.) - cmd.Env = append(os.Environ(), "GOPATH=does_not_exist") + cmd.Env = append(os.Environ(), + "GOPATH=does_not_exist", + "GOPROXY=off", + "GO111MODULE=off") if err := cmd.Start(); err != nil { t.Fatalf("failed to start godoc: %s", err) @@ -448,6 +452,8 @@ func main() { print(lib.V) } cmd.Env = os.Environ() cmd.Env = append(cmd.Env, fmt.Sprintf("GOROOT=%s", filepath.Join(tmpdir, "goroot"))) cmd.Env = append(cmd.Env, fmt.Sprintf("GOPATH=%s", filepath.Join(tmpdir, "gopath"))) + cmd.Env = append(cmd.Env, "GO111MODULE=off") + cmd.Env = append(cmd.Env, "GOPROXY=off") cmd.Stdout = os.Stderr stderr, err := cmd.StderrPipe() if err != nil { diff --git a/cmd/guru/guru_test.go b/cmd/guru/guru_test.go index 5a06d09c..6322275d 100644 --- a/cmd/guru/guru_test.go +++ b/cmd/guru/guru_test.go @@ -35,6 +35,7 @@ import ( "go/token" "io" "io/ioutil" + "log" "os" "os/exec" "path/filepath" @@ -49,6 +50,18 @@ import ( guru "golang.org/x/tools/cmd/guru" ) +func init() { + // This test currently requires GOPATH mode. + // Explicitly disabling module mode should suffix, but + // we'll also turn off GOPROXY just for good measure. + if err := os.Setenv("GO111MODULE", "off"); err != nil { + log.Fatal(err) + } + if err := os.Setenv("GOPROXY", "off"); err != nil { + log.Fatal(err) + } +} + var updateFlag = flag.Bool("update", false, "Update the golden files.") type query struct { diff --git a/go/analysis/passes/findcall/findcall_test.go b/go/analysis/passes/findcall/findcall_test.go index c521885c..105f1a0e 100644 --- a/go/analysis/passes/findcall/findcall_test.go +++ b/go/analysis/passes/findcall/findcall_test.go @@ -1,12 +1,26 @@ package findcall_test import ( + "log" + "os" "testing" "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/findcall" ) +func init() { + // This test currently requires GOPATH mode. + // Explicitly disabling module mode should suffix, but + // we'll also turn off GOPROXY just for good measure. + if err := os.Setenv("GO111MODULE", "off"); err != nil { + log.Fatal(err) + } + if err := os.Setenv("GOPROXY", "off"); err != nil { + log.Fatal(err) + } +} + // TestFromStringLiterals demonstrates how to test an analysis using // a table of string literals for each test case. // diff --git a/go/analysis/passes/pkgfact/pkgfact_test.go b/go/analysis/passes/pkgfact/pkgfact_test.go index 11e6f893..511026c0 100644 --- a/go/analysis/passes/pkgfact/pkgfact_test.go +++ b/go/analysis/passes/pkgfact/pkgfact_test.go @@ -1,12 +1,26 @@ package pkgfact_test import ( + "log" + "os" "testing" "golang.org/x/tools/go/analysis/analysistest" "golang.org/x/tools/go/analysis/passes/pkgfact" ) +func init() { + // This test currently requires GOPATH mode. + // Explicitly disabling module mode should suffix, but + // we'll also turn off GOPROXY just for good measure. + if err := os.Setenv("GO111MODULE", "off"); err != nil { + log.Fatal(err) + } + if err := os.Setenv("GOPROXY", "off"); err != nil { + log.Fatal(err) + } +} + func Test(t *testing.T) { testdata := analysistest.TestData() analysistest.Run(t, testdata, pkgfact.Analyzer,