mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
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>
This commit is contained in:
parent
c161412db0
commit
4ea155ddbd
@ -16,6 +16,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"golang.org/x/tools/go/buildutil"
|
||||
"golang.org/x/tools/go/packages/packagestest"
|
||||
)
|
||||
|
||||
func TestAllPackages(t *testing.T) {
|
||||
@ -23,7 +24,24 @@ func TestAllPackages(t *testing.T) {
|
||||
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 {
|
||||
|
@ -8,10 +8,13 @@ import (
|
||||
"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 @@ func TestContainingPackage(t *testing.T) {
|
||||
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 @@ func TestContainingPackage(t *testing.T) {
|
||||
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 @@ func TestContainingPackage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user