1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00

internal/imports: suggest x for x_tests

golang.org/cl/170238 forgot that it's okay for an x_test to import a
package in the same directory. Only skip the candidate if the package
we're looking for has the same name as the one being fixed.

Fixes golang/go#32440

Change-Id: I7806d9f4174ca217ac83887da5e23b28cd102095
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181338
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Heschi Kreinick 2019-06-07 17:21:57 -04:00
parent e88138204b
commit 0945d3616f
2 changed files with 37 additions and 6 deletions

View File

@ -702,7 +702,7 @@ func addExternalCandidates(pass *pass, refs references, filename string) error {
go func(pkgName string, symbols map[string]bool) {
defer wg.Done()
found, err := findImport(ctx, pass.env, dirScan, pkgName, symbols, filename)
found, err := findImport(ctx, pass, dirScan, pkgName, symbols, filename)
if err != nil {
firstErrOnce.Do(func() {
@ -1028,7 +1028,7 @@ func loadExports(ctx context.Context, env *ProcessEnv, expectPackage string, pkg
// findImport searches for a package with the given symbols.
// If no package is found, findImport returns ("", false, nil)
func findImport(ctx context.Context, env *ProcessEnv, dirScan []*pkg, pkgName string, symbols map[string]bool, filename string) (*pkg, error) {
func findImport(ctx context.Context, pass *pass, dirScan []*pkg, pkgName string, symbols map[string]bool, filename string) (*pkg, error) {
pkgDir, err := filepath.Abs(filename)
if err != nil {
return nil, err
@ -1038,7 +1038,12 @@ func findImport(ctx context.Context, env *ProcessEnv, dirScan []*pkg, pkgName st
// Find candidate packages, looking only at their directory names first.
var candidates []pkgDistance
for _, pkg := range dirScan {
if pkg.dir != pkgDir && pkgIsCandidate(filename, pkgName, pkg) {
if pkg.dir == pkgDir && pass.f.Name.Name == pkgName {
// The candidate is in the same directory and has the
// same package name. Don't try to import ourselves.
continue
}
if pkgIsCandidate(filename, pkgName, pkg) {
candidates = append(candidates, pkgDistance{
pkg: pkg,
distance: distance(pkgDir, pkg.dir),
@ -1051,7 +1056,7 @@ func findImport(ctx context.Context, env *ProcessEnv, dirScan []*pkg, pkgName st
// ones. Note that this sorts by the de-vendored name, so
// there's no "penalty" for vendoring.
sort.Sort(byDistanceOrImportPathShortLength(candidates))
if env.Debug {
if pass.env.Debug {
for i, c := range candidates {
log.Printf("%s candidate %d/%d: %v in %v", pkgName, i+1, len(candidates), c.pkg.importPathShort, c.pkg.dir)
}
@ -1090,9 +1095,9 @@ func findImport(ctx context.Context, env *ProcessEnv, dirScan []*pkg, pkgName st
wg.Done()
}()
exports, err := loadExports(ctx, env, pkgName, c.pkg)
exports, err := loadExports(ctx, pass.env, pkgName, c.pkg)
if err != nil {
if env.Debug {
if pass.env.Debug {
log.Printf("loading exports in dir %s (seeking package %s): %v", c.pkg.dir, pkgName, err)
}
resc <- nil

View File

@ -2116,6 +2116,32 @@ const _ = pkg.X
}.processTest(t, "foo.com", "pkg/b.go", nil, nil, want)
}
func TestExternalTestImportsPackageUnderTest(t *testing.T) {
const provide = `package pkg
func DoIt(){}
`
const input = `package pkg_test
var _ = pkg.DoIt`
const want = `package pkg_test
import "foo.com/pkg"
var _ = pkg.DoIt
`
testConfig{
module: packagestest.Module{
Name: "foo.com",
Files: fm{
"pkg/provide.go": provide,
"pkg/x_test.go": input,
},
},
}.processTest(t, "foo.com", "pkg/x_test.go", nil, nil, want)
}
func TestPkgIsCandidate(t *testing.T) {
tests := []struct {
name string