uber-go / mock

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

feat: add static validation of generated code for interface #67

Open bogdanlytvynovskyi opened 1 year ago

bogdanlytvynovskyi commented 1 year ago

Requested feature A clear description of the desired feature and an example of how it would be used.

For each generated interface mock include a line like: var _ Interface = (*MockInterface)(nil) in mock file

Why the feature is needed A clear description of how this feature is not served by existing functionality in gomock.

Often build step and generation step are separated. The workflow for developer should look like:

go generate ./... git add . git commit However when working with old interfaces, and updating those - more often than not people tend to forget to do the generation step. If there is no mock usage within this repository everything is going to be fine in terms of build/test for this repository, but consumers of this package in a different repository are going to break. Which requires going back and releasing new "fixed" version. The proposal is to enforce build failure in case generated files are not updated appropriately.

WDYT?

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

r-hang commented 1 year ago

CI is failing since there is another call to GenerateMockInterface that should be modified. The generated code should also be updated with these new assertions.

If a user updates an externally facing interface, I think external consumers could break with or without mock interface assertions. From my understanding of the issue, I see that this feature would be most helpful for erroring out only situations where there are mocks of interfaces for which the mocks aren't used anywhere in tests.

Do you see this feature as a means to remind developers when they are making interface breaking changes?

bogdanlytvynovskyi commented 1 year ago

@r-hang

Do you see this feature as a means to remind developers when they are making interface breaking changes?

Yes - this is exactly the point. Unfortunately I see a lot of code where people return interface instead of value.

Unfortunately there is a few bad practices coming together that leads to this situation especially in private repositories.