uber-go / mock

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

Command line flag to toggle whether to emit source file/interface names comment in generated code #32

Closed utgwkk closed 1 year ago

utgwkk commented 1 year ago

Requested feature I want to toggle whether to emit a comment of mock's origin (a .go file for source mode, a package and interface names for reflect mode) in a generated code like below (from example code):

// Source: go.uber.org/mock/sample (interfaces: Index,Embed,Embedded)

Why the feature is needed I made a wrapper tool of mockgen (named bulkmockgen), which manages interfaces to be mocked with a variable in a .go file and invokes mockgen with symbols arguments from the variable.

For example, running go generate with the below code will execute a command mockgen -package mock_foo -destination ./mock_foo/mock.go . IFoo,IBar.

//go:generate bulkmockgen Iset -- -package mock_foo -destination ./mock_foo/mock.go
var Iset = []any{
    new(IFoo),
    new(IBar),
}

This works fine, but there is a problem that the generated code's comment causes many Git conflicts because there are original interface names in one line in a top of generated code and the line is edited every time I add an interface name to mock target list.

(Optional) Proposed solution Adding a command line flag like -write_source_comment and controlling whether to emit mock's origin comment by the flag will solve the problem described above. I implemented it in my repository fork.

sywhang commented 1 year ago

Thanks, this is a reasonable request, given how we already have write_package_comment.

If you want to make a pull request for this, we'll review it soon.