Skip to content

Commit

Permalink
Add --inpackage-suffix option
Browse files Browse the repository at this point in the history
This allows users to choose whether they prefer mock_name.go or
name_mock.go for their filenames.
  • Loading branch information
grongor committed Apr 19, 2022
1 parent aad3571 commit c98782f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

homedir "github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -213,6 +214,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,
Expand Down
2 changes: 1 addition & 1 deletion mocks/pkg/fixtures/RequesterVariadicOneArgument.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion pkg/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type FileOutputStreamProvider struct {
Config config.Config
BaseDir string
InPackage bool
InPackageSuffix bool
TestOnly bool
Case string
KeepTree bool
Expand Down Expand Up @@ -86,13 +87,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"
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/outputter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit c98782f

Please sign in to comment.