golang / mock

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

genmock doesn't import self_package when interface use structure that have same package with interface #11

Closed soulski closed 8 years ago

soulski commented 8 years ago

source code

type A struct {}

type I interface {
    Foo() A
}

Mock that was generate from genmock will have error because when genmock create mock file mock package is mock* so mock won't find struct A in mock* package

petrkotek commented 8 years ago

+1 (having the same issue)

dsymonds commented 8 years ago

How are you running genmock? If I add that example to sample/user.go and run update_mocks.sh, the generated code imports the sample package just fine.

petrkotek commented 8 years ago

Thanks @dsymonds for taking a look. I call mockgen via go generate like this:

...
//go:generate mockgen -source mappers.go -destination _mock_mapper/mock_mapper.go
...

and then e.g. go generate ./...

i can imagine that it's a bit tricky (or not possible) to reliably generate the import statement for mocked package when using the filename (like mockgen -source mappers. go -destination ...) instead of fully qualified package name (like ./update_mocks.sh does).

generating a mock this way works fine:

mockgen github.com/<organisation>/<repo>/mappers Mapper > _mock_mapper/mock_mapper.go

I'm not sure if that's a bug or expected behavior, but could be eventually handy to mention this as a caveat of using mockgen -source ... -destination ... syntax.

dsymonds commented 8 years ago

Source mode is pretty hard to get right, and gomock can't tell what A you necessary mean. That's why I generally recommend people use the reflect mode, since it avoids all these pitfalls.

petrkotek commented 8 years ago

Makes sense. I've updated my //go:generate to the form below & works as a charm. I'm pretty sure it will resolve @soulski's issue too.

//go:generate sh -c "mockgen github.com/<org>/<repo>/mappers Mapper > _mock_mapper/mock_mapper.go"

Thanks a lot, really appreciated.

dsymonds commented 8 years ago

You shouldn't have to use sh -c, but that's a separate issue.

petrkotek commented 8 years ago

Reason for using sh -c is that I need to redirect output of mockgen <pkg> <interfaces> to a file (otherwise > and output_file.go are interpreted as another string arguments for mockgen.

dsymonds commented 8 years ago

Why not use the -destination flag?

petrkotek commented 8 years ago

Because mockgen <pkg> <interfaces> -destination _mock_mapper/mock_mapper.go results in Expected exactly two arguments error.

dsymonds commented 8 years ago

Flags have to go before non-flag args.

petrkotek commented 8 years ago

:facepalm: of course, sorry & thanks

jmhodges commented 7 years ago

Could this be reopened? I'm trying to use the bazel build system and generating a mock from outside of a generated go protobuf library, and the bazel/rules_go stuff doesn't provide a working $GOPATH for me to throw mockgen at. That is, the reflect style won't work, and only -source would, but it seems to be broken.

Specifically, mockgen with -source and a -package that is different from the original package, generates code that references types as if they are in the original package and setting -self_package and -imports to anything just seems to cause a . to go in front of the type names but not the actual package.