Closed chidiwilliams closed 4 years ago
Hi @chidiwilliams,
Thanks for reporting this. This is in indeed a problem in Pegomock. The reason why it is working when you specify the filepath instead, is that Pegomock uses a completely different mechanism to discover the interface methods when you specify a file instead of a package.
Here's what you can do, if you want to make it work with packages too. Instead of
pegomock generate [package-path] Foo
use
pegomock generate --use-experimental-model-gen [package-path] Foo
If you're interested in the internals, here's what's happening:
Without the --use-experimental-model-gen
, pegomock will generate a program that imports your package and then use Go's introspection features to learn about its interfaces. It will then print out the interfaces+methods to StdOut, where it is picked up by Pegomock and used to generate the mock code. Now with your Println
in the Init
function, that messes up the output on StdOut.
With the --use-experimental-model-gen
, pegomock will use a proper Go code parser to understand the interfaces in your code. It never runs any code and therefore also no Init methods.
The plan is to eventually make --use-experimental-model-gen
the default.
I'd be curious to hear if that flag works for you.
Thank you, Peter
Thanks, @petergtz! It works perfectly as you said.
x.go
Running
pegomock generate [package-path] Foo
generates the following error:However, the mock is generated if I:
pegomock generate x.go
Println
call (e.g. I usePrint
,Printf
, etc.)