golang / mock

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

Allow mock generation for interfaces that are used in the import path in reflect mode #200

Closed seeruk closed 4 years ago

seeruk commented 6 years ago

I generate my mocks using a script, and generally try to keep the mocks next to the tests that use them. So far this has helped me avoid cyclic dependencies, and helped me keep mock code in test packages that aren't compiled during my normal application build (e.g. in a file called export_test.go). It also helps in my IDE (Goland in this case) because it means that when I am looking at the implementations of an interface, my mocks are highlighted in green, making it very obvious they're for testing, rather than having multiple "normal code" implementations, and having to filter through to get to the right one in the annoyingly small popup that doesn't show the entire path easily...

With all of that background; it'd be great if I could run something like this:

#!/usr/bin/env bash

ROOT_PACKAGE="bitbucket.org/icelolly/click-tracking-api"

mockgen -package handlers \
    -destination=./handlers/export_test.go \
    "${ROOT_PACKAGE}/handlers" InterfaceDeclaredInHandlersPkg,another.InterfaceDefinedElsewhere

In that case, another would be an imported package. It would either be aliased to another, or it's package name would already be another. mockgen would then be able to generate the interface for that.

Right now I'll have to throw a bunch of separate files in. It's not the end of the world, and I keep most of the benefits I mentioned earlier this way, but it's not quite as tidy in the filesystem.

codyoss commented 4 years ago

I don't think we will be moving forward with this at this time. The complexity vs the what needs to be done otherwise is not worth the trade-off imo. Also, //go:generate directives directly in your code might make this seem a little more maintainable.