diff --git a/.mockery.yaml b/.mockery.yaml index 4430ecdc..10886968 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -1,147 +1,92 @@ disable-version-string: True -mockname: "{{.InterfaceName}}Mock" -filename: "moq_test.go" -template: moq -dir: "mocks/moq/{{.PackagePath}}" -formatter: "goimports" - +mockname: "{{.InterfaceName}}" +filename: "{{.MockName}}_mock.go" +outpkg: mocks +tags: "custom2" +issue-845-fix: True +_anchors: &inpackage_config + all: True + dir: "{{.InterfaceDir}}" + mockname: "Mock{{.InterfaceName}}" + outpkg: "{{.PackageName}}_test" + filename: "mock_{{.InterfaceNameSnake}}_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: config: - include-regex: '.*' - exclude-regex: 'RequesterGenerics|UnsafeInterface|requester_unexported' - outpkg: test - filename: "moq_fixtures_test.go" - template-data: - 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 + all: True + interfaces: + RequesterArgSameAsNamedImport: + RequesterVariadic: + configs: + - mockname: RequesterVariadicOneArgument + unroll-variadic: False + - mockname: RequesterVariadic + unroll-variadic: True + Expecter: + configs: + - mockname: ExpecterAndRolledVariadic + unroll-variadic: False + - mockname: Expecter + 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}}" + outpkg: "{{.PackageName}}" + github.com/vektra/mockery/v2/pkg/fixtures/empty_return: + config: + all: True + dir: "{{.InterfaceDir}}" + mockname: "{{.InterfaceName}}Mock" + outpkg: "{{.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" + outpkg: "{{.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" + outpkg: "{{.PackageName}}_test" diff --git a/.mockery_moq.yaml b/.mockery_moq.yaml new file mode 100644 index 00000000..d84eaca6 --- /dev/null +++ b/.mockery_moq.yaml @@ -0,0 +1,17 @@ +disable-version-string: True +mockname: "{{.InterfaceName}}Mock" +filename: "moq_test.go" +template: moq +dir: "mocks/moq/{{.PackagePath}}" +formatter: "goimports" + +packages: + github.com/vektra/mockery/v2/pkg/fixtures: + config: + include-regex: '.*' + exclude-regex: 'RequesterGenerics|UnsafeInterface|requester_unexported' + outpkg: test + template-data: + with-resets: true + skip-ensure: true + stub-impl: false diff --git a/Taskfile.yml b/Taskfile.yml index 18026996..9e9c2a0b 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -24,10 +24,21 @@ tasks: - find . -name '*_mock.go' -o -name 'mock_*_test.go' | xargs -r rm - rm -rf mocks/ - mocks.generate: - desc: generate mockery mocks + mocks.generate.mockery: + silent: True + cmds: + - MOCKERY_CONFIG=./.mockery.yaml go run . + + mocks.generate.moq: + silent: True cmds: - - go run . + - MOCKERY_CONFIG=./.mockery_moq.yaml go run . + + mocks.generate: + desc: generate mocks + deps: + - mocks.generate.mockery + - mocks.generate.moq docker: desc: build the mockery docker image diff --git a/cmd/mockery.go b/cmd/mockery.go index 3934faeb..922f4d97 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -19,16 +19,14 @@ import ( ) var ( - cfgFile = "" - viperCfg *viper.Viper + cfgFile = "" ) -func init() { - cobra.OnInitialize(func() { initConfig(nil, viperCfg, nil) }) -} - -func NewRootCmd() *cobra.Command { - viperCfg = viper.NewWithOptions(viper.KeyDelimiter("::")) +func NewRootCmd() (*cobra.Command, error) { + viperCfg, err := getConfig(nil, nil) + if err != nil { + return nil, err + } cmd := &cobra.Command{ Use: "mockery", Short: "Generate mock objects for your Golang interfaces", @@ -72,7 +70,7 @@ func NewRootCmd() *cobra.Command { } cmd.AddCommand(NewShowConfigCmd()) - return cmd + return cmd, nil } func printStackTrace(e error) { @@ -85,16 +83,20 @@ func printStackTrace(e error) { // Execute executes the cobra CLI workflow func Execute() { - if err := NewRootCmd().Execute(); err != nil { + cmd, err := NewRootCmd() + if err != nil { + os.Exit(1) + } + if cmd.Execute(); err != nil { os.Exit(1) } } -func initConfig( +func getConfig( baseSearchPath *pathlib.Path, - viperObj *viper.Viper, configPath *pathlib.Path, -) *viper.Viper { +) (*viper.Viper, error) { + viperObj := viper.NewWithOptions(viper.KeyDelimiter("::")) if baseSearchPath == nil { currentWorkingDir, err := os.Getwd() if err != nil { @@ -140,12 +142,13 @@ func initConfig( } if err := viperObj.ReadInConfig(); err != nil { log, _ := logging.GetLogger("debug") - log.Info().Msg("couldn't read any config file") + log.Err(err).Msg("couldn't read any config file") + return nil, err } } viperObj.Set("config", viperObj.ConfigFileUsed()) - return viperObj + return viperObj, nil } const regexMetadataChars = "\\.+*?()|[]{}^$" diff --git a/cmd/mockery_test.go b/cmd/mockery_test.go index 014afdd7..982e334e 100644 --- a/cmd/mockery_test.go +++ b/cmd/mockery_test.go @@ -52,7 +52,7 @@ func Test_initConfig(t *testing.T) { viperObj := viper.New() - initConfig(baseDir, viperObj, nil) + getConfig(baseDir, viperObj, nil) assert.Equal(t, configPath.String(), viperObj.ConfigFileUsed()) }) @@ -72,7 +72,7 @@ name: Foo require.NoError(t, configPath.WriteFile([]byte(config))) v := viper.New() - initConfig(nil, v, configPath) + getConfig(nil, v, configPath) app, err := GetRootAppFromViper(v) require.NoError(t, err) assert.Error(t, app.Run()) @@ -102,7 +102,7 @@ packages: mockPath := pathlib.NewPath(tmpDir).Join("hello_Writer.go") v := newViper(tmpDir) - initConfig(nil, v, configPath) + getConfig(nil, v, configPath) app, err := GetRootAppFromViper(v) require.NoError(t, err) require.NoError(t, app.Run()) @@ -130,7 +130,7 @@ packages: mockPath := pathlib.NewPath(tmpDir).Join("mock_Writer.go") v := newViper(tmpDir) - initConfig(nil, v, configPath) + getConfig(nil, v, configPath) app, err := GetRootAppFromViper(v) require.NoError(t, err) require.NoError(t, app.Run()) @@ -189,7 +189,7 @@ type FooInterface interface { require.NoError(t, os.Chdir(tmpDir)) v := viper.New() - initConfig(nil, v, configPath) + getConfig(nil, v, configPath) app, err := GetRootAppFromViper(v) require.NoError(t, err) require.NoError(t, app.Run()) @@ -219,7 +219,7 @@ type Foo interface { v.Set("disable-config-search", true) require.NoError(t, os.Chdir(tmpDir.String())) - initConfig(nil, v, nil) + getConfig(nil, v, nil) app, err := GetRootAppFromViper(v) require.NoError(t, err) require.NoError(t, app.Run()) @@ -256,7 +256,7 @@ type Foo interface { v.Set("recursive", true) require.NoError(t, os.Chdir(tmpDir.String())) - initConfig(nil, v, nil) + getConfig(nil, v, nil) app, err := GetRootAppFromViper(v) require.NoError(t, err) require.NoError(t, app.Run()) diff --git a/cmd/showconfig.go b/cmd/showconfig.go index 8beb1f81..4eba8d84 100644 --- a/cmd/showconfig.go +++ b/cmd/showconfig.go @@ -19,7 +19,13 @@ func NewShowConfigCmd() *cobra.Command { Use: "showconfig", Short: "Show the yaml config", Long: `Print out a yaml representation of the yaml config file. This does not show config from exterior sources like CLI, environment etc.`, - RunE: func(cmd *cobra.Command, args []string) error { return showConfig(cmd, args, viperCfg, os.Stdout) }, + RunE: func(cmd *cobra.Command, args []string) error { + cfg, err := getConfig(nil, nil) + if err != nil { + return err + } + return showConfig(cmd, args, cfg, os.Stdout) + }, } } @@ -29,9 +35,6 @@ func showConfig( v *viper.Viper, outputter io.Writer, ) error { - if v == nil { - v = viperCfg - } ctx := context.Background() config, err := pkg.NewConfigFromViper(v) if err != nil { diff --git a/mocks/moq/github.com/vektra/mockery/v2/pkg/fixtures/moq_fixtures_test.go b/mocks/moq/github.com/vektra/mockery/v2/pkg/fixtures/moq_test.go similarity index 100% rename from mocks/moq/github.com/vektra/mockery/v2/pkg/fixtures/moq_fixtures_test.go rename to mocks/moq/github.com/vektra/mockery/v2/pkg/fixtures/moq_test.go diff --git a/pkg/template_generator.go b/pkg/template_generator.go index d9de10da..e4304922 100644 --- a/pkg/template_generator.go +++ b/pkg/template_generator.go @@ -136,8 +136,6 @@ func (g *TemplateGenerator) Generate( interfaces []*Interface, ) ([]byte, error) { log := zerolog.Ctx(ctx) - log.Info().Msg("generating templated mock for interface") - mockData := []template.MockData{} for _, ifaceMock := range interfaces { iface, tparams, err := g.registry.LookupInterface(ifaceMock.Name)