Skip to content

add a mockery template that generates counterfeiter fakes - #399

Closed
LandonTClipp wants to merge 1 commit into
maxbrunsfeld:mainfrom
LandonTClipp:mockery-template
Closed

LandonTClipp wants to merge 1 commit into
maxbrunsfeld:mainfrom
LandonTClipp:mockery-template

Conversation

@LandonTClipp

@LandonTClipp LandonTClipp commented Nov 13, 2026 •

Copy link
Copy Markdown

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 in mockery/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:

  • Inside the fake's methods, parameters and results use mockery's names: the interface's own names, or names derived from the type when there are none. For example, DoThings(s string, v uint64) rather than DoThings(arg1 string, arg2 uint64). The <Method>Args structs keep their Arg1…ArgN fields.
  • mockery writes the import statements, so it only adds an alias when one is needed. counterfeiter also names packages whose name differs from their directory, such as hyphenpackage "…/go-hyphenpackage". When two imports share a name, mockery's aliases also differ from counterfeiter's (sync0 rather than synca).

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:

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-file template-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 with sync, 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 if mockery isn't on PATH.

Known gaps:

  • mockery only fakes interfaces, so function types and package mode (-p) still need counterfeiter.
  • The README's example URL pins vX.Y.Z. It will work once a release includes the template.

@LandonTClipp
LandonTClipp force-pushed the mockery-template branch 2 times, most recently from f854f65 to dd27545 Compare November 13, 2026 00:16
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>
@joefitzgerald

Copy link
Copy Markdown
Collaborator

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.

@LandonTClipp

LandonTClipp commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

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 //go:generate-style of generation. The community has attested to anywhere from 10x-30x performance improvements:

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.

@joefitzgerald

Copy link
Copy Markdown
Collaborator

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 counterfeiter'-compatible template for mockery`, you should maintain it in-tree in the mockery repo and maintain it over time there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants