go/buildutil: get tests to pass when run from a module
These tests tried to access the user's GOPATH, which can't work if you're
running from a module! So use packagestest to set up a fake GOPATH.
Also remove a TODO that's never going to be done because we won't
be making more investments in go/build.
Change-Id: I379c74345ace91e6c4282a42ff62106bf325d0e0
Reviewed-on: https://go-review.googlesource.com/c/162757
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/go/buildutil/allpackages_test.go b/go/buildutil/allpackages_test.go
index 1815512..ccdc31b 100644
--- a/go/buildutil/allpackages_test.go
+++ b/go/buildutil/allpackages_test.go
@@ -16,6 +16,7 @@
"testing"
"golang.org/x/tools/go/buildutil"
+ "golang.org/x/tools/go/packages/packagestest"
)
func TestAllPackages(t *testing.T) {
@@ -23,7 +24,24 @@
t.Skip("gccgo has no standard packages")
}
- all := buildutil.AllPackages(&build.Default)
+ exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
+ {Name: "golang.org/x/tools/go/buildutil", 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
+ all := buildutil.AllPackages(&buildContext)
set := make(map[string]bool)
for _, pkg := range all {
diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go
index c72d59d..e676130 100644
--- a/go/buildutil/util_test.go
+++ b/go/buildutil/util_test.go
@@ -8,10 +8,13 @@
"go/build"
"io/ioutil"
"os"
+ "path/filepath"
"runtime"
+ "strings"
"testing"
"golang.org/x/tools/go/buildutil"
+ "golang.org/x/tools/go/packages/packagestest"
)
func TestContainingPackage(t *testing.T) {
@@ -19,9 +22,22 @@
t.Skip("gccgo has no GOROOT")
}
- // unvirtualized:
+ exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
+ {Name: "golang.org/x/tools/go/buildutil", Files: packagestest.MustCopyFileTree(".")}})
+ defer exported.Cleanup()
+
goroot := runtime.GOROOT()
- gopath := gopathContainingTools(t)
+ 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)
+ }
+ buildutildir := filepath.Join(gopath, "golang.org", "x", "tools", "go", "buildutil")
type Test struct {
gopath, filename, wantPkg string
@@ -60,7 +76,7 @@
var got string
var buildContext = build.Default
buildContext.GOPATH = test.gopath
- bp, err := buildutil.ContainingPackage(&buildContext, ".", test.filename)
+ bp, err := buildutil.ContainingPackage(&buildContext, buildutildir, test.filename)
if err != nil {
got = "(not found)"
} else {
@@ -71,15 +87,4 @@
}
}
- // TODO(adonovan): test on virtualized GOPATH too.
-}
-
-// gopathContainingTools returns the path of the GOPATH workspace
-// with golang.org/x/tools, or fails the test if it can't locate it.
-func gopathContainingTools(t *testing.T) string {
- p, err := build.Import("golang.org/x/tools", "", build.FindOnly)
- if err != nil {
- t.Fatal(err)
- }
- return p.Root
}