Skip to content

Commit

Permalink
Merge pull request #143 from kingofsouth/inpkg-bug
Browse files Browse the repository at this point in the history
Fixed bug in inpkg where generated source would import itself
  • Loading branch information
evanphx authored May 19, 2017
2 parents e4fa388 + 95b0a31 commit 1401627
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
11 changes: 8 additions & 3 deletions mockery/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,13 @@ func (g *Generator) sortedImportNames() (importNames []string) {
}

func (g *Generator) generateImports() {
pkgPath := g.nameToPackagePath[g.iface.Pkg.Name()]
// Sort by import name so that we get a deterministic order
for _, name := range g.sortedImportNames() {
path := g.nameToPackagePath[name]
if g.ip && path == pkgPath {
continue
}
g.printf("import %s \"%s\"\n", name, path)
}
}
Expand Down Expand Up @@ -312,11 +316,11 @@ type namer interface {
Name() string
}

func (g *Generator) renderType(t types.Type) string {
switch t := t.(type) {
func (g *Generator) renderType(typ types.Type) string {
switch t := typ.(type) {
case *types.Named:
o := t.Obj()
if o.Pkg() == nil || o.Pkg().Name() == "main" || (g.ip && o.Pkg().Name() == g.pkg) {
if o.Pkg() == nil || o.Pkg().Name() == "main" || (g.ip && o.Pkg() == g.iface.Pkg) {
return o.Name()
}
return g.addPackageImport(o.Pkg()) + "." + o.Name()
Expand Down Expand Up @@ -450,6 +454,7 @@ func (g *Generator) genList(list *types.Tuple, variadic bool) *paramList {

params.Names = append(params.Names, pname)
params.Types = append(params.Types, ts)

params.Params = append(params.Params, fmt.Sprintf("%s %s", pname, ts))
params.Nilable = append(params.Nilable, isNillable(v.Type()))
}
Expand Down
11 changes: 9 additions & 2 deletions mockery/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package mockery
import (
"io/ioutil"
"path/filepath"
"testing"

"strings"
"testing"

"github.com/stretchr/testify/suite"
)
Expand Down Expand Up @@ -919,6 +918,14 @@ func (_m *Example) B(_a0 string) fixtureshttp.MyStruct {
)
}

func (s *GeneratorSuite) TestGeneratorWithImportSameAsLocalPackageInpkgNoCycle() {
iface := s.getInterfaceFromFile("imports_same_as_package.go", "ImportsSameAsPackage")
pkg := iface.Path
gen := NewGenerator(iface, pkg, true)
gen.GeneratePrologue(pkg)
s.NotContains(gen.buf.String(), `import test "github.com/vektra/mockery/mockery/fixtures/test"`)
}

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

0 comments on commit 1401627

Please sign in to comment.