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

Fix: avoid package name collision with inPackage (#291) #422

Merged
merged 5 commits into from Apr 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
i-sevostyanov marked this conversation as resolved.
Show resolved Hide resolved
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