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

Prefix #128

Merged
merged 1 commit into from
Nov 19, 2024
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ or install minimock using [v2 branch](https://github.com/gojuno/minimock/tree/v2
-p string
comma-separated package names,
by default the generated package names are taken from the destination directory names
-pr string
mock file prefix
-s string
mock file suffix (default "_mock_test.go")
-gr
Expand Down
13 changes: 7 additions & 6 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type (
interfaces []interfaceInfo
noGenerate bool
suffix string
prefix string
mockNames []string
packageNames []string
goGenerateGoRun bool
Expand Down Expand Up @@ -173,15 +174,15 @@ func run(opts *options) (err error) {
if len(opts.interfaces) == len(opts.mockNames) {
mockName = opts.mockNames[i]
}
if err := processPackage(gopts, interfaces, in.WriteTo, opts.suffix, mockName); err != nil {
if err := processPackage(gopts, interfaces, in.WriteTo, opts.prefix, opts.suffix, mockName); err != nil {
return err
}
}

return nil
}

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

Expand All @@ -201,7 +202,7 @@ func processPackage(opts generator.Options, interfaces []types.InterfaceSpecific

paramsString := strings.Join(params, ",")

opts.OutputFile, err = destinationFile(iface.InterfaceName, writeTo, suffix)
opts.OutputFile, err = destinationFile(iface.InterfaceName, writeTo, prefix, suffix)
if err != nil {
return errors.Wrapf(err, "failed to generate mock for %s", iface.InterfaceName)
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func isGoFile(path string) (bool, error) {
return strings.HasSuffix(path, ".go") && !stat.IsDir(), nil
}

func destinationFile(interfaceName, writeTo, suffix string) (string, error) {
func destinationFile(interfaceName, writeTo, prefix, suffix string) (string, error) {
ok, err := isGoFile(writeTo)
if err != nil {
return "", err
Expand All @@ -279,7 +280,7 @@ func destinationFile(interfaceName, writeTo, suffix string) (string, error) {
if ok {
path = writeTo
} else {
path = filepath.Join(writeTo, minimock.CamelToSnake(interfaceName)+suffix)
path = filepath.Join(writeTo, prefix+minimock.CamelToSnake(interfaceName)+suffix)
}

if !strings.HasPrefix(path, "/") && !strings.HasPrefix(path, ".") {
Expand Down Expand Up @@ -387,7 +388,7 @@ func processArgs(args []string, stdout, stderr io.Writer) (*options, error) {
fs.BoolVar(&opts.goGenerateGoRun, "gr", false, `changes go:generate line from "//go:generate minimock args..." to "//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock",
useful while controlling minimock version with go mod`)
fs.StringVar(&opts.suffix, "s", "_mock_test.go", "mock file suffix")

fs.StringVar(&opts.prefix, "pr", "", "mock file prefix")
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")
Expand Down