Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

fix: avoid constructing improper import graph #383

Merged
merged 2 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = ""
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions mockgen/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down