Skip to content

Add -embed flag into mockgen #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 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"

"go.uber.org/mock/gomock"
"go.uber.org/mock/mockgen/internal/tests/embed"
mock_embed "go.uber.org/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"

"go.uber.org/mock/gomock"
"go.uber.org/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 @@ -64,6 +64,7 @@ var (
writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (package mode) comment if true.")
writeGenerateDirective = flag.Bool("write_generate_directive", false, "Add //go:generate directive to regenerate the mock")
copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header")
embed = flag.Bool("embed", false, "Embed source interface into generated mock structure")
buildConstraint = flag.String("build_constraint", "", "If non-empty, added as //go:build <constraint>")
typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function")
imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
Expand Down Expand Up @@ -411,6 +412,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
}

// Ensure there is an empty line between “generated by” block and
// package documentation comments to follow the recommendations:
Expand Down Expand Up @@ -442,7 +446,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
}

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 @@ -484,14 +488,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.p("isgomock struct{}")
Expand Down