Skip to content

Commit

Permalink
Fix: avoid package name collision with inPackage (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilia Sevostianov authored and i-sevostyanov committed Apr 21, 2022
1 parent 58a7f18 commit b8c62f7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
33 changes: 33 additions & 0 deletions mocks/pkg/fixtures/example_project/bar/foo/Client.go

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

29 changes: 29 additions & 0 deletions mocks/pkg/fixtures/example_project/foo/Collision.go

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

5 changes: 5 additions & 0 deletions pkg/fixtures/example_project/bar/foo/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo

type Client interface {
Search(query string) ([]string, error)
}
7 changes: 7 additions & 0 deletions pkg/fixtures/example_project/foo/collision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package foo

import "github.com/vektra/mockery/v2/pkg/fixtures/example_project/bar/foo"

type Collision interface {
NewClient() foo.Client
}
10 changes: 8 additions & 2 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func (g *Generator) addPackageImportWithName(ctx context.Context, path, name str

func (g *Generator) getNonConflictingName(path, name string) string {
if !g.importNameExists(name) {
return name
// do not allow imports with the same name as the package when inPackage
if !g.InPackage || g.iface.Pkg.Name() != name {
return name
}
}

// The path will always contain '/' because it is enforced in getLocalizedPath
Expand All @@ -124,7 +127,10 @@ func (g *Generator) getNonConflictingName(path, name string) string {
for i := 1; i <= numDirectories; i++ {
prospectiveName = strings.Join(cleanedDirectories[numDirectories-i:], "")
if !g.importNameExists(prospectiveName) {
return prospectiveName
// do not allow imports with the same name as the package when inPackage
if !g.InPackage || g.iface.Pkg.Name() != prospectiveName {
return prospectiveName
}
}
}
// Try adding numbers to the given name
Expand Down
20 changes: 18 additions & 2 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1774,15 +1774,15 @@ func (s *GeneratorSuite) TestKeepTreeInPackageCombined() {
tests := []testData{
{path: filepath.Join("example_project", "root.go"), name: "Root", expected: `package example_project
import example_project "github.com/vektra/mockery/v2/pkg/fixtures/example_project"
import fixturesexample_project "github.com/vektra/mockery/v2/pkg/fixtures/example_project"
import foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo"
import mock "github.com/stretchr/testify/mock"
import testing "testing"
`},
{path: filepath.Join("example_project", "foo", "foo.go"), name: "Foo", expected: `package foo
import foo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo"
import example_projectfoo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/foo"
import mock "github.com/stretchr/testify/mock"
import testing "testing"
Expand All @@ -1800,6 +1800,22 @@ import testing "testing"
}
}

func (s *GeneratorSuite) TestInPackagePackageCollision() {
expected := `package foo
import barfoo "github.com/vektra/mockery/v2/pkg/fixtures/example_project/bar/foo"
import mock "github.com/stretchr/testify/mock"
`
generator := NewGenerator(
s.ctx,
config.Config{InPackage: true, LogLevel: "debug"},
s.getInterfaceFromFile("example_project/foo/collision.go", "Collision"),
pkg,
)
s.checkPrologueGeneration(generator, expected)
}

func TestGeneratorSuite(t *testing.T) {
generatorSuite := new(GeneratorSuite)
suite.Run(t, generatorSuite)
Expand Down

0 comments on commit b8c62f7

Please sign in to comment.