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

Commit 1b95bd9

Browse files
authoredJan 12, 2020
fix: avoid constructing improper import graph (#383)
Fixes #380 Fixes #381 Fixes #382
1 parent 3251ae5 commit 1b95bd9

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed
 

‎mockgen/mockgen.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ func main() {
9292
dst = f
9393
}
9494

95-
packageName := *packageOut
96-
if packageName == "" {
95+
outputPackageName := *packageOut
96+
if outputPackageName == "" {
9797
// pkg.Name in reflect mode is the base name of the import path,
9898
// which might have characters that are illegal to have in package names.
99-
packageName = "mock_" + sanitize(pkg.Name)
99+
outputPackageName = "mock_" + sanitize(pkg.Name)
100100
}
101101

102102
// outputPackagePath represents the fully qualified name of the package of
@@ -137,7 +137,7 @@ func main() {
137137

138138
g.copyrightHeader = string(header)
139139
}
140-
if err := g.Generate(pkg, packageName, outputPackagePath); err != nil {
140+
if err := g.Generate(pkg, outputPackageName, outputPackagePath); err != nil {
141141
log.Fatalf("Failed generating mock: %v", err)
142142
}
143143
if _, err := dst.Write(g.Output()); err != nil {
@@ -234,8 +234,8 @@ func sanitize(s string) string {
234234
return t
235235
}
236236

237-
func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePath string) error {
238-
if pkgName != pkg.Name {
237+
func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPackagePath string) error {
238+
if outputPkgName != pkg.Name {
239239
outputPackagePath = ""
240240
}
241241

@@ -294,22 +294,27 @@ func (g *generator) Generate(pkg *model.Package, pkgName string, outputPackagePa
294294
i++
295295
}
296296

297+
// Avoid importing package if source pkg == output pkg
298+
if pth == pkg.PkgPath && outputPkgName == pkg.Name {
299+
continue
300+
}
301+
297302
g.packageMap[pth] = pkgName
298303
localNames[pkgName] = true
299304
}
300305

301306
if *writePkgComment {
302-
g.p("// Package %v is a generated GoMock package.", pkgName)
307+
g.p("// Package %v is a generated GoMock package.", outputPkgName)
303308
}
304-
g.p("package %v", pkgName)
309+
g.p("package %v", outputPkgName)
305310
g.p("")
306311
g.p("import (")
307312
g.in()
308-
for pkgPath, pkg := range g.packageMap {
313+
for pkgPath, pkgName := range g.packageMap {
309314
if pkgPath == outputPackagePath {
310315
continue
311316
}
312-
g.p("%v %q", pkg, pkgPath)
317+
g.p("%v %q", pkgName, pkgPath)
313318
}
314319
for _, pkgPath := range pkg.DotImports {
315320
g.p(". %q", pkgPath)

‎mockgen/model/model.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const pkgPath = "github.com/golang/mock/mockgen/model"
2929
// Package is a Go package. It may be a subset.
3030
type Package struct {
3131
Name string
32+
PkgPath string
3233
Interfaces []*Interface
3334
DotImports []string
3435
}

‎mockgen/parse.go

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag
187187
}
188188
return &model.Package{
189189
Name: file.Name.String(),
190+
PkgPath: importPath,
190191
Interfaces: is,
191192
DotImports: dotImports,
192193
}, nil

0 commit comments

Comments
 (0)
This repository has been archived.