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

Support overriding destination package name and type naming template in "-generate" call #191

Open
matt-royal opened this issue Sep 30, 2021 · 5 comments

Comments

@matt-royal
Copy link

I've found that I prefer to use a package called "fake" for my fakes, and that I prefer to remove "Fake" from the type name. This looks like the following currently:

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate -o fake -fake-name MyRepository . MyRepository
//counterfeiter:generate -o fake -fake-name MyPresenter . MyPresenter

This allows me to refer to the fake like this:

myRepository := new(fake.MyRepository)

Unfortunately, I have to repeat those -o and -fake-name arguments in every generate call. It would be nice to be able to do something like the following:

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate -o fake -fake-name-template "{{.TargetName}}"
//counterfeiter:generate . MyRepository
//counterfeiter:generate . MyPresenter

I'm happy to work on a PR to add this behavior, but I want to make sure you'd be open to the idea first. I'd also love to hear other ideas to accomplish the same goal of removing repetition and risk of inconsistency.

Thanks!

@matt-royal
Copy link
Author

@maxbrunsfeld, @joefitzgerald, and @tjarratt Any thoughts on this? It seems like y'all are the maintainers these days

@maxbrunsfeld
Copy link
Owner

Hey Matt! This makes a lot of sense to me, from my understanding.

I would merge such a PR, although @joefitzgerald is now the primary author and maintainer of this project - it’s been basically rewritten (and much improved) since I last touched it.

@joefitzgerald
Copy link
Collaborator

This sounds like a great idea! +1 to merging a PR like this. Thanks so much for your help @matt-royal 🙏 .

@matt-royal
Copy link
Author

Thank you, @maxbrunsfeld and @joefitzgerald for all your work writing and maintaining counterfeiter. I can't imagine testing in go without it.

I'm not sure how soon I'll have the PR ready but I'll send it your way when it's done.

matt-royal added a commit to matt-royal/counterfeiter that referenced this issue Jan 22, 2022
When the -generate flag is given with a -o flag, that flag will be the
default value for each invocation. If the invocation specifies it's own
-o, then that value will be used. Similar logic applies for the -header
flag (which was true before this commit) and for the -q flag.

Also, there is now a -fake-name-template flag when -generate is
provided. When an invocation does not specify a -fake-name, the value in
-fake-name-template will be evaluated as a text/template string and then
run against an object with a `TargetName` field. The result will be the
-fake-name for the invocation.

For example, the following comments will result in a fake named
`TheMaskedSinger` in the `fakes` package:

    //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate -o fakes -fake-name-template "TheMasked{{.TargetName}}"

    //counterfeiter:generate . Singer

[Issue maxbrunsfeld#191]

Authored-by: Matt Royal <[email protected]>
matt-royal added a commit to matt-royal/counterfeiter that referenced this issue Jan 22, 2022
When the -generate flag is given with a -o flag, that flag will be the
default value for each invocation. If the invocation specifies it's own
-o, then that value will be used. Similar logic applies for the -header
flag (which was true before this commit) and for the -q flag.

Also, there is now a -fake-name-template flag when -generate is
provided. When an invocation does not specify a -fake-name, the value in
-fake-name-template will be evaluated as a text/template string and then
run against an object with a `TargetName` field. The result will be the
-fake-name for the invocation.

For example, the following comments will result in a fake named
`TheMaskedSinger` in the `fakes` package:

    //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate -o fakes -fake-name-template "TheMasked{{.TargetName}}"

    //counterfeiter:generate . Singer

[Issue maxbrunsfeld#191]

Authored-by: Matt Royal <[email protected]>
@matt-royal
Copy link
Author

I took a crack at this in #195. Please let me know what you think

joefitzgerald pushed a commit to matt-royal/counterfeiter that referenced this issue Jan 24, 2022
When the -generate flag is given with a -o flag, that flag will be the
default value for each invocation. If the invocation specifies it's own
-o, then that value will be used. Similar logic applies for the -header
flag (which was true before this commit) and for the -q flag.

Also, there is now a -fake-name-template flag when -generate is
provided. When an invocation does not specify a -fake-name, the value in
-fake-name-template will be evaluated as a text/template string and then
run against an object with a `TargetName` field. The result will be the
-fake-name for the invocation.

For example, the following comments will result in a fake named
`TheMaskedSinger` in the `fakes` package:

    //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate -o fakes -fake-name-template "TheMasked{{.TargetName}}"

    //counterfeiter:generate . Singer

[Issue maxbrunsfeld#191]

Authored-by: Matt Royal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants