mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
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:
parent
4ea155ddbd
commit
12f59dd68b
@ -11,8 +11,10 @@ package importgraph_test
|
|||||||
import (
|
import (
|
||||||
"go/build"
|
"go/build"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/tools/go/packages/packagestest"
|
||||||
"golang.org/x/tools/refactor/importgraph"
|
"golang.org/x/tools/refactor/importgraph"
|
||||||
|
|
||||||
_ "crypto/hmac" // just for test, below
|
_ "crypto/hmac" // just for test, below
|
||||||
@ -21,7 +23,25 @@ import (
|
|||||||
const this = "golang.org/x/tools/refactor/importgraph"
|
const this = "golang.org/x/tools/refactor/importgraph"
|
||||||
|
|
||||||
func TestBuild(t *testing.T) {
|
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.
|
// Test direct edges.
|
||||||
// We throw in crypto/hmac to prove that external test files
|
// We throw in crypto/hmac to prove that external test files
|
||||||
|
Loading…
x
Reference in New Issue
Block a user