Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ type ReplaceType struct {
TypeName string `koanf:"type-name" yaml:"type-name,omitempty"`
}

type FormatterOptions struct {
GoImports *struct {
LocalPrefix string `koanf:"local-prefix" yaml:"local-prefix,omitempty"`
} `koanf:"goimports" yaml:"goimports,omitempty"`
}

type Config struct {
All *bool `koanf:"all" yaml:"all,omitempty"`
Anchors map[string]any `koanf:"_anchors" yaml:"_anchors,omitempty"`
Expand All @@ -571,16 +577,17 @@ type Config struct {
ExcludeInterfaceRegex *string `koanf:"exclude-interface-regex" yaml:"exclude-interface-regex,omitempty"`
FileName *string `koanf:"filename" yaml:"filename,omitempty"`
// ForceFileWrite controls whether mockery will overwrite existing files when generating mocks. This is by default set to false.
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
Generate *bool `koanf:"generate" yaml:"generate,omitempty"`
IncludeAutoGenerated *bool `koanf:"include-auto-generated" yaml:"include-auto-generated,omitempty"`
IncludeInterfaceRegex *string `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
InPackage *bool `koanf:"inpackage" yaml:"inpackage,omitempty"`
LogLevel *string `koanf:"log-level" yaml:"log-level,omitempty"`
StructName *string `koanf:"structname" yaml:"structname,omitempty"`
PkgName *string `koanf:"pkgname" yaml:"pkgname,omitempty"`
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
FormatterOptions FormatterOptions `koanf:"formatter-options" yaml:"formatter-options,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be *FormatterOptions so that the yaml map can be nil.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we just end up with a zero value FormatterOptions in that case? I was thinking to do it this way to avoid the nil checks later, might be missing something though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is pretty complicated. It has to do with the migrate command when going from v2 to v3. If you set the default in koanf then mockery will never have nil values for it.

Generate *bool `koanf:"generate" yaml:"generate,omitempty"`
IncludeAutoGenerated *bool `koanf:"include-auto-generated" yaml:"include-auto-generated,omitempty"`
IncludeInterfaceRegex *string `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
InPackage *bool `koanf:"inpackage" yaml:"inpackage,omitempty"`
LogLevel *string `koanf:"log-level" yaml:"log-level,omitempty"`
StructName *string `koanf:"structname" yaml:"structname,omitempty"`
PkgName *string `koanf:"pkgname" yaml:"pkgname,omitempty"`
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
// ReplaceType is a nested map of format map["package path"]["type name"]*ReplaceType
ReplaceType map[string]map[string]*ReplaceType `koanf:"replace-type" yaml:"replace-type,omitempty"`
// RequireTemplateSchemaExists sets whether mockery will fail if the specified
Expand Down
20 changes: 20 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ packages:
assert.NoError(t, err)
}

func TestNewRootConfigFormatterOptions(t *testing.T) {
configFile := path.Join(t.TempDir(), "config.yaml")
require.NoError(t, os.WriteFile(configFile, []byte(`
formatter: goimports
formatter-options:
goimports:
local-prefix: github.com/vektra/mockery
`), 0o600))

flags := pflag.NewFlagSet("test", pflag.ExitOnError)
flags.String("config", "", "")

require.NoError(t, flags.Parse([]string{"--config", configFile}))
cfg, _, err := NewRootConfig(context.Background(), flags)
require.NoError(t, err)
require.NotNil(t, cfg.FormatterOptions.GoImports)
assert.Equal(t, "goimports", *cfg.Formatter)
assert.Equal(t, "github.com/vektra/mockery", cfg.FormatterOptions.GoImports.LocalPrefix)
}

func TestExtractConfigFromDirectiveComments(t *testing.T) {
configs := []struct {
name string
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Parameter Descriptions
| `filename` | :fontawesome-solid-check: | `#!yaml "mocks_test.go"` | The name of the file the mock will reside in. Multiple interfaces from the same source package can be placed into the same output file. |
| `force-file-write` | :fontawesome-solid-x: | `#!yaml true` | When set to `#!yaml force-file-write: true`, mockery will forcibly overwrite any existing files. Otherwise, it will fail if the output file already exists. |
| `formatter` | :fontawesome-solid-x: | `#!yaml "goimports"` | The formatter to use on the rendered template. Choices are: `gofmt`, `goimports`, `noop`. |
| `formatter-options` | :fontawesome-solid-x: | `#!yaml nil` | Additional options for the formatter. Currently supports `goimports.local-prefix` which will set the corresponding option when running goimports. |
| `generate` | :fontawesome-solid-x: | `#!yaml true` | Can be used to selectively enable/disable generation of specific interfaces. See [the related docs](generate-directive.md) for more details. |
| [`include-auto-generated`](include-auto-generated.md){ data-preview } | :fontawesome-solid-x: | `#!yaml false` | When set to `true`, mockery will parse files that are auto-generated. This can only be specified in the top-level config or package-level config. |
| `include-interface-regex` | :fontawesome-solid-x: | `#!yaml ""` | When set, only interface names that match the expression will be generated. This setting is ignored if `all: True` is specified in the configuration. To further refine the interfaces generated, use `exclude-interface-regex`. |
Expand Down
10 changes: 8 additions & 2 deletions internal/template_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ func NewTemplateGenerator(
func (g *TemplateGenerator) format(src []byte) ([]byte, error) {
switch g.formatter {
case FormatGoImports:
return goimports(src)
var localPrefix string
if c := g.pkgConfig.FormatterOptions.GoImports; c != nil {
localPrefix = c.LocalPrefix
}
return goimports(src, localPrefix)
case FormatGofmt:
return gofmt(src)
case FormatNoop:
Expand Down Expand Up @@ -485,7 +489,9 @@ func (g *TemplateGenerator) Generate(
return formatted, nil
}

func goimports(src []byte) ([]byte, error) {
func goimports(src []byte, localPrefix string) ([]byte, error) {
imports.LocalPrefix = localPrefix

formatted, err := imports.Process("/", src, &imports.Options{
TabWidth: 8,
TabIndent: true,
Expand Down