Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Nov 19, 2024
1 parent c5ff306 commit 4ced14b
Show file tree
Hide file tree
Showing 65 changed files with 253 additions and 72 deletions.
17 changes: 16 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ quiet: False
disable-version-string: True
with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}.go"
filename: "{{.MockName}}_mock.go"
outpkg: mocks
tags: "custom2"
issue-845-fix: True
Expand Down Expand Up @@ -94,3 +94,18 @@ packages:
mockname: WithoutFix
- issue-845-fix: True
mockname: WithFix
github.com/vektra/mockery/v2/pkg/fixtures/type_alias:
config:
all: True
dir: "{{.InterfaceDir}}"
filename: "mock_{{.MockName}}_test.go"
outpkg: "fuck"
inpackage: True
interfaces:
Interface1:
configs:
- resolve-type-alias: False
mockname: InterfaceWithUnresolvedAlias
- resolve-type-alias: True
mockname: InterfaceWithResolvedAlias

2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tasks:
mocks.remove:
desc: remove all mock files
cmds:
- find . -name '*_mock.go' | xargs -r rm
- find . -name '*_mock.go' -o -name 'mock_*_test.go' | xargs -r rm
- rm -rf mocks/

mocks.generate:
Expand Down
7 changes: 7 additions & 0 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ func (r *RootApp) Run() error {
}
if len(configuredPackages) != 0 {
r.Config.LogUnsupportedPackagesConfig(ctx)
if !r.Config.Issue845Fix {
warnDeprecated(
ctx,
"issue-845-fix must be set to True to remove this warning. Visit the link for more details."

Check failure on line 237 in cmd/mockery.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.22)

missing ',' before newline in argument list

Check failure on line 237 in cmd/mockery.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 1.23)

missing ',' before newline in argument list
)
}

configuredPackages, err := r.Config.GetPackages(ctx)
if err != nil {
Expand Down Expand Up @@ -377,6 +383,7 @@ func (r *RootApp) Run() error {
UnrollVariadic: r.Config.UnrollVariadic,
WithExpecter: r.Config.WithExpecter,
ReplaceType: r.Config.ReplaceType,
ResolveTypeAlias: r.Config.ResolveTypeAlias,
}, osp, r.Config.DryRun)

generated := walker.Walk(ctx, visitor)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Config struct {
Issue845Fix bool `mapstructure:"issue-845-fix"`
IncludeAutoGenerated bool `mapstructure:"include-auto-generated"`
IncludeRegex string `mapstructure:"include-regex"`
Issue845Fix bool `mapstructure:"issue-845-fix"`
KeepTree bool `mapstructure:"keeptree"`
LogLevel string `mapstructure:"log-level"`
MockBuildTags string `mapstructure:"mock-build-tags"`
Expand Down
69 changes: 0 additions & 69 deletions pkg/fixtures/example_project/mock_Stringer_test.go

This file was deleted.

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

type Type = int

type Interface1 interface {
Foo() Type
}
56 changes: 56 additions & 0 deletions pkg/fixtures/type_alias/interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package type_alias_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/vektra/mockery/v2/pkg"
"golang.org/x/tools/go/packages"
)

func TestTypeAliasResolved(t *testing.T) {
parser := pkg.NewParser([]string{})
require.NoError(t, parser.ParsePackages(context.Background(), []string{"github.com/vektra/mockery/v2/fixtures/type_alias"}))
var config packages.Config
config.Mode = packages.NeedTypes |
packages.NeedTypesSizes |
packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedImports |
packages.NeedName |
packages.NeedFiles |
packages.NeedCompiledGoFiles
config.Tests = true
packageList, err := packages.Load(&config, "github.com/vektra/mockery/v2/pkg/fixtures/type_alias")
require.NoError(t, err)
require.Equal(t, 3, len(packageList))

//var pkg *packages.Package

for _, pkgElem := range packageList {
t.Log(pkgElem.ID)
}

//pkg := packageList[0]
//for _, file := range pkg.Syntax {
// t.Log("FOUND FILE")
// t.Log(file.Name.Name)
// for _, decl := range file.Decls {
// t.Logf("%v", decl)
// }
//}
t.Fail()

}

//func TestTypeAliasUnResolved(t *testing.T) {
// expectedReturn := Type(1)
// mockInterface := NewInterfaceWithUnresolvedAlias(t)
// mockInterface.EXPECT().Foo().Return(expectedReturn)
//
// actualReturn := mockInterface.Foo()
// assert.Equal(t, "foo", reflect.TypeOf(actualReturn).String())
// assert.Equal(t, expectedReturn, actualReturn)
//
//}
77 changes: 77 additions & 0 deletions pkg/fixtures/type_alias/mock_InterfaceWithResolvedAlias_test.go

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

80 changes: 80 additions & 0 deletions pkg/fixtures/type_alias/mock_InterfaceWithUnresolvedAlias_test.go

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

4 changes: 4 additions & 0 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,17 @@ func (g *Generator) renderNamedType(ctx context.Context, t interface {
}

func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
log := zerolog.Ctx(ctx)
switch t := typ.(type) {
case *types.Named:
return g.renderNamedType(ctx, t)
case *types.Alias:
log.Debug().Msg("found type alias")
if g.config.ResolveTypeAlias {
log.Debug().Msg("resolving type alias to underlying type")
return g.renderType(ctx, t.Underlying())
}
log.Debug().Msg("not resolving type alias to underlying type")
return g.renderNamedType(ctx, t)
case *types.TypeParam:
if t.Constraint() != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error {
UnrollVariadic: interfaceConfig.UnrollVariadic,
WithExpecter: interfaceConfig.WithExpecter,
ReplaceType: interfaceConfig.ReplaceType,
ResolveTypeAlias: interfaceConfig.ResolveTypeAlias,
}
generator := NewGenerator(ctx, g, iface, "")
generator := NewGenerator(ctx, g, iface, interfaceConfig.Outpkg)

log.Debug().Msg("generating mock in-memory")
if err := generator.GenerateAll(ctx); err != nil {
Expand Down
Loading

0 comments on commit 4ced14b

Please sign in to comment.