From f042bfa663d7d192e0959fda1471fe02cfbc3ba1 Mon Sep 17 00:00:00 2001 From: joaks Date: Wed, 18 Sep 2024 16:39:57 +0000 Subject: [PATCH] Avoid stringer-related deadlocks without adding ISGOMOCK A deadlock related to controller calling Stringer on mocks themselves was revealed in #116. #144 solved this deadlock by adding a generated `ISGOMOCK()` method to all generated mocks, and then checking for it before calling `.String()` on arguments. This reveals an exported method on each generated mock that: * Bloats the generated code * Can be taken dependency on in strange ways via Hyrum's Law * Technically opens up another route for naming collision. This PR attempts to clean up this type of check by instead generating an unexported field in generated mock structs instead, and checks for it using reflect. This hides this implementation detail of gomock/mockgen better, and produces less generated bloat. This PR then regenerated all generated code for tests/example via `go generate`. Note that, importantly, the regression test added in #144 still passes with this PR. Ref: #193 --- gomock/internal/mock_gomock/mock_matcher.go | 6 +-- gomock/mock_test.go | 6 +-- gomock/string.go | 43 ++++++++++++------- .../tests/add_generate_directive/mock.go | 6 +-- .../internal/tests/const_array_length/mock.go | 6 +-- mockgen/internal/tests/copyright_file/mock.go | 6 +-- .../greeter/greeter_mock_test.go | 6 +-- .../tests/defined_import_local_name/mock.go | 6 +-- mockgen/internal/tests/dot_imports/mock.go | 6 +-- .../internal/tests/empty_interface/mock.go | 6 +-- mockgen/internal/tests/exclude/mock.go | 6 +-- mockgen/internal/tests/extra_import/mock.go | 6 +-- .../bugreport_mock.go | 6 +-- .../bugreport_mock.go | 6 +-- .../import_embedded_interface/net_mock.go | 6 +-- .../import_source/definition/source_mock.go | 6 +-- .../tests/import_source/source_mock.go | 6 +-- .../subdir/internal/pkg/source_output/mock.go | 12 +----- .../missing_import/output/source_mock.go | 6 +-- .../tests/mock_in_test_package/mock_test.go | 6 +-- .../tests/mock_name/mocks/post_service.go | 6 +-- .../tests/mock_name/mocks/user_service.go | 6 +-- .../tests/overlapping_methods/mock.go | 6 +-- .../internal/tests/package_comment/mock.go | 6 +-- .../internal/tests/panicing_test/mock_test.go | 6 +-- .../tests/sanitization/mockout/mock.go | 6 +-- mockgen/internal/tests/self_package/mock.go | 6 +-- .../internal/tests/test_package/mock_test.go | 6 +-- mockgen/internal/tests/typed_inorder/mock.go | 6 +-- .../tests/unexported_method/bugreport_mock.go | 6 +-- mockgen/internal/tests/vendor_dep/mock.go | 6 +-- .../vendor_dep/source_mock_package/mock.go | 6 +-- mockgen/internal/tests/vendor_pkg/mock.go | 6 +-- mockgen/mockgen.go | 9 +--- 34 files changed, 61 insertions(+), 189 deletions(-) diff --git a/gomock/internal/mock_gomock/mock_matcher.go b/gomock/internal/mock_gomock/mock_matcher.go index fb0693c..2558913 100644 --- a/gomock/internal/mock_gomock/mock_matcher.go +++ b/gomock/internal/mock_gomock/mock_matcher.go @@ -19,6 +19,7 @@ import ( type MockMatcher struct { ctrl *gomock.Controller recorder *MockMatcherMockRecorder + isgomock struct{} } // MockMatcherMockRecorder is the mock recorder for MockMatcher. @@ -38,11 +39,6 @@ func (m *MockMatcher) EXPECT() *MockMatcherMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockMatcher) ISGOMOCK() struct{} { - return struct{}{} -} - // Matches mocks base method. func (m *MockMatcher) Matches(arg0 any) bool { m.ctrl.T.Helper() diff --git a/gomock/mock_test.go b/gomock/mock_test.go index aa632a4..8b3ca38 100644 --- a/gomock/mock_test.go +++ b/gomock/mock_test.go @@ -19,6 +19,7 @@ import ( type MockFoo struct { ctrl *gomock.Controller recorder *MockFooMockRecorder + isgomock struct{} } // MockFooMockRecorder is the mock recorder for MockFoo. @@ -38,11 +39,6 @@ func (m *MockFoo) EXPECT() *MockFooMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFoo) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockFoo) Bar(arg0 string) string { m.ctrl.T.Helper() diff --git a/gomock/string.go b/gomock/string.go index 6daf917..ec4ca7e 100644 --- a/gomock/string.go +++ b/gomock/string.go @@ -1,25 +1,36 @@ package gomock -import "fmt" - -type mockInstance interface { - ISGOMOCK() struct{} -} -type mockedStringer interface { - fmt.Stringer - mockInstance -} +import ( + "fmt" + "reflect" +) // getString is a safe way to convert a value to a string for printing results // If the value is a a mock, getString avoids calling the mocked String() method, // which avoids potential deadlocks func getString(x any) string { - switch v := x.(type) { - case mockedStringer: - return fmt.Sprintf("%T", v) - case fmt.Stringer: - return v.String() - default: - return fmt.Sprintf("%v", v) + if isGeneratedMock(x) { + return fmt.Sprintf("%T", x) + } + if s, ok := x.(fmt.Stringer); ok { + return s.String() + } + return fmt.Sprintf("%v", x) +} + +// isGeneratedMock checks if the given type has a "isgomock" field, +// indicating it is a generated mock. +func isGeneratedMock(x any) bool { + typ := reflect.TypeOf(x) + if typ == nil { + return false + } + if typ.Kind() == reflect.Ptr { + typ = typ.Elem() + } + if typ.Kind() != reflect.Struct { + return false } + _, isgomock := typ.FieldByName("isgomock") + return isgomock } diff --git a/mockgen/internal/tests/add_generate_directive/mock.go b/mockgen/internal/tests/add_generate_directive/mock.go index 1594548..2cd489e 100644 --- a/mockgen/internal/tests/add_generate_directive/mock.go +++ b/mockgen/internal/tests/add_generate_directive/mock.go @@ -21,6 +21,7 @@ import ( type MockFoo struct { ctrl *gomock.Controller recorder *MockFooMockRecorder + isgomock struct{} } // MockFooMockRecorder is the mock recorder for MockFoo. @@ -40,11 +41,6 @@ func (m *MockFoo) EXPECT() *MockFooMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFoo) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockFoo) Bar(arg0 []string, arg1 chan<- Message) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/const_array_length/mock.go b/mockgen/internal/tests/const_array_length/mock.go index 844a9ab..c1ec7f8 100644 --- a/mockgen/internal/tests/const_array_length/mock.go +++ b/mockgen/internal/tests/const_array_length/mock.go @@ -19,6 +19,7 @@ import ( type MockI struct { ctrl *gomock.Controller recorder *MockIMockRecorder + isgomock struct{} } // MockIMockRecorder is the mock recorder for MockI. @@ -38,11 +39,6 @@ func (m *MockI) EXPECT() *MockIMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockI) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockI) Bar() [2]int { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/copyright_file/mock.go b/mockgen/internal/tests/copyright_file/mock.go index cd71bea..8408cda 100644 --- a/mockgen/internal/tests/copyright_file/mock.go +++ b/mockgen/internal/tests/copyright_file/mock.go @@ -24,6 +24,7 @@ import ( type MockEmpty struct { ctrl *gomock.Controller recorder *MockEmptyMockRecorder + isgomock struct{} } // MockEmptyMockRecorder is the mock recorder for MockEmpty. @@ -42,8 +43,3 @@ func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { return m.recorder } - -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockEmpty) ISGOMOCK() struct{} { - return struct{}{} -} diff --git a/mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go b/mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go index 1eaa93c..f87a3c3 100644 --- a/mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go +++ b/mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go @@ -20,6 +20,7 @@ import ( type MockInputMaker struct { ctrl *gomock.Controller recorder *MockInputMakerMockRecorder + isgomock struct{} } // MockInputMakerMockRecorder is the mock recorder for MockInputMaker. @@ -39,11 +40,6 @@ func (m *MockInputMaker) EXPECT() *MockInputMakerMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockInputMaker) ISGOMOCK() struct{} { - return struct{}{} -} - // MakeInput mocks base method. func (m *MockInputMaker) MakeInput() client.GreetInput { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/defined_import_local_name/mock.go b/mockgen/internal/tests/defined_import_local_name/mock.go index 9648c8f..810f364 100644 --- a/mockgen/internal/tests/defined_import_local_name/mock.go +++ b/mockgen/internal/tests/defined_import_local_name/mock.go @@ -21,6 +21,7 @@ import ( type MockWithImports struct { ctrl *gomock.Controller recorder *MockWithImportsMockRecorder + isgomock struct{} } // MockWithImportsMockRecorder is the mock recorder for MockWithImports. @@ -40,11 +41,6 @@ func (m *MockWithImports) EXPECT() *MockWithImportsMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockWithImports) ISGOMOCK() struct{} { - return struct{}{} -} - // Method1 mocks base method. func (m *MockWithImports) Method1() b_mock.Buffer { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/dot_imports/mock.go b/mockgen/internal/tests/dot_imports/mock.go index 20d27d3..e941ee1 100644 --- a/mockgen/internal/tests/dot_imports/mock.go +++ b/mockgen/internal/tests/dot_imports/mock.go @@ -22,6 +22,7 @@ import ( type MockWithDotImports struct { ctrl *gomock.Controller recorder *MockWithDotImportsMockRecorder + isgomock struct{} } // MockWithDotImportsMockRecorder is the mock recorder for MockWithDotImports. @@ -41,11 +42,6 @@ func (m *MockWithDotImports) EXPECT() *MockWithDotImportsMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockWithDotImports) ISGOMOCK() struct{} { - return struct{}{} -} - // Method1 mocks base method. func (m *MockWithDotImports) Method1() Request { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/empty_interface/mock.go b/mockgen/internal/tests/empty_interface/mock.go index 6656e40..bff5ea1 100644 --- a/mockgen/internal/tests/empty_interface/mock.go +++ b/mockgen/internal/tests/empty_interface/mock.go @@ -17,6 +17,7 @@ import ( type MockEmpty struct { ctrl *gomock.Controller recorder *MockEmptyMockRecorder + isgomock struct{} } // MockEmptyMockRecorder is the mock recorder for MockEmpty. @@ -35,8 +36,3 @@ func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { return m.recorder } - -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockEmpty) ISGOMOCK() struct{} { - return struct{}{} -} diff --git a/mockgen/internal/tests/exclude/mock.go b/mockgen/internal/tests/exclude/mock.go index 4f219ae..304e018 100644 --- a/mockgen/internal/tests/exclude/mock.go +++ b/mockgen/internal/tests/exclude/mock.go @@ -19,6 +19,7 @@ import ( type MockGenerateMockForMe struct { ctrl *gomock.Controller recorder *MockGenerateMockForMeMockRecorder + isgomock struct{} } // MockGenerateMockForMeMockRecorder is the mock recorder for MockGenerateMockForMe. @@ -38,11 +39,6 @@ func (m *MockGenerateMockForMe) EXPECT() *MockGenerateMockForMeMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockGenerateMockForMe) ISGOMOCK() struct{} { - return struct{}{} -} - // B mocks base method. func (m *MockGenerateMockForMe) B() int { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/extra_import/mock.go b/mockgen/internal/tests/extra_import/mock.go index 1c3173c..da611a8 100644 --- a/mockgen/internal/tests/extra_import/mock.go +++ b/mockgen/internal/tests/extra_import/mock.go @@ -19,6 +19,7 @@ import ( type MockFoo struct { ctrl *gomock.Controller recorder *MockFooMockRecorder + isgomock struct{} } // MockFooMockRecorder is the mock recorder for MockFoo. @@ -38,11 +39,6 @@ func (m *MockFoo) EXPECT() *MockFooMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFoo) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockFoo) Bar(arg0 []string, arg1 chan<- Message) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/generated_identifier_conflict/bugreport_mock.go b/mockgen/internal/tests/generated_identifier_conflict/bugreport_mock.go index cf871e1..e066b90 100644 --- a/mockgen/internal/tests/generated_identifier_conflict/bugreport_mock.go +++ b/mockgen/internal/tests/generated_identifier_conflict/bugreport_mock.go @@ -19,6 +19,7 @@ import ( type MockExample struct { ctrl *gomock.Controller recorder *MockExampleMockRecorder + isgomock struct{} } // MockExampleMockRecorder is the mock recorder for MockExample. @@ -38,11 +39,6 @@ func (m *MockExample) EXPECT() *MockExampleMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockExample) ISGOMOCK() struct{} { - return struct{}{} -} - // Method mocks base method. func (m_2 *MockExample) Method(_m, _mr, m, mr int) { m_2.ctrl.T.Helper() diff --git a/mockgen/internal/tests/import_embedded_interface/bugreport_mock.go b/mockgen/internal/tests/import_embedded_interface/bugreport_mock.go index 5564c86..bd89e59 100644 --- a/mockgen/internal/tests/import_embedded_interface/bugreport_mock.go +++ b/mockgen/internal/tests/import_embedded_interface/bugreport_mock.go @@ -21,6 +21,7 @@ import ( type MockSource struct { ctrl *gomock.Controller recorder *MockSourceMockRecorder + isgomock struct{} } // MockSourceMockRecorder is the mock recorder for MockSource. @@ -40,11 +41,6 @@ func (m *MockSource) EXPECT() *MockSourceMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockSource) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockSource) Bar() Baz { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/import_embedded_interface/net_mock.go b/mockgen/internal/tests/import_embedded_interface/net_mock.go index 1f7ba06..0c7da5b 100644 --- a/mockgen/internal/tests/import_embedded_interface/net_mock.go +++ b/mockgen/internal/tests/import_embedded_interface/net_mock.go @@ -20,6 +20,7 @@ import ( type MockNet struct { ctrl *gomock.Controller recorder *MockNetMockRecorder + isgomock struct{} } // MockNetMockRecorder is the mock recorder for MockNet. @@ -39,11 +40,6 @@ func (m *MockNet) EXPECT() *MockNetMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockNet) ISGOMOCK() struct{} { - return struct{}{} -} - // Header mocks base method. func (m *MockNet) Header() http.Header { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/import_source/definition/source_mock.go b/mockgen/internal/tests/import_source/definition/source_mock.go index 318544a..a819a6a 100644 --- a/mockgen/internal/tests/import_source/definition/source_mock.go +++ b/mockgen/internal/tests/import_source/definition/source_mock.go @@ -19,6 +19,7 @@ import ( type MockS struct { ctrl *gomock.Controller recorder *MockSMockRecorder + isgomock struct{} } // MockSMockRecorder is the mock recorder for MockS. @@ -38,11 +39,6 @@ func (m *MockS) EXPECT() *MockSMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockS) ISGOMOCK() struct{} { - return struct{}{} -} - // F mocks base method. func (m *MockS) F(arg0 X) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/import_source/source_mock.go b/mockgen/internal/tests/import_source/source_mock.go index 7605f4a..5538ed7 100644 --- a/mockgen/internal/tests/import_source/source_mock.go +++ b/mockgen/internal/tests/import_source/source_mock.go @@ -20,6 +20,7 @@ import ( type MockS struct { ctrl *gomock.Controller recorder *MockSMockRecorder + isgomock struct{} } // MockSMockRecorder is the mock recorder for MockS. @@ -39,11 +40,6 @@ func (m *MockS) EXPECT() *MockSMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockS) ISGOMOCK() struct{} { - return struct{}{} -} - // F mocks base method. func (m *MockS) F(arg0 source.X) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/internal_pkg/subdir/internal/pkg/source_output/mock.go b/mockgen/internal/tests/internal_pkg/subdir/internal/pkg/source_output/mock.go index 0f841bf..52adf40 100644 --- a/mockgen/internal/tests/internal_pkg/subdir/internal/pkg/source_output/mock.go +++ b/mockgen/internal/tests/internal_pkg/subdir/internal/pkg/source_output/mock.go @@ -20,6 +20,7 @@ import ( type MockArg struct { ctrl *gomock.Controller recorder *MockArgMockRecorder + isgomock struct{} } // MockArgMockRecorder is the mock recorder for MockArg. @@ -39,11 +40,6 @@ func (m *MockArg) EXPECT() *MockArgMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockArg) ISGOMOCK() struct{} { - return struct{}{} -} - // Foo mocks base method. func (m *MockArg) Foo() int { m.ctrl.T.Helper() @@ -62,6 +58,7 @@ func (mr *MockArgMockRecorder) Foo() *gomock.Call { type MockIntf struct { ctrl *gomock.Controller recorder *MockIntfMockRecorder + isgomock struct{} } // MockIntfMockRecorder is the mock recorder for MockIntf. @@ -81,11 +78,6 @@ func (m *MockIntf) EXPECT() *MockIntfMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockIntf) ISGOMOCK() struct{} { - return struct{}{} -} - // F mocks base method. func (m *MockIntf) F() pkg.Arg { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/missing_import/output/source_mock.go b/mockgen/internal/tests/missing_import/output/source_mock.go index 7f20a99..3f38ad8 100644 --- a/mockgen/internal/tests/missing_import/output/source_mock.go +++ b/mockgen/internal/tests/missing_import/output/source_mock.go @@ -20,6 +20,7 @@ import ( type MockBar struct { ctrl *gomock.Controller recorder *MockBarMockRecorder + isgomock struct{} } // MockBarMockRecorder is the mock recorder for MockBar. @@ -39,11 +40,6 @@ func (m *MockBar) EXPECT() *MockBarMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockBar) ISGOMOCK() struct{} { - return struct{}{} -} - // Baz mocks base method. func (m *MockBar) Baz(arg0 source.Foo) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/mock_in_test_package/mock_test.go b/mockgen/internal/tests/mock_in_test_package/mock_test.go index 6b187d8..ead45ab 100644 --- a/mockgen/internal/tests/mock_in_test_package/mock_test.go +++ b/mockgen/internal/tests/mock_in_test_package/mock_test.go @@ -20,6 +20,7 @@ import ( type MockFinder struct { ctrl *gomock.Controller recorder *MockFinderMockRecorder + isgomock struct{} } // MockFinderMockRecorder is the mock recorder for MockFinder. @@ -39,11 +40,6 @@ func (m *MockFinder) EXPECT() *MockFinderMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFinder) ISGOMOCK() struct{} { - return struct{}{} -} - // Add mocks base method. func (m *MockFinder) Add(u users.User) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/mock_name/mocks/post_service.go b/mockgen/internal/tests/mock_name/mocks/post_service.go index d1dcc6d..d8dc033 100644 --- a/mockgen/internal/tests/mock_name/mocks/post_service.go +++ b/mockgen/internal/tests/mock_name/mocks/post_service.go @@ -21,6 +21,7 @@ import ( type PostServiceMock struct { ctrl *gomock.Controller recorder *PostServiceMockMockRecorder + isgomock struct{} } // PostServiceMockMockRecorder is the mock recorder for PostServiceMock. @@ -40,11 +41,6 @@ func (m *PostServiceMock) EXPECT() *PostServiceMockMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *PostServiceMock) ISGOMOCK() struct{} { - return struct{}{} -} - // Create mocks base method. func (m *PostServiceMock) Create(arg0, arg1 string, arg2 *user.User) (*post.Post, error) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/mock_name/mocks/user_service.go b/mockgen/internal/tests/mock_name/mocks/user_service.go index 8842eae..6447691 100644 --- a/mockgen/internal/tests/mock_name/mocks/user_service.go +++ b/mockgen/internal/tests/mock_name/mocks/user_service.go @@ -20,6 +20,7 @@ import ( type UserServiceMock struct { ctrl *gomock.Controller recorder *UserServiceMockMockRecorder + isgomock struct{} } // UserServiceMockMockRecorder is the mock recorder for UserServiceMock. @@ -39,11 +40,6 @@ func (m *UserServiceMock) EXPECT() *UserServiceMockMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *UserServiceMock) ISGOMOCK() struct{} { - return struct{}{} -} - // Create mocks base method. func (m *UserServiceMock) Create(arg0 string) (*user.User, error) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/overlapping_methods/mock.go b/mockgen/internal/tests/overlapping_methods/mock.go index 2cd0751..7f5c44b 100644 --- a/mockgen/internal/tests/overlapping_methods/mock.go +++ b/mockgen/internal/tests/overlapping_methods/mock.go @@ -19,6 +19,7 @@ import ( type MockReadWriteCloser struct { ctrl *gomock.Controller recorder *MockReadWriteCloserMockRecorder + isgomock struct{} } // MockReadWriteCloserMockRecorder is the mock recorder for MockReadWriteCloser. @@ -38,11 +39,6 @@ func (m *MockReadWriteCloser) EXPECT() *MockReadWriteCloserMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockReadWriteCloser) ISGOMOCK() struct{} { - return struct{}{} -} - // Close mocks base method. func (m *MockReadWriteCloser) Close() error { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/package_comment/mock.go b/mockgen/internal/tests/package_comment/mock.go index 8b5a097..f2fb9fb 100644 --- a/mockgen/internal/tests/package_comment/mock.go +++ b/mockgen/internal/tests/package_comment/mock.go @@ -16,6 +16,7 @@ import ( type MockEmpty struct { ctrl *gomock.Controller recorder *MockEmptyMockRecorder + isgomock struct{} } // MockEmptyMockRecorder is the mock recorder for MockEmpty. @@ -34,8 +35,3 @@ func NewMockEmpty(ctrl *gomock.Controller) *MockEmpty { func (m *MockEmpty) EXPECT() *MockEmptyMockRecorder { return m.recorder } - -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockEmpty) ISGOMOCK() struct{} { - return struct{}{} -} diff --git a/mockgen/internal/tests/panicing_test/mock_test.go b/mockgen/internal/tests/panicing_test/mock_test.go index 42e5fae..aef67e6 100644 --- a/mockgen/internal/tests/panicing_test/mock_test.go +++ b/mockgen/internal/tests/panicing_test/mock_test.go @@ -19,6 +19,7 @@ import ( type MockFoo struct { ctrl *gomock.Controller recorder *MockFooMockRecorder + isgomock struct{} } // MockFooMockRecorder is the mock recorder for MockFoo. @@ -38,11 +39,6 @@ func (m *MockFoo) EXPECT() *MockFooMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFoo) ISGOMOCK() struct{} { - return struct{}{} -} - // Bar mocks base method. func (m *MockFoo) Bar() string { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/sanitization/mockout/mock.go b/mockgen/internal/tests/sanitization/mockout/mock.go index 8e130b9..345d2b8 100644 --- a/mockgen/internal/tests/sanitization/mockout/mock.go +++ b/mockgen/internal/tests/sanitization/mockout/mock.go @@ -20,6 +20,7 @@ import ( type MockAnyMock struct { ctrl *gomock.Controller recorder *MockAnyMockMockRecorder + isgomock struct{} } // MockAnyMockMockRecorder is the mock recorder for MockAnyMock. @@ -39,11 +40,6 @@ func (m *MockAnyMock) EXPECT() *MockAnyMockMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockAnyMock) ISGOMOCK() struct{} { - return struct{}{} -} - // Do mocks base method. func (m *MockAnyMock) Do(arg0 *any0.Any, arg1 int) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/self_package/mock.go b/mockgen/internal/tests/self_package/mock.go index 18494fa..05a5761 100644 --- a/mockgen/internal/tests/self_package/mock.go +++ b/mockgen/internal/tests/self_package/mock.go @@ -19,6 +19,7 @@ import ( type MockMethods struct { ctrl *gomock.Controller recorder *MockMethodsMockRecorder + isgomock struct{} } // MockMethodsMockRecorder is the mock recorder for MockMethods. @@ -38,11 +39,6 @@ func (m *MockMethods) EXPECT() *MockMethodsMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockMethods) ISGOMOCK() struct{} { - return struct{}{} -} - // getInfo mocks base method. func (m *MockMethods) getInfo() Info { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/test_package/mock_test.go b/mockgen/internal/tests/test_package/mock_test.go index e0a5a0d..ed8ca9b 100644 --- a/mockgen/internal/tests/test_package/mock_test.go +++ b/mockgen/internal/tests/test_package/mock_test.go @@ -19,6 +19,7 @@ import ( type MockFinder struct { ctrl *gomock.Controller recorder *MockFinderMockRecorder + isgomock struct{} } // MockFinderMockRecorder is the mock recorder for MockFinder. @@ -38,11 +39,6 @@ func (m *MockFinder) EXPECT() *MockFinderMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockFinder) ISGOMOCK() struct{} { - return struct{}{} -} - // Add mocks base method. func (m *MockFinder) Add(u User) { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/typed_inorder/mock.go b/mockgen/internal/tests/typed_inorder/mock.go index ff98128..e7a21e3 100644 --- a/mockgen/internal/tests/typed_inorder/mock.go +++ b/mockgen/internal/tests/typed_inorder/mock.go @@ -19,6 +19,7 @@ import ( type MockAnimal struct { ctrl *gomock.Controller recorder *MockAnimalMockRecorder + isgomock struct{} } // MockAnimalMockRecorder is the mock recorder for MockAnimal. @@ -38,11 +39,6 @@ func (m *MockAnimal) EXPECT() *MockAnimalMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockAnimal) ISGOMOCK() struct{} { - return struct{}{} -} - // Feed mocks base method. func (m *MockAnimal) Feed(arg0 string) error { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/unexported_method/bugreport_mock.go b/mockgen/internal/tests/unexported_method/bugreport_mock.go index 8cb2e03..5670756 100644 --- a/mockgen/internal/tests/unexported_method/bugreport_mock.go +++ b/mockgen/internal/tests/unexported_method/bugreport_mock.go @@ -19,6 +19,7 @@ import ( type MockExample struct { ctrl *gomock.Controller recorder *MockExampleMockRecorder + isgomock struct{} } // MockExampleMockRecorder is the mock recorder for MockExample. @@ -38,11 +39,6 @@ func (m *MockExample) EXPECT() *MockExampleMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockExample) ISGOMOCK() struct{} { - return struct{}{} -} - // someMethod mocks base method. func (m *MockExample) someMethod(arg0 string) string { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/vendor_dep/mock.go b/mockgen/internal/tests/vendor_dep/mock.go index 7ce6fd4..ae663b6 100644 --- a/mockgen/internal/tests/vendor_dep/mock.go +++ b/mockgen/internal/tests/vendor_dep/mock.go @@ -20,6 +20,7 @@ import ( type MockVendorsDep struct { ctrl *gomock.Controller recorder *MockVendorsDepMockRecorder + isgomock struct{} } // MockVendorsDepMockRecorder is the mock recorder for MockVendorsDep. @@ -39,11 +40,6 @@ func (m *MockVendorsDep) EXPECT() *MockVendorsDepMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockVendorsDep) ISGOMOCK() struct{} { - return struct{}{} -} - // Foo mocks base method. func (m *MockVendorsDep) Foo() present.Elem { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/vendor_dep/source_mock_package/mock.go b/mockgen/internal/tests/vendor_dep/source_mock_package/mock.go index b55394a..88461e2 100644 --- a/mockgen/internal/tests/vendor_dep/source_mock_package/mock.go +++ b/mockgen/internal/tests/vendor_dep/source_mock_package/mock.go @@ -20,6 +20,7 @@ import ( type MockVendorsDep struct { ctrl *gomock.Controller recorder *MockVendorsDepMockRecorder + isgomock struct{} } // MockVendorsDepMockRecorder is the mock recorder for MockVendorsDep. @@ -39,11 +40,6 @@ func (m *MockVendorsDep) EXPECT() *MockVendorsDepMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockVendorsDep) ISGOMOCK() struct{} { - return struct{}{} -} - // Foo mocks base method. func (m *MockVendorsDep) Foo() present.Elem { m.ctrl.T.Helper() diff --git a/mockgen/internal/tests/vendor_pkg/mock.go b/mockgen/internal/tests/vendor_pkg/mock.go index 10eb886..e32fb0f 100644 --- a/mockgen/internal/tests/vendor_pkg/mock.go +++ b/mockgen/internal/tests/vendor_pkg/mock.go @@ -19,6 +19,7 @@ import ( type MockElem struct { ctrl *gomock.Controller recorder *MockElemMockRecorder + isgomock struct{} } // MockElemMockRecorder is the mock recorder for MockElem. @@ -38,11 +39,6 @@ func (m *MockElem) EXPECT() *MockElemMockRecorder { return m.recorder } -// ISGOMOCK indicates that this struct is a gomock mock. -func (m *MockElem) ISGOMOCK() struct{} { - return struct{}{} -} - // TemplateName mocks base method. func (m *MockElem) TemplateName() string { m.ctrl.T.Helper() diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 5d57868..6d7634b 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -477,6 +477,7 @@ func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePa g.in() g.p("ctrl *gomock.Controller") g.p("recorder *%vMockRecorder%v", mockType, shortTp) + g.p("isgomock struct{}") g.out() g.p("}") g.p("") @@ -507,14 +508,6 @@ func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePa g.out() g.p("}") - // XXX: possible name collision here if someone has ISGOMOCK in their interface. - g.p("// ISGOMOCK indicates that this struct is a gomock mock.") - g.p("func (m *%v%v) ISGOMOCK() struct{} {", mockType, shortTp) - g.in() - g.p("return struct{}{}") - g.out() - g.p("}") - g.GenerateMockMethods(mockType, intf, outputPackagePath, longTp, shortTp, *typed) return nil