petergtz / pegomock

Pegomock is a powerful, yet simple mocking framework for the Go programming language
Apache License 2.0
254 stars 28 forks source link

Add support for vendor packages #50

Closed aakn closed 6 years ago

aakn commented 6 years ago

Takes the vendor package import path as an argument. Then remove the vendor package from the import path.

Example: If I have my dependencies in vendor, and have an external dependency (example sqlx). I would import that as sqlx "github.com/jmoiron/sqlx"

But the generated mock would have the following import: sqlx "github.com/aakn/test/vendor/github.com/jmoiron/sqlx"

This is handled by removing the vendor path from the import path.

Usage: Add an argument --vendor_path=github.com/org/project/vendor to the CLI

petergtz commented 6 years ago

Hi @aakn,

Thanks so much for your contribution. Proper handling of vendor directories is really missing in pegomock, and it's awesome to see that you've picked up this missing feature and sent a PR.

What do you think about making the vendor path configuration completely automatic? I.e. instead of passing it in as a CLI argument, let the code generator detect if something is a vendor path and remove it from the import path automatically. I think that detection would be possible, either by following Go's documented rules on how vendor directories work, or by using some library functions from the Go standard library.

Then a user never has to think about it again.

Thanks, Peter

aakn commented 6 years ago

I had earlier planned to do it automatically. But the only way I could think of that was to check if the import path contains 'vendor', and remove the vendor path prefix. I didn't opt for this approach because I am not sure if other developers need it. But yeah, if they have vendor packages in their working dir, then they do need it.

Does that sound correct? I'm not sure if I've missed any cases.

petergtz commented 6 years ago

Does that sound correct? I'm not sure if I've missed any cases.

Yes, I think that does sound correct. I suggest to start with that.

I didn't opt for this approach because I am not sure if other developers need it.

I think vendor is never allowed to be part of an import path. So it's probably safe to assume developers never need it in there.

Should we notice that this approach doesn't work in all cases, we can still tune it. Does that sound good to you?

aakn commented 6 years ago

Yup, this sounds good. I'll update the pr.

petergtz commented 6 years ago

Looks great! Thank you, @aakn.

petergtz commented 6 years ago

@aakn Do you happen to have a Twitter handle? I want to post about the update on Twitter and would like to mention you.

lkysow commented 6 years ago

Nice work @aakn :D Now I can remove these go:generate sed lines!

//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_runner.go Runner
//go:generate sed -i "" -e "s#go_version \"github.com/hootsuite/atlantis/vendor/github.com/hashicorp/go-version\"#go_version \"github.com/hashicorp/go-version\"#" mocks/mock_runner.go