From 9f6d4ad827bbe70b5f5c8db2d3d279ea0a2767ad Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Sat, 17 Feb 2018 02:00:07 -0500 Subject: [PATCH] go/buildutil: use build.Import to find GOPATH containing x/tools Previously, the test assumed that golang.org/x/tools was always in the first GOPATH workspace. It may not be. Find it dynamically instead. Fixes golang/go#19400. Change-Id: I9c75d5ff1409aebd403d7ad4314b496fe1a04140 Reviewed-on: https://go-review.googlesource.com/94900 Run-TryBot: Dmitri Shuralyov TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- go/buildutil/util_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index 72db3172a..f7c26dd3c 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -8,7 +8,6 @@ import ( "go/build" "io/ioutil" "os" - "path/filepath" "runtime" "testing" @@ -18,7 +17,7 @@ import ( func TestContainingPackage(t *testing.T) { // unvirtualized: goroot := runtime.GOROOT() - gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] + gopath := gopathContainingTools(t) type Test struct { gopath, filename, wantPkg string @@ -70,3 +69,13 @@ 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 +}