From e6add862c517390590a00fbf19db484d71d21a64 Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Fri, 4 Aug 2023 15:33:52 -0600 Subject: [PATCH] Add error log for unsupported config This commit will log an error message if unsupported config is being used. I have chosen not to cause mockery to return an error code because it's possible that configs using unsupported parameters are working just fine, so we don't want to break those users. Instead, the log message should hopefully get their attention, and the attention of anyone trying to migrate to packages. Also removed is the conditional used to enter into the `packages` code path. Previously it asserted that `Config.Name == ""` but this was causing more confusion than it needed to. This fixes #685 --- .mockery.yaml | 2 +- cmd/mockery.go | 14 +++++++++++--- pkg/config/config.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.mockery.yaml b/.mockery.yaml index af246cc3..89b676b3 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -1,10 +1,10 @@ quiet: False -keeptree: True disable-version-string: True with-expecter: True mockname: "{{.InterfaceName}}" filename: "{{.MockName}}.go" outpkg: mocks +name: foobar packages: github.com/vektra/mockery/v2/pkg: interfaces: diff --git a/cmd/mockery.go b/cmd/mockery.go index c02b7336..b65a9e77 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -54,7 +54,7 @@ func NewRootCmd() *cobra.Command { 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", "./mocks", "directory to write mocks to") + 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") @@ -64,7 +64,7 @@ func NewRootCmd() *cobra.Command { 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", "camel", "name the mocked file using casing convention [camel, snake, underscore]") + 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") @@ -230,7 +230,15 @@ func (r *RootApp) Run() error { if err != nil && !errors.Is(err, os.ErrNotExist) { return fmt.Errorf("failed to determine configured packages: %w", err) } - if len(configuredPackages) != 0 && r.Config.Name == "" { + if len(configuredPackages) != 0 { + // Check for unsupported config options + if unsupported := r.Config.CheckUnsupportedPackagesConfig(ctx); len(unsupported) != 0 { + l := log.With(). + Dict("unsupported-fields", zerolog.Dict().Fields(unsupported)). + Str("url", logging.DocsURL("/configuration/#parameter-descriptions")). + Logger() + l.Error().Msg("use of unsupported options detected. mockery behavior is undefined.") + } configuredPackages, err := r.Config.GetPackages(ctx) if err != nil { return fmt.Errorf("failed to get package from config: %w", err) diff --git a/pkg/config/config.go b/pkg/config/config.go index 66ce2774..595f66e7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -90,7 +90,9 @@ func NewConfigFromViper(v *viper.Viper) (*Config, error) { // Set defaults if len(packages) == 0 { + v.SetDefault("case", "camel") v.SetDefault("dir", ".") + v.SetDefault("output", "./mocks") } else { v.SetDefault("dir", "mocks/{{.PackagePath}}") v.SetDefault("filename", "mock_{{.InterfaceName}}.go") @@ -702,3 +704,33 @@ func (c *Config) getInterfacesForPackage(ctx context.Context, pkgPath string) ([ } return interfaces, nil } + +func (c *Config) TagName(name string) string { + field, ok := reflect.TypeOf(c).Elem().FieldByName(name) + if !ok { + panic(fmt.Sprintf("unknown config field: %s", name)) + } + return string(field.Tag.Get("mapstructure")) +} + +// CheckUnsupportedPackagesConfig is a method that will help aid migrations to the +// packages config feature. This is intended to be a temporary measure until v3 +// when we can remove all legacy config options. +func (c *Config) CheckUnsupportedPackagesConfig(ctx context.Context) map[string]any { + unsupportedOptions := make(map[string]any) + for _, name := range []string{"Name", "KeepTree", "Case", "Output", "TestOnly"} { + value := reflect.ValueOf(c).Elem().FieldByName(name) + var valueAsString string + if value.Kind().String() == "bool" { + valueAsString = fmt.Sprintf("%v", value.Bool()) + } + if value.Kind().String() == "string" { + valueAsString = value.String() + } + + if !value.IsZero() { + unsupportedOptions[c.TagName(name)] = valueAsString + } + } + return unsupportedOptions +}