From 3cef7fdc3cde80a25f5791f3c6dd7fe247c01b22 Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:43:43 -0600 Subject: [PATCH 1/3] Fix bug with type aliases to structs @max-melentyev found in #843 that type aliases to structs were being rendered as a struct literal. The problem was that we need the type of the right hand side of the alias definition, not the underlying type itself. --- .mockery.yaml | 6 ++ pkg/fixtures/type_alias/interface.go | 7 ++ pkg/fixtures/type_alias/interface_test.go | 5 ++ .../mock_Interface2WithResolvedAlias_test.go | 70 ++++++++++++++++++ ...mock_Interface2WithUnresolvedAlias_test.go | 71 +++++++++++++++++++ pkg/fixtures/type_alias/subpkg/interface.go | 5 ++ pkg/generator.go | 3 +- 7 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go create mode 100644 pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go create mode 100644 pkg/fixtures/type_alias/subpkg/interface.go diff --git a/.mockery.yaml b/.mockery.yaml index f3590418..56b72995 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -108,4 +108,10 @@ packages: mockname: InterfaceWithUnresolvedAlias - resolve-type-alias: True mockname: InterfaceWithResolvedAlias + Interface2: + configs: + - resolve-type-alias: False + mockname: Interface2WithUnresolvedAlias + - resolve-type-alias: True + mockname: Interface2WithResolvedAlias diff --git a/pkg/fixtures/type_alias/interface.go b/pkg/fixtures/type_alias/interface.go index 95b16766..5d0e8a08 100644 --- a/pkg/fixtures/type_alias/interface.go +++ b/pkg/fixtures/type_alias/interface.go @@ -1,7 +1,14 @@ package type_alias +import "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg" + type Type = int +type S = subpkg.S type Interface1 interface { Foo() Type } + +type Interface2 interface { + F(Type, S, subpkg.S) +} diff --git a/pkg/fixtures/type_alias/interface_test.go b/pkg/fixtures/type_alias/interface_test.go index c8d545ce..f6b52aae 100644 --- a/pkg/fixtures/type_alias/interface_test.go +++ b/pkg/fixtures/type_alias/interface_test.go @@ -25,6 +25,11 @@ func TestTypeAlias(t *testing.T) { filepath: "./mock_InterfaceWithUnresolvedAlias_test.go", expectedRegex: `func \((_?[a-zA-Z]*)+ \*InterfaceWithUnresolvedAlias\) Foo\(\) type_alias.Type {`, }, + { + name: "Alias to type with underlying struct with resolve-type-alias: True", + filepath: "./mock_Interface2WithResolvedAlias_test.go", + expectedRegex: `func \(_m \*Interface2WithResolvedAlias\) F\(_a0 int, _a1 subpkg.S, _a2 subpkg.S\) {`, + }, } { t.Run(tt.name, func(t *testing.T) { regex, err := regexp.Compile(tt.expectedRegex) diff --git a/pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go b/pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go new file mode 100644 index 00000000..80cdaabe --- /dev/null +++ b/pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go @@ -0,0 +1,70 @@ +// Code generated by mockery. DO NOT EDIT. + +package type_alias_test + +import ( + mock "github.com/stretchr/testify/mock" + subpkg "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg" +) + +// Interface2WithResolvedAlias is an autogenerated mock type for the Interface2 type +type Interface2WithResolvedAlias struct { + mock.Mock +} + +type Interface2WithResolvedAlias_Expecter struct { + mock *mock.Mock +} + +func (_m *Interface2WithResolvedAlias) EXPECT() *Interface2WithResolvedAlias_Expecter { + return &Interface2WithResolvedAlias_Expecter{mock: &_m.Mock} +} + +// F provides a mock function with given fields: _a0, _a1, _a2 +func (_m *Interface2WithResolvedAlias) F(_a0 int, _a1 subpkg.S, _a2 subpkg.S) { + _m.Called(_a0, _a1, _a2) +} + +// Interface2WithResolvedAlias_F_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'F' +type Interface2WithResolvedAlias_F_Call struct { + *mock.Call +} + +// F is a helper method to define mock.On call +// - _a0 int +// - _a1 subpkg.S +// - _a2 subpkg.S +func (_e *Interface2WithResolvedAlias_Expecter) F(_a0 interface{}, _a1 interface{}, _a2 interface{}) *Interface2WithResolvedAlias_F_Call { + return &Interface2WithResolvedAlias_F_Call{Call: _e.mock.On("F", _a0, _a1, _a2)} +} + +func (_c *Interface2WithResolvedAlias_F_Call) Run(run func(_a0 int, _a1 subpkg.S, _a2 subpkg.S)) *Interface2WithResolvedAlias_F_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(int), args[1].(subpkg.S), args[2].(subpkg.S)) + }) + return _c +} + +func (_c *Interface2WithResolvedAlias_F_Call) Return() *Interface2WithResolvedAlias_F_Call { + _c.Call.Return() + return _c +} + +func (_c *Interface2WithResolvedAlias_F_Call) RunAndReturn(run func(int, subpkg.S, subpkg.S)) *Interface2WithResolvedAlias_F_Call { + _c.Call.Return(run) + return _c +} + +// NewInterface2WithResolvedAlias creates a new instance of Interface2WithResolvedAlias. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewInterface2WithResolvedAlias(t interface { + mock.TestingT + Cleanup(func()) +}) *Interface2WithResolvedAlias { + mock := &Interface2WithResolvedAlias{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go b/pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go new file mode 100644 index 00000000..68b64cf0 --- /dev/null +++ b/pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go @@ -0,0 +1,71 @@ +// Code generated by mockery. DO NOT EDIT. + +package type_alias_test + +import ( + mock "github.com/stretchr/testify/mock" + type_alias "github.com/vektra/mockery/v2/pkg/fixtures/type_alias" + subpkg "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg" +) + +// Interface2WithUnresolvedAlias is an autogenerated mock type for the Interface2 type +type Interface2WithUnresolvedAlias struct { + mock.Mock +} + +type Interface2WithUnresolvedAlias_Expecter struct { + mock *mock.Mock +} + +func (_m *Interface2WithUnresolvedAlias) EXPECT() *Interface2WithUnresolvedAlias_Expecter { + return &Interface2WithUnresolvedAlias_Expecter{mock: &_m.Mock} +} + +// F provides a mock function with given fields: _a0, _a1, _a2 +func (_m *Interface2WithUnresolvedAlias) F(_a0 type_alias.Type, _a1 type_alias.S, _a2 subpkg.S) { + _m.Called(_a0, _a1, _a2) +} + +// Interface2WithUnresolvedAlias_F_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'F' +type Interface2WithUnresolvedAlias_F_Call struct { + *mock.Call +} + +// F is a helper method to define mock.On call +// - _a0 type_alias.Type +// - _a1 type_alias.S +// - _a2 subpkg.S +func (_e *Interface2WithUnresolvedAlias_Expecter) F(_a0 interface{}, _a1 interface{}, _a2 interface{}) *Interface2WithUnresolvedAlias_F_Call { + return &Interface2WithUnresolvedAlias_F_Call{Call: _e.mock.On("F", _a0, _a1, _a2)} +} + +func (_c *Interface2WithUnresolvedAlias_F_Call) Run(run func(_a0 type_alias.Type, _a1 type_alias.S, _a2 subpkg.S)) *Interface2WithUnresolvedAlias_F_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(type_alias.Type), args[1].(type_alias.S), args[2].(subpkg.S)) + }) + return _c +} + +func (_c *Interface2WithUnresolvedAlias_F_Call) Return() *Interface2WithUnresolvedAlias_F_Call { + _c.Call.Return() + return _c +} + +func (_c *Interface2WithUnresolvedAlias_F_Call) RunAndReturn(run func(type_alias.Type, type_alias.S, subpkg.S)) *Interface2WithUnresolvedAlias_F_Call { + _c.Call.Return(run) + return _c +} + +// NewInterface2WithUnresolvedAlias creates a new instance of Interface2WithUnresolvedAlias. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewInterface2WithUnresolvedAlias(t interface { + mock.TestingT + Cleanup(func()) +}) *Interface2WithUnresolvedAlias { + mock := &Interface2WithUnresolvedAlias{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/pkg/fixtures/type_alias/subpkg/interface.go b/pkg/fixtures/type_alias/subpkg/interface.go new file mode 100644 index 00000000..cf7807c3 --- /dev/null +++ b/pkg/fixtures/type_alias/subpkg/interface.go @@ -0,0 +1,5 @@ +package subpkg + +type S struct { + A int +} diff --git a/pkg/generator.go b/pkg/generator.go index ae2ca81d..70587966 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -547,8 +547,7 @@ func (g *Generator) renderType(ctx context.Context, typ types.Type) string { "url": logging.DocsURL("/deprecations/#resolve-type-alias"), }, ) - log.Debug().Msg("resolving type alias to underlying type") - return g.renderType(ctx, t.Underlying()) + return g.renderType(ctx, t.Rhs()) } log.Debug().Msg("not resolving type alias to underlying type") return g.renderNamedType(ctx, t) From 198df07844efd9ed2768fa9f0a31860f29db0065 Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:48:13 -0600 Subject: [PATCH 2/3] formatting --- pkg/fixtures/type_alias/interface.go | 6 ++++-- tools/cmd/tag.go | 10 ++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pkg/fixtures/type_alias/interface.go b/pkg/fixtures/type_alias/interface.go index 5d0e8a08..171c1e67 100644 --- a/pkg/fixtures/type_alias/interface.go +++ b/pkg/fixtures/type_alias/interface.go @@ -2,8 +2,10 @@ package type_alias import "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg" -type Type = int -type S = subpkg.S +type ( + Type = int + S = subpkg.S +) type Interface1 interface { Foo() Type diff --git a/tools/cmd/tag.go b/tools/cmd/tag.go index 4a2861c3..04b69bed 100644 --- a/tools/cmd/tag.go +++ b/tools/cmd/tag.go @@ -15,13 +15,9 @@ import ( "github.com/spf13/viper" ) -var ( - ErrNoNewVersion = errors.New("no new version specified") -) +var ErrNoNewVersion = errors.New("no new version specified") -var ( - EXIT_CODE_NO_NEW_VERSION = 8 -) +var EXIT_CODE_NO_NEW_VERSION = 8 func NewTagCmd(v *viper.Viper) (*cobra.Command, error) { if err := v.ReadInConfig(); err != nil { @@ -121,7 +117,6 @@ func (t *Tagger) largestTagSemver(repo *git.Repository) (*semver.Version, error) largestTag = version } return nil - }); err != nil { return nil, err } @@ -201,5 +196,4 @@ func (t *Tagger) Tag() error { logger.Info().Msg("created new tag. Push to origin still required.") return nil - } From b1145cb049ea97881a3d517a0443832e12241ed6 Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:51:06 -0600 Subject: [PATCH 3/3] bump debug --- mockery-tools.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockery-tools.env b/mockery-tools.env index 809b21c2..9abcce5a 100644 --- a/mockery-tools.env +++ b/mockery-tools.env @@ -1 +1 @@ -VERSION=v2.49.0 \ No newline at end of file +VERSION=v2.49.1 \ No newline at end of file