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 <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Michael Matloob 2019-02-14 14:13:34 -05:00
parent 4ea155ddbd
commit 12f59dd68b

View File

@ -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