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

Add flag to generate go:generate instruction in the generate file #45

Closed
damianopetrungaro opened this issue Jul 23, 2023 · 2 comments · Fixed by #46
Closed

Add flag to generate go:generate instruction in the generate file #45

damianopetrungaro opened this issue Jul 23, 2023 · 2 comments · Fixed by #46

Comments

@damianopetrungaro
Copy link
Contributor

Requested feature: Add support for a "go:generate" instruction as part of the generated mock files in gomock.

Why the feature is needed: Currently, when using gomock to generate mock files, there is no built-in functionality to include a "go:generate" instruction within the generated mock files.

This instruction is essential for integrating the generated mocks seamlessly into the development workflow; there are already other popular go libraries that do that (for example: hexdigest/gowrap).

Without this feature, users are required to manually add the "go:generate" instruction to each generated mock file, which is time-consuming and prone to human errors, and overall hard to maintain.

(Optional) Proposed solution:
https://github.com/damianopetrungaro/mock/tree/feat_add_go_generate_instruction

@bradleygore
Copy link

Hey @damianopetrungaro this is interesting. Wouldn't the human have to first generate the mock in order to get this go:generate directive in the first place? My process is to add the directive atop my interfaces that need mocked and use go generate in the terminal instead of using mockgen directly. This way I know the cmd will work the same way for the rest of my team. Example:

//go:generate mockgen -destination=./mocks/Doer.go -package=mocks path/to/pkg Doer
type Doer interface {
    Do() error
}

Would that sort of approach be helpful to you and alleviate the human errors aspect? Or is there an additional benefit to the proposed approach that I'm missing? So far, this approach is working nicely for my team, but I'm always up for improving!

@damianopetrungaro
Copy link
Contributor Author

@bradleygore I guess there's more than one way to skin a cat.

What I find useful is more the other way around, mockgen command from the CLI first and then paste it where needed (sometimes is not even close to the interface itself but more in the package that holds the generated code).

sywhang added a commit that referenced this issue Aug 11, 2023
As you can read by the PR title, I added a flag (false by default) that
allows mockgen to add the go:generate instruction as part of the
generated file.

This is something I was looking to see supported, and I finally had some
time to add this tiny feature.
It would save a lot of time, and it is similar to what other tools
already ([gowrap](https://github.com/hexdigest/gowrap) for example).

Resolves: #45

---------

Co-authored-by: Moises Vega <[email protected]>
Co-authored-by: Sung Yoon Whang <[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

Successfully merging a pull request may close this issue.

2 participants