cmd/gomvpkg: Only process paths that are children

When determining children of a parent directory, look for the expected /
to avoid processing directories that look similar but are not direct
descendants.

Fixes golang/go#21991

Change-Id: I5c7d18076540e5588a856845f04645231856196f
Reviewed-on: https://go-review.googlesource.com/65672
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Michael Fraenkel 2017-09-24 10:03:04 -04:00 committed by Michael Matloob
parent a5d79d28aa
commit af2bfe26b6
2 changed files with 26 additions and 5 deletions

View File

@ -145,7 +145,9 @@ func subpackages(ctxt *build.Context, srcDir string, dir string) map[string]bool
log.Fatalf("unexpected error in ForEachPackage: %v", err) log.Fatalf("unexpected error in ForEachPackage: %v", err)
} }
if !strings.HasPrefix(pkg, path.Join(dir, "")) { // Only process the package or a sub-package
if !(strings.HasPrefix(pkg, dir) &&
(len(pkg) == len(dir) || pkg[len(dir)] == '/')) {
return return
} }

View File

@ -214,6 +214,23 @@ var _ a.T
}, },
}, },
// References into subpackages where directories have overlapped names
{
ctxt: fakeContext(map[string][]string{
"foo": {},
"foo/a": {`package a`},
"foo/aa": {`package bar`},
"foo/c": {`package c; import _ "foo/bar";`},
}),
from: "foo/a", to: "foo/spam",
want: map[string]string{
"/go/src/foo/spam/0.go": `package spam
`,
"/go/src/foo/aa/0.go": `package bar`,
"/go/src/foo/c/0.go": `package c; import _ "foo/bar";`,
},
},
// External test packages // External test packages
{ {
ctxt: buildutil.FakeContext(map[string]map[string]string{ ctxt: buildutil.FakeContext(map[string]map[string]string{
@ -398,11 +415,13 @@ var _ foo.T
} }
moveDirectory = func(from, to string) error { moveDirectory = func(from, to string) error {
for path, contents := range got { for path, contents := range got {
if strings.HasPrefix(path, from) { if !(strings.HasPrefix(path, from) &&
newPath := strings.Replace(path, from, to, 1) (len(path) == len(from) || path[len(from)] == filepath.Separator)) {
delete(got, path) continue
got[newPath] = contents
} }
newPath := strings.Replace(path, from, to, 1)
delete(got, path)
got[newPath] = contents
} }
return nil return nil
} }