From a43dd57ceb264a52d2f0c00202a660a192c33dd9 Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Sat, 27 Jun 2020 23:03:07 -0500 Subject: [PATCH] Revert "Fixing #291 where a different import with the same name as the package would not get included in the mock" --- pkg/generator.go | 7 ++++++- pkg/generator_test.go | 37 ++----------------------------------- 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/pkg/generator.go b/pkg/generator.go index 84aebaac..cd48a621 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -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) } } @@ -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) diff --git a/pkg/generator_test.go b/pkg/generator_test.go index 80c269e6..884764a0 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -14,7 +14,7 @@ import ( "github.com/vektra/mockery/pkg/config" ) -const pkg = "pkg/test" +const pkg = "test" type GeneratorSuite struct { suite.Suite @@ -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() { @@ -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 {