Skip to content
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

Fix bug with type aliases to structs #853

Merged
merged 3 commits into from
Nov 26, 2024
Merged
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
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion mockery-tools.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=v2.49.0
VERSION=v2.49.1
11 changes: 10 additions & 1 deletion pkg/fixtures/type_alias/interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package type_alias

type Type = int
import "github.com/vektra/mockery/v2/pkg/fixtures/type_alias/subpkg"

type (
Type = int
S = subpkg.S
)

type Interface1 interface {
Foo() Type
}

type Interface2 interface {
F(Type, S, subpkg.S)
}
5 changes: 5 additions & 0 deletions pkg/fixtures/type_alias/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 70 additions & 0 deletions pkg/fixtures/type_alias/mock_Interface2WithResolvedAlias_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions pkg/fixtures/type_alias/mock_Interface2WithUnresolvedAlias_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/fixtures/type_alias/subpkg/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subpkg

type S struct {
A int
}
3 changes: 1 addition & 2 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions tools/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -121,7 +117,6 @@ func (t *Tagger) largestTagSemver(repo *git.Repository) (*semver.Version, error)
largestTag = version
}
return nil

}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -201,5 +196,4 @@ func (t *Tagger) Tag() error {
logger.Info().Msg("created new tag. Push to origin still required.")

return nil

}