Skip to content

Commit bc0ef83

Browse files
committed
feat: goimports local prefix
1 parent 6e6205d commit bc0ef83

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

config/config.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ type ReplaceType struct {
561561
TypeName string `koanf:"type-name" yaml:"type-name,omitempty"`
562562
}
563563

564+
type FormatterOptions struct {
565+
GoImports *struct {
566+
LocalPrefix string `koanf:"local-prefix" yaml:"local-prefix,omitempty"`
567+
} `koanf:"goimports" yaml:"goimports,omitempty"`
568+
}
569+
564570
type Config struct {
565571
All *bool `koanf:"all" yaml:"all,omitempty"`
566572
Anchors map[string]any `koanf:"_anchors" yaml:"_anchors,omitempty"`
@@ -571,16 +577,17 @@ type Config struct {
571577
ExcludeInterfaceRegex *string `koanf:"exclude-interface-regex" yaml:"exclude-interface-regex,omitempty"`
572578
FileName *string `koanf:"filename" yaml:"filename,omitempty"`
573579
// ForceFileWrite controls whether mockery will overwrite existing files when generating mocks. This is by default set to false.
574-
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
575-
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
576-
Generate *bool `koanf:"generate" yaml:"generate,omitempty"`
577-
IncludeAutoGenerated *bool `koanf:"include-auto-generated" yaml:"include-auto-generated,omitempty"`
578-
IncludeInterfaceRegex *string `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
579-
InPackage *bool `koanf:"inpackage" yaml:"inpackage,omitempty"`
580-
LogLevel *string `koanf:"log-level" yaml:"log-level,omitempty"`
581-
StructName *string `koanf:"structname" yaml:"structname,omitempty"`
582-
PkgName *string `koanf:"pkgname" yaml:"pkgname,omitempty"`
583-
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
580+
ForceFileWrite *bool `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
581+
Formatter *string `koanf:"formatter" yaml:"formatter,omitempty"`
582+
FormatterOptions FormatterOptions `koanf:"formatter-options" yaml:"formatter-options,omitempty"`
583+
Generate *bool `koanf:"generate" yaml:"generate,omitempty"`
584+
IncludeAutoGenerated *bool `koanf:"include-auto-generated" yaml:"include-auto-generated,omitempty"`
585+
IncludeInterfaceRegex *string `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
586+
InPackage *bool `koanf:"inpackage" yaml:"inpackage,omitempty"`
587+
LogLevel *string `koanf:"log-level" yaml:"log-level,omitempty"`
588+
StructName *string `koanf:"structname" yaml:"structname,omitempty"`
589+
PkgName *string `koanf:"pkgname" yaml:"pkgname,omitempty"`
590+
Recursive *bool `koanf:"recursive" yaml:"recursive,omitempty"`
584591
// ReplaceType is a nested map of format map["package path"]["type name"]*ReplaceType
585592
ReplaceType map[string]map[string]*ReplaceType `koanf:"replace-type" yaml:"replace-type,omitempty"`
586593
// RequireTemplateSchemaExists sets whether mockery will fail if the specified

config/config_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ packages:
7373
assert.NoError(t, err)
7474
}
7575

76+
func TestNewRootConfigFormatterOptions(t *testing.T) {
77+
configFile := path.Join(t.TempDir(), "config.yaml")
78+
require.NoError(t, os.WriteFile(configFile, []byte(`
79+
formatter: goimports
80+
formatter-options:
81+
goimports:
82+
local-prefix: github.com/vektra/mockery
83+
`), 0o600))
84+
85+
flags := pflag.NewFlagSet("test", pflag.ExitOnError)
86+
flags.String("config", "", "")
87+
88+
require.NoError(t, flags.Parse([]string{"--config", configFile}))
89+
cfg, _, err := NewRootConfig(context.Background(), flags)
90+
require.NoError(t, err)
91+
require.NotNil(t, cfg.FormatterOptions.GoImports)
92+
assert.Equal(t, "goimports", *cfg.Formatter)
93+
assert.Equal(t, "github.com/vektra/mockery", cfg.FormatterOptions.GoImports.LocalPrefix)
94+
}
95+
7696
func TestExtractConfigFromDirectiveComments(t *testing.T) {
7797
configs := []struct {
7898
name string

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Parameter Descriptions
125125
| `filename` | :fontawesome-solid-check: | `#!yaml "mocks_test.go"` | The name of the file the mock will reside in. |
126126
| `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. |
127127
| `formatter` | :fontawesome-solid-x: | `#!yaml "goimports"` | The formatter to use on the rendered template. Choices are: `gofmt`, `goimports`, `noop`. |
128+
| `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. |
128129
| `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. |
129130
| [`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. |
130131
| `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`. |

internal/template_generator.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ func NewTemplateGenerator(
179179
func (g *TemplateGenerator) format(src []byte) ([]byte, error) {
180180
switch g.formatter {
181181
case FormatGoImports:
182-
return goimports(src)
182+
var localPrefix string
183+
if c := g.pkgConfig.FormatterOptions.GoImports; c != nil {
184+
localPrefix = c.LocalPrefix
185+
}
186+
return goimports(src, localPrefix)
183187
case FormatGofmt:
184188
return gofmt(src)
185189
case FormatNoop:
@@ -485,7 +489,9 @@ func (g *TemplateGenerator) Generate(
485489
return formatted, nil
486490
}
487491

488-
func goimports(src []byte) ([]byte, error) {
492+
func goimports(src []byte, localPrefix string) ([]byte, error) {
493+
imports.LocalPrefix = localPrefix
494+
489495
formatted, err := imports.Process("/", src, &imports.Options{
490496
TabWidth: 8,
491497
TabIndent: true,

0 commit comments

Comments
 (0)