Skip to content

Commit

Permalink
Add error log for unsupported config
Browse files Browse the repository at this point in the history
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.

This fixes #685
  • Loading branch information
LandonTClipp committed Aug 4, 2023
1 parent 2046503 commit f368bd4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
14 changes: 11 additions & 3 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit f368bd4

Please sign in to comment.