Skip to content

Commit 12f59dd

Browse files
committed
refactor/importgraph: get test to pass when run in modules mode
Use packagestest to copy the code in the test directory to a GOPATH in a temporary directory, so the test doesn't depend on accessing the user's GOPATH. Change-Id: I5895ad97ab478625048fdcd330ef516df9c428d3 Reviewed-on: https://go-review.googlesource.com/c/162777 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 4ea155d commit 12f59dd

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

refactor/importgraph/graph_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ package importgraph_test
1111
import (
1212
"go/build"
1313
"sort"
14+
"strings"
1415
"testing"
1516

17+
"golang.org/x/tools/go/packages/packagestest"
1618
"golang.org/x/tools/refactor/importgraph"
1719

1820
_ "crypto/hmac" // just for test, below
@@ -21,7 +23,25 @@ import (
2123
const this = "golang.org/x/tools/refactor/importgraph"
2224

2325
func TestBuild(t *testing.T) {
24-
forward, reverse, errors := importgraph.Build(&build.Default)
26+
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
27+
{Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}})
28+
defer exported.Cleanup()
29+
30+
var gopath string
31+
for _, env := range exported.Config.Env {
32+
if !strings.HasPrefix(env, "GOPATH=") {
33+
continue
34+
}
35+
gopath = strings.TrimPrefix(env, "GOPATH=")
36+
}
37+
if gopath == "" {
38+
t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
39+
}
40+
41+
var buildContext = build.Default
42+
buildContext.GOPATH = gopath
43+
44+
forward, reverse, errors := importgraph.Build(&buildContext)
2545

2646
// Test direct edges.
2747
// We throw in crypto/hmac to prove that external test files

0 commit comments

Comments
 (0)