Skip to content

Commit

Permalink
Add full .mockery.yaml file, fix a number of bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Dec 30, 2024
1 parent a90446c commit 870a9f9
Show file tree
Hide file tree
Showing 73 changed files with 4,451 additions and 562 deletions.
153 changes: 72 additions & 81 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
mockname: "Mock{{.InterfaceName}}"
filename: "mockery_mock.go"
template: "mockery"
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}_mock.go"
pkgname: mocks
tags: "custom2"
issue-845-fix: True
template: "mockery"
boilerplate-file: "./.boilerplate.txt"
mock-build-tags: test
_anchors: &inpackage_config
all: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.PackageName}}_test"
filename: "mock_{{.InterfaceNameSnake}}_test.go"
pkgname: "{{.SrcPackageName}}_test"
filename: "mock_{{.InterfaceName | snakecase }}_test.go"
packages:
#github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment:
# config:
# mock-build-tags: "custom3 && (!windows || !darwin || !freebsd)"
# disable-version-string: true
# interfaces:
# IfaceWithCustomBuildTagInComment:
#github.com/vektra/mockery/v2/pkg:
# interfaces:
# TypesPackage:
github.com/vektra/mockery/v2/pkg/fixtures/buildtag/comment:
config:
mock-build-tags: "custom3 && (!windows || !darwin || !freebsd)"
interfaces:
IfaceWithCustomBuildTagInComment:
github.com/vektra/mockery/v2/pkg:
interfaces:
TypesPackage:
github.com/vektra/mockery/v2/pkg/fixtures:
config:
all: False
all: True
interfaces:
A:
RequesterGenerics:
#RequesterArgSameAsNamedImport:
#RequesterVariadic:
# configs:
# - mockname: RequesterVariadicOneArgument
# unroll-variadic: False
# - mockname: RequesterVariadic
# unroll-variadic: True
RequesterArgSameAsNamedImport:
RequesterVariadic:
configs:
- mockname: RequesterVariadicOneArgument
template-data:
unroll-variadic: False
- mockname: RequesterVariadic
template-data:
unroll-variadic: True
Expecter:
configs:
- mockname: ExpecterAndRolledVariadic
Expand All @@ -43,57 +39,52 @@ packages:
- mockname: Expecter
template-data:
unroll-variadic: True
#RequesterReturnElided:
#VariadicNoReturnInterface:
# config:
# unroll-variadic: False
#ReplaceGeneric:
#ReplaceGenericSelf:
#github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
# config:
# recursive: True
# all: True
# dir: "{{.InterfaceDir}}"
# filename: "{{.InterfaceName}}_mock.go"
# mockname: "Mock{{.InterfaceName}}"
# pkgname: "{{.PackageName}}"
#github.com/vektra/mockery/v2/pkg/fixtures/empty_return:
# config:
# all: True
# dir: "{{.InterfaceDir}}"
# mockname: "{{.InterfaceName}}Mock"
# pkgname: "{{.PackageName}}"
# filename: "mock_{{.InterfaceName}}_test.go"
#github.com/vektra/mockery/v2/pkg/fixtures/method_args/same_name_arg_and_type:
# config:
# all: True
# dir: "{{.InterfaceDir}}"
# mockname: "{{.InterfaceName}}Mock"
# pkgname: "{{.PackageName}}"
# filename: "mock_{{.InterfaceName}}_test.go"
#github.com/vektra/mockery/v2/pkg/fixtures/iface_typed_param:
# config: *inpackage_config
#github.com/vektra/mockery/v2/pkg/fixtures/example_project:
# config: *inpackage_config
#github.com/vektra/mockery/v2/pkg/fixtures/index_list_expr:
# config: *inpackage_config
#github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type:
# config: *inpackage_config
#github.com/vektra/mockery/v2/pkg/fixtures/issue845:
# config:
# <<: *inpackage_config
# filename: "mock_{{.MockName}}_test.go"
# interfaces:
# Interface:
# configs:
# - issue-845-fix: False
# 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"
# pkgname: "{{.PackageName}}_test"
#
RequesterReturnElided:
VariadicNoReturnInterface:
config:
template-data:
unroll-variadic: False
github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
config:
recursive: True
all: True
dir: "{{.InterfaceDir}}"
filename: "{{.InterfaceName}}_mock.go"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.SrcPackageName}}"
github.com/vektra/mockery/v2/pkg/fixtures/empty_return:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
pkgname: "{{.SrcPackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
github.com/vektra/mockery/v2/pkg/fixtures/method_args/same_name_arg_and_type:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
pkgname: "{{.SrcPackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
github.com/vektra/mockery/v2/pkg/fixtures/iface_typed_param:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/example_project:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/index_list_expr:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/issue845:
config:
<<: *inpackage_config
filename: "mock_{{.MockName}}_test.go"
interfaces:
Interface:
configs:
- mockname: WithFix
github.com/vektra/mockery/v2/pkg/fixtures/type_alias:
config:
all: True
dir: "{{.InterfaceDir}}"
filename: "mock_{{.MockName}}_test.go"
pkgname: "{{.SrcPackageName}}_test"
2 changes: 1 addition & 1 deletion .mockery_moq.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mockname: "{{.InterfaceName}}Mock"
filename: "moq_test.go"
template: moq
dir: "mocks/moq/{{.PackagePath}}"
dir: "mocks/moq/{{.SrcPackagePath}}"
formatter: "goimports"

