go/buildutil: changes for vendor support

Add AllowVendor constant for convenience across x/tools.
 (Will delete go/loader's importMode later)
Add StripVendor utility function + test.

Change-Id: I885076cb4ea67588996d85b749b85f49cd619f0d
Reviewed-on: https://go-review.googlesource.com/18049
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Alan Donovan 2015-12-18 13:19:00 -05:00 committed by Alan Donovan
parent 42bc1282f3
commit 27534aa951
5 changed files with 62 additions and 2 deletions

View File

@ -18,9 +18,10 @@ import (
"sync" "sync"
) )
// AllPackages returns the import path of each Go package in any source // AllPackages returns the package path of each Go package in any source
// directory of the specified build context (e.g. $GOROOT or an element // directory of the specified build context (e.g. $GOROOT or an element
// of $GOPATH). Errors are ignored. The results are sorted. // of $GOPATH). Errors are ignored. The results are sorted.
// All package paths are canonical, and thus may contain "/vendor/".
// //
// The result may include import paths for directories that contain no // The result may include import paths for directories that contain no
// *.go files, such as "archive" (in $GOROOT/src). // *.go files, such as "archive" (in $GOROOT/src).
@ -37,9 +38,10 @@ func AllPackages(ctxt *build.Context) []string {
return list return list
} }
// ForEachPackage calls the found function with the import path of // ForEachPackage calls the found function with the package path of
// each Go package it finds in any source directory of the specified // each Go package it finds in any source directory of the specified
// build context (e.g. $GOROOT or an element of $GOPATH). // build context (e.g. $GOROOT or an element of $GOPATH).
// All package paths are canonical, and thus may contain "/vendor/".
// //
// If the package directory exists but could not be read, the second // If the package directory exists but could not be read, the second
// argument to the found function provides the error. // argument to the found function provides the error.

View File

@ -0,0 +1,14 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !go1.6
package buildutil
import "go/build"
// AllowVendor is a synonym for zero.
// It allows applications to refer to the go/build.AllowVendor
// feature whether or not it is supported.
const AllowVendor build.ImportMode = 0

View File

@ -0,0 +1,14 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build go1.6
package buildutil
import "go/build"
// AllowVendor is a synonym for go/build.AllowVendor.
// It allows applications to refer to the AllowVendor
// feature whether or not it is supported.
const AllowVendor build.ImportMode = build.AllowVendor

View File

@ -90,6 +90,15 @@ func dirHasPrefix(dir, prefix string) bool {
return len(dir) >= len(prefix) && strings.EqualFold(dir[:len(prefix)], prefix) return len(dir) >= len(prefix) && strings.EqualFold(dir[:len(prefix)], prefix)
} }
// StripVendor removes the "vendor" segment and all preceding ones
// from a slash-segmented path. (See go/build.AllowVendor.)
func StripVendor(path string) string {
if i := strings.LastIndex(path, "/vendor/"); i >= 0 {
return path[i+len("/vendor/"):]
}
return strings.TrimPrefix(path, "vendor/")
}
// -- Effective methods of file system interface ------------------------- // -- Effective methods of file system interface -------------------------
// (go/build.Context defines these as methods, but does not export them.) // (go/build.Context defines these as methods, but does not export them.)

View File

@ -29,6 +29,8 @@ func TestContainingPackage(t *testing.T) {
{goroot + "/src/encoding/missing/foo.go", "(not found)"}, {goroot + "/src/encoding/missing/foo.go", "(not found)"},
{gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", {gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go",
"golang.org/x/tools/go/buildutil"}, "golang.org/x/tools/go/buildutil"},
{gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go",
"vendor/golang.org/x/net/http2/hpack"},
} { } {
file, want := test[0], test[1] file, want := test[0], test[1]
bp, err := buildutil.ContainingPackage(&build.Default, ".", file) bp, err := buildutil.ContainingPackage(&build.Default, ".", file)
@ -43,3 +45,22 @@ func TestContainingPackage(t *testing.T) {
// TODO(adonovan): test on virtualized GOPATH too. // TODO(adonovan): test on virtualized GOPATH too.
} }
func TestStripVendor(t *testing.T) {
for _, test := range []struct {
path, want string
}{
{"", ""},
{"a", "a"},
{"a/b", "a/b"},
{"a/vendor/b", "b"},
{"a/b/vendor/c/d", "c/d"},
{"vendor/a/b", "a/b"},
{"a/vendor", "a/vendor"},
} {
if got := buildutil.StripVendor(test.path); got != test.want {
t.Errorf("StripVendor(%q) = %q, want %q",
test.path, got, test.want)
}
}
}