cmd/go: fix handling of vendored imports in foo_test.go files

Fixes #11977.
Fixes #11988.

Change-Id: I9f80006946d3752ee6d644ee51f2decfeaca1ff6
Reviewed-on: https://go-review.googlesource.com/13230
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2015-08-05 12:09:05 -04:00
parent 98aa82287f
commit 0fb87abddf
4 changed files with 86 additions and 18 deletions

View file

@ -175,17 +175,9 @@ func runList(cmd *Command, args []string) {
}
for _, pkg := range load(args) {
// We apply vendoredImportPath here for test imports.
// It's not needed for regular imports, because it was
// done while loading the package.
for i, path := range pkg.TestImports {
path, _ = vendoredImportPath(pkg, path)
pkg.TestImports[i] = path
}
for i, path := range pkg.XTestImports {
path, _ = vendoredImportPath(pkg, path)
pkg.XTestImports[i] = path
}
// Show vendor-expanded paths in listing
pkg.TestImports = pkg.vendored(pkg.TestImports)
pkg.XTestImports = pkg.vendored(pkg.XTestImports)
do(pkg)
}

View file

@ -101,6 +101,33 @@ type Package struct {
gobinSubdir bool // install target would be subdir of GOBIN
}
// vendored returns the vendor-resolved version of imports,
// which should be p.TestImports or p.XTestImports, NOT p.Imports.
// The imports in p.TestImports and p.XTestImports are not recursively
// loaded during the initial load of p, so they list the imports found in
// the source file, but most processing should be over the vendor-resolved
// import paths. We do this resolution lazily both to avoid file system work
// and because the eventual real load of the test imports (during 'go test')
// can produce better error messages if it starts with the original paths.
// The initial load of p loads all the non-test imports and rewrites
// the vendored paths, so nothing should ever call p.vendored(p.Imports).
func (p *Package) vendored(imports []string) []string {
if len(imports) > 0 && len(p.Imports) > 0 && &imports[0] == &p.Imports[0] {
panic("internal error: p.vendored(p.Imports) called")
}
seen := make(map[string]bool)
var all []string
for _, path := range imports {
path, _ = vendoredImportPath(p, path)
if !seen[path] {
seen[path] = true
all = append(all, path)
}
}
sort.Strings(all)
return all
}
// CoverVar holds the name of the generated coverage variables targeting the named file.
type CoverVar struct {
File string // local file name

View file

@ -384,10 +384,10 @@ func runTest(cmd *Command, args []string) {
for _, path := range p.Imports {
deps[path] = true
}
for _, path := range p.TestImports {
for _, path := range p.vendored(p.TestImports) {
deps[path] = true
}
for _, path := range p.XTestImports {
for _, path := range p.vendored(p.XTestImports) {
deps[path] = true
}
}
@ -611,10 +611,6 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
stk.push(p.ImportPath + "_test")
pxtestNeedsPtest := false
for i, path := range p.XTestImports {
if path == p.ImportPath {
pxtestNeedsPtest = true
continue
}
p1 := loadImport(path, p.Dir, p, &stk, p.build.XTestImportPos[path], useVendor)
if p1.Error != nil {
return nil, nil, nil, p1.Error
@ -624,7 +620,11 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
err.Pos = "" // show full import stack
return nil, nil, nil, err
}
ximports = append(ximports, p1)
if p1.ImportPath == p.ImportPath {
pxtestNeedsPtest = true
} else {
ximports = append(ximports, p1)
}
p.XTestImports[i] = p1.ImportPath
}
stk.pop()

View file

@ -195,3 +195,52 @@ func TestVendorCache(t *testing.T) {
tg.runFail("build", "p")
tg.grepStderr("must be imported as x", "did not fail to build p")
}
func TestVendorTest2(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
tg := testgo(t)
defer tg.cleanup()
tg.makeTempdir()
tg.setenv("GOPATH", tg.path("."))
tg.setenv("GO15VENDOREXPERIMENT", "1")
tg.run("get", "github.com/rsc/go-get-issue-11864")
// build -i should work
tg.run("build", "-i", "github.com/rsc/go-get-issue-11864")
tg.run("build", "-i", "github.com/rsc/go-get-issue-11864/t")
// test -i should work like build -i (golang.org/issue/11988)
tg.run("test", "-i", "github.com/rsc/go-get-issue-11864")
tg.run("test", "-i", "github.com/rsc/go-get-issue-11864/t")
// test should work too
tg.run("test", "github.com/rsc/go-get-issue-11864")
tg.run("test", "github.com/rsc/go-get-issue-11864/t")
// external tests should observe internal test exports (golang.org/issue/11977)
tg.run("test", "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx2")
}
func TestVendorList(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
tg := testgo(t)
defer tg.cleanup()
tg.makeTempdir()
tg.setenv("GOPATH", tg.path("."))
tg.setenv("GO15VENDOREXPERIMENT", "1")
tg.run("get", "github.com/rsc/go-get-issue-11864")
tg.run("list", "-f", `{{join .TestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/t")
tg.grepStdout("go-get-issue-11864/vendor/vendor.org/p", "did not find vendor-expanded p")
tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/tx")
tg.grepStdout("go-get-issue-11864/vendor/vendor.org/p", "did not find vendor-expanded p")
tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx2")
tg.grepStdout("go-get-issue-11864/vendor/vendor.org/tx2", "did not find vendor-expanded tx2")
tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx3")
tg.grepStdout("go-get-issue-11864/vendor/vendor.org/tx3", "did not find vendor-expanded tx3")
}