Skip to content

Commit ea0e8fe

Browse files
authored
Merge pull request #297 from RossHammer/master
Fixing #291 where a different import with the same name as the package would not get included in the mock
2 parents 56b6eee + 7ba373f commit ea0e8fe

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

pkg/generator.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,12 @@ func (g *Generator) generateImports(ctx context.Context) {
259259
log.Debug().Msgf("generating imports")
260260
log.Debug().Msgf("%v", g.nameToPackagePath)
261261

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

268267
path := g.nameToPackagePath[name]
269-
if g.InPackage && path == pkgPath {
270-
logImport.Debug().Msgf("import (%s) equals interface's package path (%s), skipping", path, pkgPath)
271-
continue
272-
}
273268
g.printf("import %s \"%s\"\n", name, path)
274269
}
275270
}
@@ -659,7 +654,7 @@ func (g *Generator) generateCalled(list *paramList, formattedParamNames string)
659654
}
660655

661656
func (g *Generator) Write(w io.Writer) error {
662-
opt := &imports.Options{Comments: true}
657+
opt := &imports.Options{Comments: true, FormatOnly: true}
663658
theBytes := g.buf.Bytes()
664659

665660
res, err := imports.Process("mock.go", theBytes, opt)

pkg/generator_test.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/vektra/mockery/pkg/config"
1515
)
1616

17-
const pkg = "test"
17+
const pkg = "pkg/test"
1818

1919
type GeneratorSuite struct {
2020
suite.Suite
@@ -962,7 +962,7 @@ func (s *GeneratorSuite) TestGeneratorWithImportSameAsLocalPackageInpkgNoCycle()
962962
InPackage: true,
963963
}, iface, pkg)
964964
gen.GeneratePrologue(s.ctx, pkg)
965-
s.NotContains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
965+
s.Contains(gen.buf.String(), `import test "github.com/vektra/mockery/pkg/fixtures/test"`)
966966
}
967967

968968
func (s *GeneratorSuite) TestMapToInterface() {
@@ -1099,6 +1099,39 @@ func (_m *A) Call() (test.B, error) {
10991099
)
11001100
}
11011101

1102+
func (s *GeneratorSuite) TestGeneratorForStructValueReturnInPackage() {
1103+
expected := `// MockA is an autogenerated mock type for the A type
1104+
type MockA struct {
1105+
mock.Mock
1106+
}
1107+
1108+
// Call provides a mock function with given fields:
1109+
func (_m *MockA) Call() (B, error) {
1110+
ret := _m.Called()
1111+
1112+
var r0 B
1113+
if rf, ok := ret.Get(0).(func() B); ok {
1114+
r0 = rf()
1115+
} else {
1116+
r0 = ret.Get(0).(B)
1117+
}
1118+
1119+
var r1 error
1120+
if rf, ok := ret.Get(1).(func() error); ok {
1121+
r1 = rf()
1122+
} else {
1123+
r1 = ret.Error(1)
1124+
}
1125+
1126+
return r0, r1
1127+
}
1128+
`
1129+
s.checkGeneration(
1130+
filepath.Join(fixturePath, "struct_value.go"), "A", true, "",
1131+
expected,
1132+
)
1133+
}
1134+
11021135
func (s *GeneratorSuite) TestStructNameOverride() {
11031136
expected := `// Requester2OverrideName is an autogenerated mock type for the Requester2 type
11041137
type Requester2OverrideName struct {

0 commit comments

Comments
 (0)