Skip to content

Commit

Permalink
Minimally working v3 implementation of moq
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Dec 26, 2024
1 parent fce2e5c commit 82d3313
Show file tree
Hide file tree
Showing 143 changed files with 6,402 additions and 10,659 deletions.
265 changes: 140 additions & 125 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,129 +1,144 @@
quiet: False
disable-version-string: True
with-expecter: True
mockname: "{{.InterfaceName}}"
filename: "{{.MockName}}_mock.go"
outpkg: mocks
tags: "custom2"
issue-845-fix: True
resolve-type-alias: False
_anchors: &inpackage_config
all: True
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}_test"
filename: "mock_{{.InterfaceNameSnake}}_test.go"
inpackage: False
mockname: "{{.InterfaceName}}Mock"
filename: "{{.InterfaceName}}.go"
dir: "mocks/moq/{{.PackagePath}}"
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:
config:
all: True
interfaces:
RequesterArgSameAsNamedImport:
RequesterVariadic:
config:
with-expecter: False
configs:
- mockname: RequesterVariadicOneArgument
unroll-variadic: False
- mockname: RequesterVariadic
unroll-variadic: True
Expecter:
config:
with-expecter: True
configs:
- mockname: ExpecterAndRolledVariadic
unroll-variadic: False
- mockname: Expecter
unroll-variadic: True
RequesterReturnElided:
VariadicNoReturnInterface:
config:
with-expecter: True
unroll-variadic: False
# Replace generic params with a new constraint and a new fixed value
ReplaceGeneric:
config:
replace-type:
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[-TImport]=github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b.B
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.String
# Replace a generic param with the parent type
ReplaceGenericSelf:
config:
replace-type:
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGenericSelf[-T]=github.com/vektra/mockery/v2/pkg/fixtures.*ReplaceGenericSelf
github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
config:
recursive: True
all: True
dir: "{{.InterfaceDir}}"
filename: "{{.InterfaceName}}_mock.go"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
inpackage: True
github.com/vektra/mockery/v2/pkg/fixtures/empty_return:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
outpkg: "{{.PackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
inpackage: True
keeptree: False
github.com/vektra/mockery/v2/pkg/fixtures/method_args/same_name_arg_and_type:
config:
all: True
dir: "{{.InterfaceDir}}"
mockname: "{{.InterfaceName}}Mock"
outpkg: "{{.PackageName}}"
filename: "mock_{{.InterfaceName}}_test.go"
inpackage: True
keeptree: False
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"
outpkg: "{{.PackageName}}_test"
inpackage: False
interfaces:
Interface1:
configs:
- resolve-type-alias: False
mockname: InterfaceWithUnresolvedAlias
- resolve-type-alias: True
mockname: InterfaceWithResolvedAlias
Interface2:
configs:
- resolve-type-alias: False
mockname: Interface2WithUnresolvedAlias
- resolve-type-alias: True
mockname: Interface2WithResolvedAlias
include-regex: '.*'
exclude-regex: 'RequesterGenerics|UnsafeInterface|requester_unexported'
template: moq
outpkg: test
template-map:
with-resets: true
skip-ensure: true
stub-impl: false
#quiet: False
#disable-version-string: True
#with-expecter: True
#mockname: "{{.InterfaceName}}"
#filename: "{{.MockName}}_mock.go"
#outpkg: mocks
#tags: "custom2"
#issue-845-fix: True
#resolve-type-alias: False
#_anchors: &inpackage_config
# all: True
# dir: "{{.InterfaceDir}}"
# mockname: "Mock{{.InterfaceName}}"
# outpkg: "{{.PackageName}}_test"
# filename: "mock_{{.InterfaceNameSnake}}_test.go"
# inpackage: False
#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:
# config:
# all: True
# interfaces:
# RequesterArgSameAsNamedImport:
# RequesterVariadic:
# config:
# with-expecter: False
# configs:
# - mockname: RequesterVariadicOneArgument
# unroll-variadic: False
# - mockname: RequesterVariadic
# unroll-variadic: True
# Expecter:
# config:
# with-expecter: True
# configs:
# - mockname: ExpecterAndRolledVariadic
# unroll-variadic: False
# - mockname: Expecter
# unroll-variadic: True
# RequesterReturnElided:
# VariadicNoReturnInterface:
# config:
# with-expecter: True
# unroll-variadic: False
# # Replace generic params with a new constraint and a new fixed value
# ReplaceGeneric:
# config:
# replace-type:
# - github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[-TImport]=github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b.B
# - github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.String
# # Replace a generic param with the parent type
# ReplaceGenericSelf:
# config:
# replace-type:
# - github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGenericSelf[-T]=github.com/vektra/mockery/v2/pkg/fixtures.*ReplaceGenericSelf
# github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
# config:
# recursive: True
# all: True
# dir: "{{.InterfaceDir}}"
# filename: "{{.InterfaceName}}_mock.go"
# mockname: "Mock{{.InterfaceName}}"
# outpkg: "{{.PackageName}}"
# inpackage: True
# github.com/vektra/mockery/v2/pkg/fixtures/empty_return:
# config:
# all: True
# dir: "{{.InterfaceDir}}"
# mockname: "{{.InterfaceName}}Mock"
# outpkg: "{{.PackageName}}"
# filename: "mock_{{.InterfaceName}}_test.go"
# inpackage: True
# keeptree: False
# github.com/vektra/mockery/v2/pkg/fixtures/method_args/same_name_arg_and_type:
# config:
# all: True
# dir: "{{.InterfaceDir}}"
# mockname: "{{.InterfaceName}}Mock"
# outpkg: "{{.PackageName}}"
# filename: "mock_{{.InterfaceName}}_test.go"
# inpackage: True
# keeptree: False
# 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"
# outpkg: "{{.PackageName}}_test"
# inpackage: False
# interfaces:
# Interface1:
# configs:
# - resolve-type-alias: False
# mockname: InterfaceWithUnresolvedAlias
# - resolve-type-alias: True
# mockname: InterfaceWithResolvedAlias
# Interface2:
# configs:
# - resolve-type-alias: False
# mockname: Interface2WithUnresolvedAlias
# - resolve-type-alias: True
# mockname: Interface2WithResolvedAlias
24 changes: 6 additions & 18 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,15 @@ func NewRootCmd() *cobra.Command {

pFlags := cmd.PersistentFlags()
pFlags.StringVar(&cfgFile, "config", "", "config file to use")
pFlags.String("name", "", "name or matching regular expression of interface to generate mock for")
pFlags.Bool("print", false, "print the generated mock to stdout")
pFlags.String("output", "", "directory to write mocks to")
pFlags.String("outpkg", "mocks", "name of generated package")
pFlags.String("packageprefix", "", "prefix for the generated package name, it is ignored if outpkg is also specified.")
pFlags.String("dir", "", "directory to search for interfaces")
pFlags.BoolP("recursive", "r", false, "recurse search into sub-directories")
pFlags.StringArray("exclude", nil, "prefixes of subdirectories and files to exclude from search")
pFlags.Bool("all", false, "generates mocks for all found interfaces in all sub-directories")
pFlags.Bool("inpackage", false, "generate a mock that goes inside the original package")
pFlags.Bool("inpackage-suffix", false, "use filename '_mock' suffix instead of 'mock_' prefix for InPackage mocks")
pFlags.Bool("testonly", false, "generate a mock in a _test.go file")
pFlags.String("case", "", "name the mocked file using casing convention [camel, snake, underscore]")
pFlags.String("note", "", "comment to insert into prologue of each generated file")
pFlags.String("cpuprofile", "", "write cpu profile to file")
pFlags.Bool("version", false, "prints the installed version of mockery")
pFlags.Bool("quiet", false, `suppresses logger output (equivalent to --log-level="")`)
pFlags.Bool("keeptree", false, "keep the tree structure of the original interface files into a different repository. Must be used with XX")
pFlags.String("tags", "", "space-separated list of additional build tags to load packages")
pFlags.String("mock-build-tags", "", "set the build tags of the generated mocks. Read more about the format: https://pkg.go.dev/cmd/go#hdr-Build_constraints")
pFlags.String("filename", "", "name of generated file (only works with -name and no regex)")
Expand All @@ -74,10 +65,6 @@ func NewRootCmd() *cobra.Command {
pFlags.Bool("disable-version-string", false, "Do not insert the version string into the generated mock file.")
pFlags.String("boilerplate-file", "", "File to read a boilerplate text from. Text should be a go block comment, i.e. /* ... */")
pFlags.Bool("unroll-variadic", true, "For functions with variadic arguments, do not unroll the arguments into the underlying testify call. Instead, pass variadic slice as-is.")
pFlags.Bool("exported", false, "Generates public mocks for private interfaces.")
pFlags.Bool("with-expecter", false, "Generate expecter utility around mock's On, Run and Return methods with explicit types. This option is NOT compatible with -unroll-variadic=false")
pFlags.StringArray("replace-type", nil, "Replace types")
pFlags.Bool("disable-func-mocks", false, "Disable generation of function mocks.")

if err := viperCfg.BindPFlags(pFlags); err != nil {
panic(fmt.Sprintf("failed to bind PFlags: %v", err))
Expand Down Expand Up @@ -214,23 +201,24 @@ func (r *RootApp) Run() error {
log.Error().Msg("no packages specified in config")
return nil
}
parser := pkg.NewParser(buildTags, pkg.ParserDisableFuncMocks(r.Config.DisableFuncMocks))
parser := pkg.NewParser(buildTags)

if err := parser.ParsePackages(ctx, configuredPackages); err != nil {
interfaces, err := parser.ParsePackages(ctx, configuredPackages)
if err != nil {
log.Error().Err(err).Msg("unable to parse packages")
return err
}
log.Info().Msg("done loading, visiting interface nodes")
for _, iface := range parser.Interfaces() {
for _, iface := range interfaces {
ifaceLog := log.
With().
Str(logging.LogKeyInterface, iface.Name).
Str(logging.LogKeyQualifiedName, iface.QualifiedName).
Str(logging.LogKeyQualifiedName, iface.Pkg.Types.Path()).
Logger()

ifaceCtx := ifaceLog.WithContext(ctx)

shouldGenerate, err := r.Config.ShouldGenerateInterface(ifaceCtx, iface.QualifiedName, iface.Name)
shouldGenerate, err := r.Config.ShouldGenerateInterface(ifaceCtx, iface.Pkg.Types.Path(), iface.Name)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 82d3313

Please sign in to comment.