Skip to content

Commit

Permalink
Merge pull request #422 from i-sevostyanov/fix-package-collision
Browse files Browse the repository at this point in the history
Fix: avoid package name collision with inPackage (#291)
  • Loading branch information
LandonTClipp committed Apr 25, 2022
2 parents 58a7f18 + facf60b commit c9dc740
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 4 deletions.
47 changes: 47 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.

41 changes: 41 additions & 0 deletions mocks/pkg/fixtures/example_project/foo/PackageNameSameAsImport.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
@@ -0,0 +1,5 @@
package foo

type Client interface {
Search(query string) ([]string, error)
}
9 changes: 9 additions & 0 deletions pkg/fixtures/example_project/context/context.go
@@ -0,0 +1,9 @@
package context

import (
"context"
)

type CollideWithStdLib interface {
NewClient(ctx context.Context)
}
7 changes: 7 additions & 0 deletions pkg/fixtures/example_project/foo/pkg_name_same_as_import.go
@@ -0,0 +1,7 @@
package foo

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

type PackageNameSameAsImport interface {
NewClient() foo.Client
}
6 changes: 4 additions & 2 deletions pkg/generator.go
Expand Up @@ -106,7 +106,8 @@ func (g *Generator) addPackageImportWithName(ctx context.Context, path, name str
}

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

Expand All @@ -123,7 +124,8 @@ func (g *Generator) getNonConflictingName(path, name string) string {
var prospectiveName string
for i := 1; i <= numDirectories; i++ {
prospectiveName = strings.Join(cleanedDirectories[numDirectories-i:], "")
if !g.importNameExists(prospectiveName) {
if !g.importNameExists(prospectiveName) && (!g.InPackage || g.iface.Pkg.Name() != prospectiveName) {
// do not allow imports with the same name as the package when inPackage
return prospectiveName
}
}
Expand Down
38 changes: 36 additions & 2 deletions pkg/generator_test.go
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,40 @@ 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"
import testing "testing"
`
generator := NewGenerator(
s.ctx,
config.Config{InPackage: true, LogLevel: "debug"},
s.getInterfaceFromFile("example_project/foo/pkg_name_same_as_import.go", "PackageNameSameAsImport"),
pkg,
)
s.checkPrologueGeneration(generator, expected)
}

func (s *GeneratorSuite) TestImportCollideWithStdLib() {
expected := `package context
import context2 "context"
import mock "github.com/stretchr/testify/mock"
import testing "testing"
`
generator := NewGenerator(
s.ctx,
config.Config{InPackage: true, LogLevel: "debug"},
s.getInterfaceFromFile("example_project/context/context.go", "CollideWithStdLib"),
pkg,
)
s.checkPrologueGeneration(generator, expected)
}

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

0 comments on commit c9dc740

Please sign in to comment.