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

feat: add argument to speecify custom mock names #57

Merged
merged 1 commit into from
Jun 21, 2021
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
30 changes: 28 additions & 2 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
interfaces []interfaceInfo
noGenerate bool
suffix string
mockNames []string
}

interfaceInfo struct {
Expand Down Expand Up @@ -108,25 +109,36 @@ func run(opts *options) (err error) {
"GenerateInstruction": !opts.noGenerate,
"Version": version,
},
Vars: map[string]interface{}{},
Funcs: helpers,
}

if err := processPackage(gopts, interfaces, in.WriteTo, opts.suffix); err != nil {
mockName := ""
if len(opts.interfaces) == len(opts.mockNames) {
mockName = opts.mockNames[i]
}
if err := processPackage(gopts, interfaces, in.WriteTo, opts.suffix, mockName); err != nil {
return err
}
}

return nil
}

func processPackage(opts generator.Options, interfaces []string, writeTo, suffix string) (err error) {
func processPackage(opts generator.Options, interfaces []string, writeTo, suffix, mockName string) (err error) {
for _, name := range interfaces {
opts.InterfaceName = name

opts.OutputFile, err = destinationFile(name, writeTo, suffix)
if err != nil {
return errors.Wrapf(err, "failed to generate mock for %s", name)
}

opts.Vars["MockName"] = fmt.Sprintf("%sMock", opts.InterfaceName)
if mockName != "" {
opts.Vars["MockName"] = mockName
}

if err := generate(opts); err != nil {
return err
}
Expand Down Expand Up @@ -286,6 +298,7 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) {

input := fs.String("i", "*", "comma-separated names of the interfaces to mock, i.e fmt.Stringer,io.Reader\nuse io.* notation to generate mocks for all interfaces in the \"io\" package")
output := fs.String("o", "", "comma-separated destination file names or packages to put the generated mocks in,\nby default the generated mock is placed in the source package directory")
aliases := fs.String("n", "", "comma-separated mock names,\nby default the generated mock names append `Mock` to the given interface name")
help := fs.Bool("h", false, "show this help message")
version := fs.Bool("version", false, "display version information and exit")

Expand All @@ -307,6 +320,19 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) {

interfaces := strings.Split(*input, ",")

var mockNames []string
if *aliases != "" {
mockNames = strings.Split(*aliases, ",")
}
if len(mockNames) != 0 && len(mockNames) != len(interfaces) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please make sure that -i option doesn't contain any wildcards when -n is given? This will generate mock with the same name for every interface in the package. Let's say we have -i io.* -n IOMock then we'll have same IOMock name for both io.Reader and io.Writer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hexdigest done :)

return nil, errors.Errorf("count of the source interfaces doesn't match the mock names count")
}
if len(mockNames) != 0 && strings.Contains(*input, "*") {
return nil, errors.Errorf("wildcards * can't be used with -n argument")
}

opts.mockNames = mockNames

var writeTo = make([]string, len(interfaces))
if *output != "" {
//if only one output package specified
Expand Down
4 changes: 2 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
// Code generated by http://github.com/gojuno/minimock ({{$.Options.HeaderVars.Version}}). DO NOT EDIT.

{{if $.Options.HeaderVars.GenerateInstruction}}
//go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.OutputFile}}
//go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.OutputFile}} -n {{(title (index $.Vars "MockName"))}}
{{end}}

import (
Expand All @@ -23,7 +23,7 @@ const (

// BodyTemplate is used to generate mock body
BodyTemplate = `
{{ $mock := (title (printf "%sMock" $.Interface.Name)) }}
{{ $mock := (title (index $.Vars "MockName")) }}

// {{$mock}} implements {{$.Interface.Type}}
type {{$mock}} struct {
Expand Down
2 changes: 1 addition & 1 deletion tests/formatter_mock.go

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

283 changes: 283 additions & 0 deletions tests/formatter_with_custom_name_mock.go

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

Loading