uber-go / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
1.85k stars 106 forks source link

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

Closed damianopetrungaro closed 11 months ago

damianopetrungaro commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

@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).