Skip to content

Commit

Permalink
Merge pull request #686 from LandonTClipp/issue_685
Browse files Browse the repository at this point in the history
Add error log for unsupported config
  • Loading branch information
LandonTClipp committed Aug 4, 2023
2 parents 2046503 + 72eb146 commit 8abb702
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
1 change: 0 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
quiet: False
keeptree: True
disable-version-string: True
with-expecter: True
mockname: "{{.InterfaceName}}"
Expand Down
8 changes: 5 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,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)
Expand Down
42 changes: 42 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,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.")

}
15 changes: 3 additions & 12 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8abb702

Please sign in to comment.