diff --git a/refactor/rename/mvpkg.go b/refactor/rename/mvpkg.go index bcdbf8bd..bb0d9b06 100644 --- a/refactor/rename/mvpkg.go +++ b/refactor/rename/mvpkg.go @@ -241,6 +241,30 @@ func (m *mover) move() error { f.Name.Name = newName // change package decl filesToUpdate[f] = true } + + // Look through the external test packages (m.iprog.Created contains the external test packages). + for _, info := range m.iprog.Created { + // Change the "package" declaration of the external test package. + if info.Pkg.Path() == m.from+"_test" { + for _, f := range info.Files { + f.Name.Name = newName + "_test" // change package decl + filesToUpdate[f] = true + } + } + + // Mark all the loaded external test packages, which import the "from" package, + // as affected packages and update the imports. + for _, imp := range info.Pkg.Imports() { + if imp.Path() == m.from { + m.affectedPackages[info.Pkg.Path()] = true + m.iprog.Imported[info.Pkg.Path()] = info + if err := importName(m.iprog, info, m.from, path.Base(m.from), newName); err != nil { + return err + } + } + } + } + // Update imports of that package to use the new import name. // None of the subpackages will change their name---only the from package // itself will. diff --git a/refactor/rename/mvpkg_test.go b/refactor/rename/mvpkg_test.go index 3c915b44..61fa354c 100644 --- a/refactor/rename/mvpkg_test.go +++ b/refactor/rename/mvpkg_test.go @@ -203,6 +203,38 @@ var _ a.T import "bar/a" var _ a.T +`, + }, + }, + + // External test packages + { + ctxt: buildutil.FakeContext(map[string]map[string]string{ + "foo": { + "0.go": `package foo; type T int`, + "0_test.go": `package foo_test; import "foo"; var _ foo.T`, + }, + "baz": { + "0_test.go": `package baz_test; import "foo"; var _ foo.T`, + }, + }), + from: "foo", to: "bar", + want: map[string]string{ + "/go/src/bar/0.go": `package bar + +type T int +`, + "/go/src/bar/0_test.go": `package bar_test + +import "bar" + +var _ bar.T +`, + "/go/src/baz/0_test.go": `package baz_test + +import "bar" + +var _ bar.T `, }, },