Skip to content

Commit

Permalink
Merge pull request #57 from damianopetrungaro/master
Browse files Browse the repository at this point in the history
feat: add argument to speecify custom mock names
  • Loading branch information
hexdigest committed Jun 21, 2021
2 parents 37f8ffa + 912133c commit 7e0b6d3
Show file tree
Hide file tree
Showing 6 changed files with 640 additions and 6 deletions.
30 changes: 28 additions & 2 deletions cmd/minimock/minimock.go
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) {
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
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.

0 comments on commit 7e0b6d3

Please sign in to comment.