diff --git a/cmd/mockery.go b/cmd/mockery.go index 76e0d77fb..ae502ac11 100644 --- a/cmd/mockery.go +++ b/cmd/mockery.go @@ -57,6 +57,7 @@ func NewRootCmd() *cobra.Command { pFlags.BoolP("recursive", "r", false, "recurse search into sub-directories") pFlags.Bool("all", false, "generates mocks for all found interfaces in all sub-directories") 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("note", "", "comment to insert into prologue of each generated file") @@ -233,6 +234,7 @@ func (r *RootApp) Run() error { Config: r.Config, BaseDir: r.Config.Output, InPackage: r.Config.InPackage, + InPackageSuffix: r.Config.InPackageSuffix, TestOnly: r.Config.TestOnly, Case: r.Config.Case, KeepTree: r.Config.KeepTree, diff --git a/pkg/config/config.go b/pkg/config/config.go index b0ca2115e..c4ef46711 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -32,6 +32,7 @@ type Config struct { Exported bool `mapstructure:"exported"` FileName string InPackage bool + InPackageSuffix bool `mapstructure:"inpackage-suffix"` KeepTree bool LogLevel string `mapstructure:"log-level"` Name string diff --git a/pkg/outputter.go b/pkg/outputter.go index d6e635c61..bb0aac5e1 100644 --- a/pkg/outputter.go +++ b/pkg/outputter.go @@ -31,6 +31,7 @@ type FileOutputStreamProvider struct { Config config.Config BaseDir string InPackage bool + InPackageSuffix bool TestOnly bool Case string KeepTree bool @@ -91,13 +92,24 @@ func (p *FileOutputStreamProvider) GetWriter(ctx context.Context, iface *Interfa func (p *FileOutputStreamProvider) filename(name string) string { if p.FileName != "" { return p.FileName - } else if p.InPackage && p.TestOnly { + } + + if p.InPackage && p.TestOnly { + if p.InPackageSuffix { + return name + "_mock_test.go" + } + return "mock_" + name + "_test.go" } else if p.InPackage && !p.KeepTree { + if p.InPackageSuffix { + return name + "_mock.go" + } + return "mock_" + name + ".go" } else if p.TestOnly { return name + "_test.go" } + return name + ".go" } diff --git a/pkg/outputter_test.go b/pkg/outputter_test.go index 3bc7a9072..67fce0f7f 100644 --- a/pkg/outputter_test.go +++ b/pkg/outputter_test.go @@ -16,11 +16,21 @@ func TestFilenameMockOnly(t *testing.T) { assert.Equal(t, "mock_name.go", out.filename("name")) } +func TestFilenameMockOnlyWithSuffix(t *testing.T) { + out := FileOutputStreamProvider{InPackage: true, InPackageSuffix: true, TestOnly: false} + assert.Equal(t, "name_mock.go", out.filename("name")) +} + func TestFilenameMockTest(t *testing.T) { out := FileOutputStreamProvider{InPackage: true, TestOnly: true} assert.Equal(t, "mock_name_test.go", out.filename("name")) } +func TestFilenameMockTestWithSuffix(t *testing.T) { + out := FileOutputStreamProvider{InPackage: true, InPackageSuffix: true, TestOnly: true} + assert.Equal(t, "name_mock_test.go", out.filename("name")) +} + func TestFilenameKeepTreeInPackage(t *testing.T) { out := FileOutputStreamProvider{KeepTree: true, InPackage: true} assert.Equal(t, "name.go", out.filename("name"))