Repository navigation
add a mockery template that generates counterfeiter fakes - #399
LandonTClipp wants to merge 1 commit into
Conversation
f854f65 to
dd27545
Compare
Projects that already use mockery can now generate counterfeiter fakes with it instead of running counterfeiter. mockery/counterfeiter.templ is a mockery v3 template that emits fakes with the same API, and with the recommended configuration in mockery/README.md it lays them out the way counterfeiter does. Generic interfaces, fakes in the interface's own package and headers (via boilerplate-file) are supported. The generated code is not identical to counterfeiter's: the template uses mockery's parameter and result names inside the fake's methods (DoThings(s string, v uint64) rather than DoThings(arg1 string, arg2 uint64)), and mockery writes the import statements. Neither affects the API, and the <Method>Args structs keep their Arg1...ArgN fields. The integration tests run mockery over the fixtures, compare each fake's API with the committed counterfeiter fake, and exercise the fakes in a small test. CI installs mockery for them. Signed-off-by: LandonTClipp <lclipp@coreweave.com>
dd27545 to
15538c9
Compare
|
Why would we manage a mockery template here in the counterfeiter repo? If you want this template, can you put it in the mockery repo? I don't want to sign up to maintain a template for an alternate generator that I'm not familiar with. |
|
Very good question. My broader proposal is that counterfeiter could retain ownership of its fake API and behavior while delegating package loading, type handling, and import management to mockery. Those are shared problems across interface generators; fixing them in a common implementation means each project doesn’t have to independently maintain the same infrastructure as Go evolves. Mockery has also demonstrated considerable performance improvements over
The benefit would therefore be more than generation speed: counterfeiter maintainers could concentrate on what makes counterfeiter useful, with mockery handling more of the machinery underneath. The compatibility tests in this PR are intended to make that possibility concrete (although function types and package mode remain gaps, but this is easily solvable). There is already one example of a third-party out-of-tree template that mockery can work with: https://vektra.github.io/mockery/latest/template/third-party/ We also maintain two templates in-tree:
I am not necessarily asking you to justify maintaining a separate template today, but more to demonstrate that moving towards this model can actually reduce your workload over time if you so choose to go this route. Of course you can do with this whatever you may. |
|
Counterfeiter is mostly feature complete. I'm going to close this, not because I'm not grateful for the contribution; rather, I think that if you want a |
This adds a mockery template,
mockery/counterfeiter.templ, that generates counterfeiter fakes. The fakes have the same API (<Method>Stub,CallCount,Calls,ArgsForCall,Args,Returns,ReturnsOnCall,Invocations,CallOrder). With the configuration inmockery/README.md, mockery lays them out the way counterfeiter does (<pkg>fakes/fake_<name>.go,Fake<Name>), so tests written against counterfeiter fakes keep working.The generated code isn't identical to counterfeiter's, though the API is the same:
DoThings(s string, v uint64)rather thanDoThings(arg1 string, arg2 uint64). The<Method>Argsstructs keep theirArg1…ArgNfields.hyphenpackage "…/go-hyphenpackage". When two imports share a name, mockery's aliases also differ from counterfeiter's (sync0rather thansynca).This keeps the template close to mockery's own templates: it uses mockery's method helpers and the method scope for naming instead of reimplementing them.
Why this might be useful: counterfeiter currently owns its whole pipeline, including package loading, type walking, import aliasing, generics and the output template. Most of the bug reports and maintenance in this repo are about the parts before the template. mockery v3 was built to share that work. It loads packages and resolves types, imports, generics and aliases once, and lets a template decide only what the generated code looks like. Other mock styles already work this way:
matryertemplate that generates moq-style mocks. It came out of a proposal to the moq project (PROPOSAL: Integration with vektra/mockery matryer/moq#208, RFC: integration of `github.com/matryer/moq`-style mocks vektra/mockery#715).faketemplate. Its rationale makes the same argument: mockery owns the hard part, and the template owns only the shape of the generated code.For users, nothing changes unless they opt in. Projects that already run mockery can generate counterfeiter fakes without a second generator. For counterfeiter, this is a possible path toward not maintaining its own code generation framework, at least for interfaces. That's a decision for you, and this PR doesn't remove or change anything in the existing generator.
What's covered: the integration tests run mockery over copies of the fixtures. For each fake, they compare the API with the committed counterfeiter fake, meaning exported fields and method signatures checked with go/types, ignoring parameter names. That covers aliases, dot imports, embedded and unexported interfaces, variadics, hyphenated packages, generic interfaces, and fakes in the interface's own package. Further cases check that headers set through a
boilerplate-filetemplate-data option (the equivalent of-header) come out as counterfeiter writes them, and that the methods use mockery's parameter names. A stdlib-only test exercises the fakes (returns, stubs, recorded and copied arguments, variadics, invocations). Other cases check a package that collides withsync, packages named like the fake's local variables, and several interfaces sharing one file. CI installs mockery v3.7.0 for these tests; locally they skip ifmockeryisn't onPATH.Known gaps:
-p) still need counterfeiter.vX.Y.Z. It will work once a release includes the template.