Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

support compile time checks for regen #698

Open
bogdanlytvynovskyi opened this issue Mar 23, 2023 · 2 comments
Open

support compile time checks for regen #698

bogdanlytvynovskyi opened this issue Mar 23, 2023 · 2 comments

Comments

@bogdanlytvynovskyi
Copy link

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?

@bogdanlytvynovskyi
Copy link
Author

Proposed patch below:

diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go
index 79cb921..8edd3ec 100644
--- a/mockgen/mockgen.go
+++ b/mockgen/mockgen.go
@@ -362,7 +362,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
 	g.p(")")
 
 	for _, intf := range pkg.Interfaces {
-		if err := g.GenerateMockInterface(intf, outputPackagePath); err != nil {
+		if err := g.GenerateMockInterface(pkg.Name, intf, outputPackagePath); err != nil {
 			return err
 		}
 	}
@@ -404,7 +404,7 @@ func (g *generator) formattedTypeParams(it *model.Interface, pkgOverride string)
 	return long.String(), short.String()
 }
 
-func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePath string) error {
+func (g *generator) GenerateMockInterface(pkgName string, intf *model.Interface, outputPackagePath string) error {
 	mockType := g.mockName(intf.Name)
 	longTp, shortTp := g.formattedTypeParams(intf, outputPackagePath)
 
@@ -417,6 +417,7 @@ func (g *generator) GenerateMockInterface(intf *model.Interface, outputPackagePa
 	g.out()
 	g.p("}")
 	g.p("")
+	g.p("var _ %v.%v = (*%v)(nil)", pkgName, intf.Name, mockType)
 
 	g.p("// %vMockRecorder is the mock recorder for %v.", mockType, mockType)
 	g.p("type %vMockRecorder%v struct {", mockType, longTp)

@manfredlift
Copy link

@bogdanlytvynovskyi have you considered opening a PR for this? This is something I am also interested in! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants