Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --inpackage-suffix option #453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
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 @@ -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,
Expand Down
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 @@ -31,6 +31,7 @@ type FileOutputStreamProvider struct {
Config config.Config
BaseDir string
InPackage bool
InPackageSuffix bool
TestOnly bool
Case string
KeepTree bool
Expand Down Expand Up @@ -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"
}

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