From 37f6db3f59e08fc340ebb454190545a090a85359 Mon Sep 17 00:00:00 2001 From: r-hang Date: Wed, 20 Dec 2023 10:12:39 -0800 Subject: [PATCH] mockgen: Sanitize the "any" package name (#132) When a package name is "any" mockgen will use that as a package name in the generated code which causes issues due to colliding with Go's "any" type. Update the sanitization logic to prevent this collision. --- .../internal/tests/sanitization/any/any.go | 5 ++ .../internal/tests/sanitization/interface.go | 11 ++++ .../tests/sanitization/mockout/mock.go | 52 +++++++++++++++++++ .../tests/sanitization/sanitization_test.go | 16 ++++++ mockgen/mockgen.go | 2 +- 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 mockgen/internal/tests/sanitization/any/any.go create mode 100644 mockgen/internal/tests/sanitization/interface.go create mode 100644 mockgen/internal/tests/sanitization/mockout/mock.go create mode 100644 mockgen/internal/tests/sanitization/sanitization_test.go diff --git a/mockgen/internal/tests/sanitization/any/any.go b/mockgen/internal/tests/sanitization/any/any.go new file mode 100644 index 0000000..463403d --- /dev/null +++ b/mockgen/internal/tests/sanitization/any/any.go @@ -0,0 +1,5 @@ +package any + +// Any is a type of a package that tests the sanitization of imported packages +// named any. +type Any struct {} diff --git a/mockgen/internal/tests/sanitization/interface.go b/mockgen/internal/tests/sanitization/interface.go new file mode 100644 index 0000000..b7caa45 --- /dev/null +++ b/mockgen/internal/tests/sanitization/interface.go @@ -0,0 +1,11 @@ +package sanitization + +import ( + "go.uber.org/mock/mockgen/internal/tests/sanitization/any" +) + +//go:generate mockgen -destination mockout/mock.go -package mockout . AnyMock + +type AnyMock interface { + Do(a *any.Any, b int) +} diff --git a/mockgen/internal/tests/sanitization/mockout/mock.go b/mockgen/internal/tests/sanitization/mockout/mock.go new file mode 100644 index 0000000..a529b2d --- /dev/null +++ b/mockgen/internal/tests/sanitization/mockout/mock.go @@ -0,0 +1,52 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/sanitization (interfaces: AnyMock) +// +// Generated by this command: +// +// mockgen -destination mockout/mock.go -package mockout . AnyMock +// + +// Package mockout is a generated GoMock package. +package mockout + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + any0 "go.uber.org/mock/mockgen/internal/tests/sanitization/any" +) + +// MockAnyMock is a mock of AnyMock interface. +type MockAnyMock struct { + ctrl *gomock.Controller + recorder *MockAnyMockMockRecorder +} + +// MockAnyMockMockRecorder is the mock recorder for MockAnyMock. +type MockAnyMockMockRecorder struct { + mock *MockAnyMock +} + +// NewMockAnyMock creates a new mock instance. +func NewMockAnyMock(ctrl *gomock.Controller) *MockAnyMock { + mock := &MockAnyMock{ctrl: ctrl} + mock.recorder = &MockAnyMockMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAnyMock) EXPECT() *MockAnyMockMockRecorder { + return m.recorder +} + +// Do mocks base method. +func (m *MockAnyMock) Do(arg0 *any0.Any, arg1 int) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Do", arg0, arg1) +} + +// Do indicates an expected call of Do. +func (mr *MockAnyMockMockRecorder) Do(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Do", reflect.TypeOf((*MockAnyMock)(nil).Do), arg0, arg1) +} diff --git a/mockgen/internal/tests/sanitization/sanitization_test.go b/mockgen/internal/tests/sanitization/sanitization_test.go new file mode 100644 index 0000000..7af1224 --- /dev/null +++ b/mockgen/internal/tests/sanitization/sanitization_test.go @@ -0,0 +1,16 @@ +package sanitization + +import ( + "testing" + + "go.uber.org/mock/gomock" + any0 "go.uber.org/mock/mockgen/internal/tests/sanitization/any" + "go.uber.org/mock/mockgen/internal/tests/sanitization/mockout" +) + +func TestSanitization(t *testing.T) { + ctrl := gomock.NewController(t) + m := mockout.NewMockAnyMock(ctrl) + m.EXPECT().Do(gomock.Any(), 1) + m.Do(&any0.Any{}, 1) +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index 94a3e71..df7d85f 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -378,7 +378,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac } i := 0 - for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() { + for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() || pkgName == "any" { pkgName = base + strconv.Itoa(i) i++ }