Skip to content

Commit

Permalink
Merge pull request #69 from zcolleen/master
Browse files Browse the repository at this point in the history
 feat: added option -t to support generating package name with suffix "_test"
  • Loading branch information
hexdigest authored Jan 7, 2024
2 parents 4b6adb4 + 24f5226 commit dba0ff5
Show file tree
Hide file tree
Showing 16 changed files with 1,265 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ _testmain.go
/dist
/bin
/.gh_token

.idea
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ generate:
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go
go run ./cmd/minimock/minimock.go -i ./tests.contextAccepter -o ./tests/context_accepter_mock.go
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -p tests_test

./bin:
mkdir ./bin
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ or install minimock using [v2 branch](https://github.com/gojuno/minimock/tree/v2
-o string
comma-separated destination file names or packages to put the generated mocks in,
by default the generated mock is placed in the source package directory
-p string
comma-separated package names,
by default the generated package names are taken from the destination directory names
-s string
mock file suffix (default "_mock_test.go")
```
Expand Down
53 changes: 39 additions & 14 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ var helpers = template.FuncMap{

type (
options struct {
interfaces []interfaceInfo
noGenerate bool
suffix string
mockNames []string
interfaces []interfaceInfo
noGenerate bool
suffix string
mockNames []string
packageNames []string
}

interfaceInfo struct {
Expand Down Expand Up @@ -98,6 +99,11 @@ func run(opts *options) (err error) {

interfaces := types.FindAllInterfaces(astPackage, in.Type)

packageName := ""
if len(opts.interfaces) == len(opts.packageNames) {
packageName = opts.packageNames[i]
}

gopts := generator.Options{
SourcePackage: sourcePackage.PkgPath,
SourcePackageAlias: "mm_" + sourcePackage.Name,
Expand All @@ -106,6 +112,7 @@ func run(opts *options) (err error) {
HeaderVars: map[string]interface{}{
"GenerateInstruction": !opts.noGenerate,
"Version": version,
"PackageName": packageName,
},
Vars: map[string]interface{}{},
Funcs: helpers,
Expand Down Expand Up @@ -302,6 +309,22 @@ Build date: {{bold .BuildDate}}
}
}

func processNames(names string, interfacesNum int, isInterfaceWildeCarded bool) ([]string, error) {
if names == "" {
return nil, nil
}

namesSplitted := strings.Split(names, ",")
if len(namesSplitted) != 0 && len(namesSplitted) != interfacesNum {
return nil, errors.Errorf("count of the source interfaces doesn't match the names count")
}
if len(namesSplitted) != 0 && isInterfaceWildeCarded {
return nil, errors.Errorf("wildcards * can't be used with naming argument")
}

return namesSplitted, nil
}

var errInvalidArguments = errors.New("invalid arguments")

func processArgs(args []string, stdout, stderr io.Writer) (*options, error) {
Expand All @@ -315,6 +338,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")
packageNames := fs.String("p", "", "comma-separated package names,\nby default the generated package names are taken from the destination directory names")
help := fs.Bool("h", false, "show this help message")
version := fs.Bool("version", false, "display version information and exit")

Expand All @@ -335,20 +359,21 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) {
}

interfaces := strings.Split(*input, ",")
interfacesLen := len(interfaces)
isWildecarded := strings.Contains(*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")
mockNames, err := processNames(*aliases, interfacesLen, isWildecarded)
if err != nil {
return nil, fmt.Errorf("processing -n flag arguments: %w", err)
}

opts.mockNames = mockNames

parsedPackages, err := processNames(*packageNames, interfacesLen, isWildecarded)
if err != nil {
return nil, fmt.Errorf("processing -p flag arguments: %w", err)
}
opts.packageNames = parsedPackages

var writeTo = make([]string, len(interfaces))
if *output != "" {
//if only one output package specified
Expand Down
8 changes: 6 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package minimock
const (
// HeaderTemplate is used to generate package clause and go:generate instruction
HeaderTemplate = `
package {{$.Package.Name}}
{{ $packageName := $.Package.Name }}
{{if $.Options.HeaderVars.PackageName }}
{{ $packageName = $.Options.HeaderVars.PackageName }}
{{end}}
package {{$packageName}}
// 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.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}}
//go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }}
{{end}}
import (
Expand Down
2 changes: 1 addition & 1 deletion tests/context_accepter_mock.go

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

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.

2 changes: 1 addition & 1 deletion tests/generic_complex_union.go

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

2 changes: 1 addition & 1 deletion tests/generic_in.go

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

2 changes: 1 addition & 1 deletion tests/generic_inline_union.go

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

2 changes: 1 addition & 1 deletion tests/generic_inout.go

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

2 changes: 1 addition & 1 deletion tests/generic_out.go

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

2 changes: 1 addition & 1 deletion tests/generic_simple_union.go

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

2 changes: 1 addition & 1 deletion tests/generic_specific.go

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

Loading

0 comments on commit dba0ff5

Please sign in to comment.