From 12f59dd68bb9d5f5731a281b455695a12e43730b Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Thu, 14 Feb 2019 14:13:34 -0500 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- refactor/importgraph/graph_test.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/refactor/importgraph/graph_test.go b/refactor/importgraph/graph_test.go index 52dd769590..e342323217 100644 --- a/refactor/importgraph/graph_test.go +++ b/refactor/importgraph/graph_test.go @@ -11,8 +11,10 @@ package importgraph_test import ( "go/build" "sort" + "strings" "testing" + "golang.org/x/tools/go/packages/packagestest" "golang.org/x/tools/refactor/importgraph" _ "crypto/hmac" // just for test, below @@ -21,7 +23,25 @@ import ( const this = "golang.org/x/tools/refactor/importgraph" func TestBuild(t *testing.T) { - forward, reverse, errors := importgraph.Build(&build.Default) + exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{ + {Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}}) + defer exported.Cleanup() + + var gopath string + for _, env := range exported.Config.Env { + if !strings.HasPrefix(env, "GOPATH=") { + continue + } + gopath = strings.TrimPrefix(env, "GOPATH=") + } + if gopath == "" { + t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env) + } + + var buildContext = build.Default + buildContext.GOPATH = gopath + + forward, reverse, errors := importgraph.Build(&buildContext) // Test direct edges. // We throw in crypto/hmac to prove that external test files