packages:
Expand Down
25 changes: 20 additions & 5 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/vektra/mockery/v2/pkg"
"github.com/vektra/mockery/v2/pkg/logging"
"github.com/vektra/mockery/v2/pkg/stackerr"
"golang.org/x/tools/go/packages"
)

var (
Expand Down Expand Up @@ -172,14 +173,21 @@ type InterfaceCollection struct {
srcPkgPath string
outPkgPath string
outFilePath *pathlib.Path
srcPkg *packages.Package
interfaces []*pkg.Interface
}

func NewInterfaceCollection(srcPkgPath string, outPkgPath string, outFilePath *pathlib.Path) *InterfaceCollection {
func NewInterfaceCollection(
srcPkgPath string,
outPkgPath string,
outFilePath *pathlib.Path,
srcPkg *packages.Package,
) *InterfaceCollection {
return &InterfaceCollection{
srcPkgPath: srcPkgPath,
outPkgPath: outPkgPath,
outFilePath: outFilePath,
srcPkg: srcPkg,
interfaces: make([]*pkg.Interface, 0),
}
}
Expand Down Expand Up @@ -280,7 +288,7 @@ func (r *RootApp) Run() error {
return err
}
for _, ifaceConfig := range ifaceConfigs {
if err := ifaceConfig.ParseTemplates(ctx, iface); err != nil {
if err := ifaceConfig.ParseTemplates(ctx, iface, iface.Pkg); err != nil {
log.Err(err).Msg("Can't parse config templates for interface")
return err
}
Expand All @@ -296,6 +304,7 @@ func (r *RootApp) Run() error {
iface.Pkg.PkgPath,
outPkgPath,
pathlib.NewPath(ifaceConfig.Dir).Join(ifaceConfig.FileName),
iface.Pkg,
)
}
mockFileToInterfaces[filePath].Append(
Expand All @@ -311,13 +320,19 @@ func (r *RootApp) Run() error {
}

for outFilePath, interfacesInFile := range mockFileToInterfaces {
log.Debug().Int("interfaces-in-file-len", len(interfacesInFile.interfaces)).Msgf("%v", interfacesInFile)
fileLog := log.With().Str("file", outFilePath).Logger()
fileCtx := fileLog.WithContext(ctx)

fileLog.Debug().Int("interfaces-in-file-len", len(interfacesInFile.interfaces)).Msgf("%v", interfacesInFile)
outPkgPath := interfacesInFile.outPkgPath

packageConfig, err := r.Config.GetPackageConfig(ctx, interfacesInFile.srcPkgPath)
packageConfig, err := r.Config.GetPackageConfig(fileCtx, interfacesInFile.srcPkgPath)
if err != nil {
return err
}
if err := packageConfig.ParseTemplates(ctx, nil, interfacesInFile.srcPkg); err != nil {
return err
}
generator, err := pkg.NewTemplateGenerator(
interfacesInFile.interfaces[0].Pkg,
outPkgPath,
Expand All @@ -328,7 +343,7 @@ func (r *RootApp) Run() error {
if err != nil {
return err
}
templateBytes, err := generator.Generate(ctx, interfacesInFile.interfaces)
templateBytes, err := generator.Generate(fileCtx, interfacesInFile.interfaces)
if err != nil {
return err
}
Expand Down

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

Loading

0 comments on commit 870a9f9

Please sign in to comment.