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

Add -embed flag into mockgen #683

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions mockgen/internal/tests/embed/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package embed

//go:generate mockgen -embed -package embed -destination mock.go . Hoge
//go:generate mockgen -embed -destination mock/mock.go . Hoge

type Hoge interface {
Fuga() error
mustImplementedFunction()
}

type HogeImpl struct {
s string
}

func (h *HogeImpl) Fuga() error {
return nil
}
61 changes: 61 additions & 0 deletions mockgen/internal/tests/embed/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions mockgen/internal/tests/embed/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions mockgen/internal/tests/embed/mock/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mock_embed_test

import (
reflect "reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/golang/mock/mockgen/internal/tests/embed"
mock_embed "github.com/golang/mock/mockgen/internal/tests/embed/mock"
)

func TestEmbed(t *testing.T) {
hoge := mock_embed.NewMockHoge(gomock.NewController(t))
et := reflect.TypeOf((*embed.Hoge)(nil)).Elem()
ht := reflect.TypeOf(hoge)
if !ht.Implements(et) {
t.Errorf("source interface has been not implemented")
}
}
19 changes: 19 additions & 0 deletions mockgen/internal/tests/embed/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This test is for when mock is same package as the source.
package embed_test

import (
reflect "reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/golang/mock/mockgen/internal/tests/embed"
)

func TestEmbed(t *testing.T) {
hoge := embed.NewMockHoge(gomock.NewController(t))
et := reflect.TypeOf((*embed.Hoge)(nil)).Elem()
ht := reflect.TypeOf(hoge)
if !ht.Implements(et) {
t.Errorf("source interface has been not implemented")
}
}
15 changes: 13 additions & 2 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
selfPackage = flag.String("self_package", "", "The full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. This can happen if the mock's package is set to one of its inputs (usually the main one) and the output is stdio so mockgen cannot detect the final output package. Setting this flag will then tell mockgen which import to exclude.")
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
embed = flag.Bool("embed", false, "Embed source interface into generated mock structure")

debugParser = flag.Bool("debug_parser", false, "Print out parser results only.")
showVersion = flag.Bool("version", false, "Print version.")
Expand Down Expand Up @@ -341,6 +342,9 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
g.packageMap[pth] = pkgName
localNames[pkgName] = true
}
if *embed && pkg.Name != *packageOut {
g.packageMap[g.srcPackage] = pkg.Name
}

if *writePkgComment {
g.p("// Package %v is a generated GoMock package.", outputPkgName)
Expand All @@ -362,7 +366,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
g.p(")")

for _, intf := range pkg.Interfaces {
if err := g.GenerateMockInterface(intf, outputPackagePath); err != nil {
if err := g.GenerateMockInterface(pkg.Name, intf, outputPackagePath); err != nil {
return err
}
}
Expand Down Expand Up @@ -404,14 +408,21 @@ func (g *generator) formattedTypeParams(it *model.Interface, pkgOverride string)
return long.String(), short.String()
}

func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePath string) error {
func (g *generator) GenerateMockInterface(pkgName string, intf *model.Interface, outputPackagePath string) error {
mockType := g.mockName(intf.Name)
longTp, shortTp := g.formattedTypeParams(intf, outputPackagePath)

g.p("")
g.p("// %v is a mock of %v interface.", mockType, intf.Name)
g.p("type %v%v struct {", mockType, longTp)
g.in()
if *embed {
if pkgName != *packageOut {
g.p("%v.%v", pkgName, intf.Name)
} else {
g.p("%v", intf.Name)
}
}
g.p("ctrl *gomock.Controller")
g.p("recorder *%vMockRecorder%v", mockType, shortTp)
g.out()
Expand Down