Skip to content

Commit

Permalink
Merge pull request #297 from RossHammer/master
Browse files Browse the repository at this point in the history
Fixing #291 where a different import with the same name as the package would not get included in the mock
  • Loading branch information
LandonTClipp committed Jun 28, 2020
2 parents 56b6eee + 7ba373f commit ea0e8fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
7 changes: 1 addition & 6 deletions pkg/generator.go
Expand Up @@ -259,17 +259,12 @@ 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 @@ -659,7 +654,7 @@ func (g *Generator) generateCalled(list *paramList, formattedParamNames string)
}

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

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

const pkg = "test"
const pkg = "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.NotContains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
s.Contains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
}

func (s *GeneratorSuite) TestMapToInterface() {
Expand Down Expand Up @@ -1099,6 +1099,39 @@ 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

0 comments on commit ea0e8fe

Please sign in to comment.