From 72eb14685bba87872bd06615d066e568233df6c7 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 | 1 - cmd/mockery.go | 8 +++++--- pkg/config/config.go | 42 +++++++++++++++++++++++++++++++++++++++ pkg/config/config_test.go | 15 +++----------- pkg/outputter.go | 2 ++ 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/.mockery.yaml b/.mockery.yaml index af246cc3..cfff59dd 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -1,5 +1,4 @@ quiet: False -keeptree: True disable-version-string: True with-expecter: True mockname: "{{.InterfaceName}}" diff --git a/cmd/mockery.go b/cmd/mockery.go index c02b7336..f18f9d83 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,9 @@ 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 { + r.Config.LogUnsupportedPackagesConfig(ctx) + 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..70a114f0 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,43 @@ 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")) +} + +// LogUnsupportedPackagesConfig 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) LogUnsupportedPackagesConfig(ctx context.Context) { + log := zerolog.Ctx(ctx) + 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 + } + } + if len(unsupportedOptions) == 0 { + return + } + + l := log.With(). + Dict("unsupported-fields", zerolog.Dict().Fields(unsupportedOptions)). + Str("url", logging.DocsURL("/configuration/#parameter-descriptions")). + Logger() + l.Error().Msg("use of unsupported options detected. mockery behavior is undefined.") + +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index ac4f012a..7772e56a 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -700,18 +700,9 @@ func TestNewConfigFromViper(t *testing.T) { return viper.New() }, want: &Config{ - Dir: ".", - }, - }, - { - name: "dir set", - v: func(t *testing.T) *viper.Viper { - v := viper.New() - v.Set("dir", "foobar") - return v - }, - want: &Config{ - Dir: "foobar", + Case: "camel", + Dir: ".", + Output: "./mocks", }, }, { diff --git a/pkg/outputter.go b/pkg/outputter.go index 657e96d5..e74fbe41 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -320,6 +320,8 @@ func (m *Outputter) Generate(ctx context.Context, iface *Interface) error { } for _, interfaceConfig := range interfaceConfigs { + interfaceConfig.LogUnsupportedPackagesConfig(ctx) + log.Debug().Msg("getting mock generator") if err := parseConfigTemplates(ctx, interfaceConfig, iface); err != nil {