Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use shorthand generate directive for external packages? #199

Closed
dionysius opened this issue Feb 23, 2022 · 1 comment
Closed

How to use shorthand generate directive for external packages? #199

dionysius opened this issue Feb 23, 2022 · 1 comment

Comments

@dionysius
Copy link
Contributor

I generate fakes from external interfaces so I can write tests against it and define a behaviour of said interface during tests.

After upgrading counterfeiter I get this warning, which is indeed true that it takes long:

WARNING: Invoking counterfeiter multiple times from "go generate" is slow.
Consider using counterfeiter:generate directives to speed things up.
See https://github.com/maxbrunsfeld/counterfeiter#step-2b---add-counterfeitergenerate-directives for more information.

So I tried to change the generation to the suggested way as written in the Readme. But the Readme also shows only an example for own interfaces where you write it directly in the source files.

How should the generate directive look for external interfaces?

 package lxf
 
 // lxdfakes
-//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o lxdfakes/fake_operation.go github.com/lxc/lxd/client.Operation
-//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o lxdfakes/fake_remote_operation.go github.com/lxc/lxd/client.RemoteOperation
-//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o lxdfakes/fake_server.go github.com/lxc/lxd/client.Server
-//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o lxdfakes/fake_image_server.go github.com/lxc/lxd/client.ImageServer
-//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o lxdfakes/fake_container_server.go github.com/lxc/lxd/client.ContainerServer
+//counterfeiter:generate -o lxdfakes/fake_operation.go github.com/lxc/lxd/client.Operation
+//counterfeiter:generate -o lxdfakes/fake_remote_operation.go github.com/lxc/lxd/client.RemoteOperation
+//counterfeiter:generate -o lxdfakes/fake_server.go github.com/lxc/lxd/client.Server
+//counterfeiter:generate -o lxdfakes/fake_image_server.go github.com/lxc/lxd/client.ImageServer
+//counterfeiter:generate -o lxdfakes/fake_container_server.go github.com/lxc/lxd/client.ContainerServer

Right now with above changes, I don't see any generation happening anymore. I've seen that there's also #195, but this doesn't explain that the generation is not doing anything.

@dionysius
Copy link
Contributor Author

dionysius commented Feb 23, 2022

Oh wait, i just found the comment:

// You only need one of these per package!
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

// You will add lots of directives like these in the same package...
//counterfeiter:generate . MySpecialInterface

Yes, that did the trick! RTFM <3

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

No branches or pull requests

1 participant