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

Revert "Fixing #291 where a different import with the same name as the package would not get included in the mock" #303

Merged
merged 1 commit into from
Jun 28, 2020
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
7 changes: 6 additions & 1 deletion pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,17 @@ func (g *Generator) generateImports(ctx context.Context) {
log.Debug().Msgf("generating imports")
log.Debug().Msgf("%v", g.nameToPackagePath)

pkgPath := g.nameToPackagePath[g.iface.Pkg.Name()]
// Sort by import name so that we get a deterministic order
for _, name := range g.sortedImportNames() {
logImport := log.With().Str(logging.LogKeyImport, g.nameToPackagePath[name]).Logger()
logImport.Debug().Msgf("found import")

path := g.nameToPackagePath[name]
if g.InPackage && path == pkgPath {
logImport.Debug().Msgf("import (%s) equals interface's package path (%s), skipping", path, pkgPath)
continue
}
g.printf("import %s \"%s\"\n", name, path)
}
}
Expand Down Expand Up @@ -654,7 +659,7 @@ func (g *Generator) generateCalled(list *paramList, formattedParamNames string)
}

func (g *Generator) Write(w io.Writer) error {
opt := &imports.Options{Comments: true, FormatOnly: true}
opt := &imports.Options{Comments: true}
theBytes := g.buf.Bytes()

res, err := imports.Process("mock.go", theBytes, opt)
Expand Down
37 changes: 2 additions & 35 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/vektra/mockery/pkg/config"
)

const pkg = "pkg/test"
const pkg = "test"

type GeneratorSuite struct {
suite.Suite
Expand Down Expand Up @@ -962,7 +962,7 @@ func (s *GeneratorSuite) TestGeneratorWithImportSameAsLocalPackageInpkgNoCycle()
InPackage: true,
}, iface, pkg)
gen.GeneratePrologue(s.ctx, pkg)
s.Contains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
s.NotContains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
}

func (s *GeneratorSuite) TestMapToInterface() {
Expand Down Expand Up @@ -1099,39 +1099,6 @@ func (_m *A) Call() (test.B, error) {
)
}

func (s *GeneratorSuite) TestGeneratorForStructValueReturnInPackage() {
expected := `// MockA is an autogenerated mock type for the A type
type MockA struct {
mock.Mock
}

// Call provides a mock function with given fields:
func (_m *MockA) Call() (B, error) {
ret := _m.Called()

var r0 B
if rf, ok := ret.Get(0).(func() B); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(B)
}

var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}

return r0, r1
}
`
s.checkGeneration(
filepath.Join(fixturePath, "struct_value.go"), "A", true, "",
expected,
)
}

func (s *GeneratorSuite) TestStructNameOverride() {
expected := `// Requester2OverrideName is an autogenerated mock type for the Requester2 type
type Requester2OverrideName struct {
Expand Down