Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively check unique pkg names against existing imports #186

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *Registry) AddImport(pkg *types.Package) *Package {
imprt := Package{pkg: pkg, Alias: r.aliases[path]}

if conflict, ok := r.searchImport(imprt.Qualifier()); ok {
resolveImportConflict(&imprt, conflict, 0)
r.resolveImportConflict(&imprt, conflict, 0)
}

r.imports[path] = &imprt
Expand Down Expand Up @@ -126,6 +126,30 @@ func (r Registry) searchImport(name string) (*Package, bool) {
return nil, false
}

// resolveImportConflict generates and assigns a unique alias for
// packages with conflicting qualifiers.
func (r Registry) resolveImportConflict(a, b *Package, lvl int) {
sudo-suhas marked this conversation as resolved.
Show resolved Hide resolved
if a.uniqueName(lvl) == b.uniqueName(lvl) {
r.resolveImportConflict(a, b, lvl+1)
return
}

for _, p := range []*Package{a, b} {
name := p.uniqueName(lvl)
// Even though the name is not conflicting with the other package we
// got, the new name we want to pick might already be taken. So check
// again for conflicts and resolve them as well. Since the name for
// this package would also get set in the recursive function call, skip
// setting the alias after it.
if conflict, ok := r.searchImport(name); ok && conflict != p {
r.resolveImportConflict(p, conflict, lvl+1)
continue
}

p.Alias = name
}
}

func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package, error) {
pkgs, err := packages.Load(&packages.Config{
Mode: mode,
Expand Down Expand Up @@ -182,15 +206,3 @@ func parseImportsAliases(pkg *packages.Package) map[string]string {
}
return aliases
}

// resolveImportConflict generates and assigns a unique alias for
// packages with conflicting qualifiers.
func resolveImportConflict(a, b *Package, lvl int) {
u1, u2 := a.uniqueName(lvl), b.uniqueName(lvl)
if u1 != u2 {
a.Alias, b.Alias = u1, u2
return
}

resolveImportConflict(a, b, lvl+1)
}
6 changes: 6 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"GenericStore1", "GenericStore2", "AliasStore"},
goldenFile: filepath.Join("testpackages/generics", "generics_moq.golden.go"),
},
{
name: "TransientImport",
cfg: Config{SrcDir: "testpackages/transientimport"},
interfaces: []string{"Transient"},
goldenFile: filepath.Join("testpackages/transientimport", "transient_moq.golden.go"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/moq/testpackages/transientimport/base/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package base

import (
four "github.com/matryer/moq/pkg/moq/testpackages/transientimport/four/app/v1"
one "github.com/matryer/moq/pkg/moq/testpackages/transientimport/one/v1"
"github.com/matryer/moq/pkg/moq/testpackages/transientimport/onev1"
three "github.com/matryer/moq/pkg/moq/testpackages/transientimport/three/v1"
two "github.com/matryer/moq/pkg/moq/testpackages/transientimport/two/app/v1"
)

type Transient interface {
DoSomething(onev1.Zero, one.One, two.Two, three.Three, four.Four)
}
3 changes: 3 additions & 0 deletions pkg/moq/testpackages/transientimport/four/app/v1/four.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v1

type Four string
3 changes: 3 additions & 0 deletions pkg/moq/testpackages/transientimport/one/v1/one.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v1

type One string
3 changes: 3 additions & 0 deletions pkg/moq/testpackages/transientimport/onev1/zero.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package onev1

type Zero string
3 changes: 3 additions & 0 deletions pkg/moq/testpackages/transientimport/three/v1/three.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v1

type Three string
7 changes: 7 additions & 0 deletions pkg/moq/testpackages/transientimport/transient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package transientimport

import (
"github.com/matryer/moq/pkg/moq/testpackages/transientimport/base"
)

type Transient = base.Transient
103 changes: 103 additions & 0 deletions pkg/moq/testpackages/transientimport/transient_moq.golden.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/moq/testpackages/transientimport/two/app/v1/two.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package v1

type Two string