From fbfdb26de12175080e4d47a233969c511e49df25 Mon Sep 17 00:00:00 2001 From: Damiano Petrungaro Date: Sun, 23 Jul 2023 19:41:39 +0200 Subject: [PATCH 1/3] feat: add flag to generate go:generate directive in the generate file --- .../tests/add_generate_directive/import.go | 10 ++++ .../tests/add_generate_directive/mock.go | 52 +++++++++++++++++++ mockgen/mockgen.go | 23 ++++---- 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 mockgen/internal/tests/add_generate_directive/import.go create mode 100644 mockgen/internal/tests/add_generate_directive/mock.go diff --git a/mockgen/internal/tests/add_generate_directive/import.go b/mockgen/internal/tests/add_generate_directive/import.go new file mode 100644 index 0000000..c24b1f2 --- /dev/null +++ b/mockgen/internal/tests/add_generate_directive/import.go @@ -0,0 +1,10 @@ +// Package add_generate_directive makes sure output places the go:generate command as a directive in the generated code. +package add_generate_directive + +type Message struct { + Text string +} + +type Foo interface { + Bar(channels []string, message chan<- Message) +} diff --git a/mockgen/internal/tests/add_generate_directive/mock.go b/mockgen/internal/tests/add_generate_directive/mock.go new file mode 100644 index 0000000..ab00a30 --- /dev/null +++ b/mockgen/internal/tests/add_generate_directive/mock.go @@ -0,0 +1,52 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/add_generate_directive (interfaces: Foo) +// +// Generated by this command: +// +// mockgen -write_generate_directive -destination mock.go -package add_generate_directive . Foo +// +// Package add_generate_directive is a generated GoMock package. +package add_generate_directive + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +//go:generate mockgen -write_generate_directive -destination mock.go -package add_generate_directive . Foo + +// MockFoo is a mock of Foo interface. +type MockFoo struct { + ctrl *gomock.Controller + recorder *MockFooMockRecorder +} + +// MockFooMockRecorder is the mock recorder for MockFoo. +type MockFooMockRecorder struct { + mock *MockFoo +} + +// NewMockFoo creates a new mock instance. +func NewMockFoo(ctrl *gomock.Controller) *MockFoo { + mock := &MockFoo{ctrl: ctrl} + mock.recorder = &MockFooMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockFoo) EXPECT() *MockFooMockRecorder { + return m.recorder +} + +// Bar mocks base method. +func (m *MockFoo) Bar(arg0 []string, arg1 chan<- Message) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Bar", arg0, arg1) +} + +// Bar indicates an expected call of Bar. +func (mr *MockFooMockRecorder) Bar(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockFoo)(nil).Bar), arg0, arg1) +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 6fe76d7..7bf45f2 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -53,15 +53,16 @@ var ( ) var ( - source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.") - destination = flag.String("destination", "", "Output file; defaults to stdout.") - mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.") - packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.") - 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.") - writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") - copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") - typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function") + source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.") + destination = flag.String("destination", "", "Output file; defaults to stdout.") + mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.") + packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.") + 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.") + writeGenerateDirective = flag.Bool("write_generate_directive", false, "Put the //go:generate directive into the generated code") + writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") + copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") + typed = flag.Bool("typed", false, "Generate Type-safe 'Return', 'Do', 'DoAndReturn' function") debugParser = flag.Bool("debug_parser", false, "Print out parser results only.") showVersion = flag.Bool("version", false, "Print version.") @@ -366,6 +367,10 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac g.out() g.p(")") + if *writeGenerateDirective { + g.p("//go:generate %v", strings.Join(os.Args, " ")) + } + for _, intf := range pkg.Interfaces { if err := g.GenerateMockInterface(intf, outputPackagePath); err != nil { return err From e8ed9a400549d8fd93c319d978a5fb3bf4664eff Mon Sep 17 00:00:00 2001 From: Moises Vega Date: Fri, 11 Aug 2023 13:28:49 -0700 Subject: [PATCH 2/3] Update mockgen/mockgen.go --- mockgen/mockgen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index b8a97a6..0642c20 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -60,7 +60,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.") writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") - writeGenerateDirective = flag.Bool("write_generate_directive", false, "Put the //go:generate directive into the generated code") + writeGenerateDirective = flag.Bool("write_generate_directive", false, "Put the //go:generate directive into the generated code") copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") 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.") From 6e2ef23c1eca6149a12830cfc806c2294ae0df7a Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 11 Aug 2023 13:30:07 -0700 Subject: [PATCH 3/3] fix formatting --- mockgen/mockgen.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 0642c20..55dbc7b 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -53,18 +53,18 @@ var ( ) var ( - source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.") - destination = flag.String("destination", "", "Output file; defaults to stdout.") - mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.") - packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.") - 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.") - writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") + source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.") + destination = flag.String("destination", "", "Output file; defaults to stdout.") + mockNames = flag.String("mock_names", "", "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix.") + packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.") + 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.") + writeSourceComment = flag.Bool("write_source_comment", true, "Writes original file (source mode) or interface names (reflect mode) comment if true.") writeGenerateDirective = flag.Bool("write_generate_directive", false, "Put the //go:generate directive into the generated code") - copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") - 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.") - auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.") + copyrightFile = flag.String("copyright_file", "", "Copyright file used to add copyright header") + 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.") + auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.") debugParser = flag.Bool("debug_parser", false, "Print out parser results only.") showVersion = flag.Bool("version", false, "Print version.")