From 62ff61221eceafa23cf78c5e680b443d12f704b2 Mon Sep 17 00:00:00 2001 From: Cody Oss Date: Fri, 10 Jan 2020 13:55:50 -0700 Subject: [PATCH 1/2] fix: avoid constructing improper import graph Fixes #380 Fixes #381 Fixes #382 --- mockgen/mockgen.go | 25 +++++++++++++++---------- mockgen/model/model.go | 1 + mockgen/parse.go | 1 + 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 18aeb3d0..f3835b10 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -92,11 +92,11 @@ func main() { dst = f } - packageName := *packageOut - if packageName == "" { + outputPackageName := *packageOut + if outputPackageName == "" { // pkg.Name in reflect mode is the base name of the import path, // which might have characters that are illegal to have in package names. - packageName = "mock_" + sanitize(pkg.Name) + outputPackageName = "mock_" + sanitize(pkg.Name) } // outputPackagePath represents the fully qualified name of the package of @@ -137,7 +137,7 @@ func main() { g.copyrightHeader = string(header) } - if err := g.Generate(pkg, packageName, outputPackagePath); err != nil { + if err := g.Generate(pkg, outputPackageName, outputPackagePath); err != nil { log.Fatalf("Failed generating mock: %v", err) } if _, err := dst.Write(g.Output()); err != nil { @@ -234,8 +234,8 @@ func sanitize(s string) string { return t } -func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePath string) error { - if pkgName != pkg.Name { +func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPackagePath string) error { + if outputPkgName != pkg.Name { outputPackagePath = "" } @@ -294,22 +294,27 @@ func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePa i++ } + // Avoid importing package if source pkg == output pkg + if pth == pkg.PkgPath && outputPkgName == pkg.Name { + continue + } + g.packageMap[pth] = pkgName localNames[pkgName] = true } if *writePkgComment { - g.p("// Package %v is a generated GoMock package.", pkgName) + g.p("// Package %v is a generated GoMock package.", outputPkgName) } - g.p("package %v", pkgName) + g.p("package %v", outputPkgName) g.p("") g.p("import (") g.in() - for pkgPath, pkg := range g.packageMap { + for pkgPath, pkgName := range g.packageMap { if pkgPath == outputPackagePath { continue } - g.p("%v %q", pkg, pkgPath) + g.p("%v %q", pkgName, pkgPath) } for _, pkgPath := range pkg.DotImports { g.p(". %q", pkgPath) diff --git a/mockgen/model/model.go b/mockgen/model/model.go index 9c143cfc..88783cfd 100644 --- a/mockgen/model/model.go +++ b/mockgen/model/model.go @@ -29,6 +29,7 @@ const pkgPath = "github.com/golang/mock/mockgen/model" // Package is a Go package. It may be a subset. type Package struct { Name string + PkgPath string Interfaces []*Interface DotImports []string } diff --git a/mockgen/parse.go b/mockgen/parse.go index b0403e58..5e830d69 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -187,6 +187,7 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag } return &model.Package{ Name: file.Name.String(), + PkgPath: importPath, Interfaces: is, DotImports: dotImports, }, nil From 099563292e166690d541d839c000a68adac51be0 Mon Sep 17 00:00:00 2001 From: Cody Oss Date: Fri, 10 Jan 2020 13:55:50 -0700 Subject: [PATCH 2/2] fix formatting --- mockgen/parse.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockgen/parse.go b/mockgen/parse.go index 5e830d69..5519507f 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -187,7 +187,7 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag } return &model.Package{ Name: file.Name.String(), - PkgPath: importPath, + PkgPath: importPath, Interfaces: is, DotImports: dotImports, }, nil