Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Support customizing generated mock package name #631

Closed
dr-dime opened this issue Mar 25, 2022 · 4 comments · May be fixed by #632
Closed

Support customizing generated mock package name #631

dr-dime opened this issue Mar 25, 2022 · 4 comments · May be fixed by #632
Labels
status: needs more info This issue need more information from the author. type: feature request

Comments

@dr-dime
Copy link

dr-dime commented Mar 25, 2022

Currently, mockgen allows specifying the generated mock package name with -package or a
generated package name with mock_ prefix will be used. However, when generating mock files
as package tests in scripts, this flag is very difficult to use.

In which case, the generated file shares the same package name as the package being mocked.
So we need to use tools (eg. grep) to extract the package name first, then pass it to mockgen.

This seems odd as mockgen already has the package name internally, and relies on external
tools for this are just a recipe for trouble.

I propose to introduce another flag to customize the generated mock package name, by default
it uses "mock_%s" which is the current behavior, and can be changed to other formats to allow
scripting usages.

@codyoss
Copy link
Member

codyoss commented Apr 24, 2022

Can you provide a small example. I don't think I quite understand the issue. Thanks

@codyoss codyoss added the status: needs more info This issue need more information from the author. label Apr 24, 2022
@codyoss
Copy link
Member

codyoss commented May 26, 2022

Closing due to lack of response.

@codyoss codyoss closed this as completed May 26, 2022
@dr-dime
Copy link
Author

dr-dime commented Jun 22, 2022

@codyoss Sorry for the late response. I must have all the emails from github archived.

Here is a simplified example: https://github.com/dr-dime/gomock-without-package.

p1 and p2 are two packages with interfaces to mock, and we want to generate the mock file for each source file in them and use the generated mock types in package tests, ie. {p1/p1,p2/p2}_test.go.

It's impossible for me to write a script to automate this process given the way the package name is generated, say mock_p{1,2}, instead of p{1,2}. The obvious solution is to grep the source file and extract the package name, but grep is not a valid parser, so it can easily get confused by package docs and other package-like statements.

@dr-dime
Copy link
Author

dr-dime commented Jul 4, 2022

@codyoss would you please reopen?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs more info This issue need more information from the author. type: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants