petergtz / pegomock

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

Users need to specify too many different options to generate mocks in different directories #69

Closed MissingRoberto closed 5 years ago

MissingRoberto commented 5 years ago

Hi Peter,

Trying to generate a mock in a fakes folder (e.g. $(PWD)/fakes/mock_<INTERFACE>_test.go), I had several several options that for me look redundant.

//go:generate pegomock generate --package fakes Namespacer -o fakes/mock_namespacer_test.go

I would expect the UX to be something like: pegomock generate --package fakes Namespacer or pegomock generate --output fakes/ Namespacer

Maybe I didn't understand completely how the options in pegomock generate work.

petergtz commented 5 years ago

Hi Roberto, the reason why it is separate is that folder name must not necessarily match package name. Your generate command could also be:

//go:generate pegomock generate --package namespacer_fakes Namespacer -o fakes/mock_namespacer_test.go

I guess, since the output folder cannot be properly "guessed", the --output option cannot be omitted. But what we could do is that if there is no --package option, it could check if there is an --output option and use the folder name as package name.

What do you think?

MissingRoberto commented 5 years ago

That would leave the command for that as follows:

pegomock generate --output fakes/mock_namespacer_test.go Namespacer

It's still lengthy, you still need to say the output (including the name of the file).

Different scenarios

No option

$ pegomock generate Namespacer
---> mock_namespacer_test.go

The UX is good for no option, because it's already generates a mock ready to be consumed in your current directory:

Only package or only output folder

$ pegomock generate --package fakes Namespacer
-->  fakes/mock_namespacer_test.go
$  pegomock generate -o fakes Namespacer
-->  fakes/mock_namespacer_test.go (with fakes as package name)

Package + output folder

$  pegomock generate --package fakes -o otherpackage Namespacer
-->  otherpackage/mock_namespacer_test.go (with fakes as package name)

Package + output file

$ pegomock generate --package fakes -o otherpackage/other_test.go Namespacer
-->  otherpackage/other_test.go (with fakes as package name)

output file

$  pegomock generate -o fakes/file_test.go Namespacer
-->  fakes/file_test.go (with fakes as package name)
petergtz commented 5 years ago

Are those "different scenarios" your desired Ginkgo spec? :-)

MissingRoberto commented 5 years ago

Yes, different use cases, so that the number of parameter is reduced to the minimum and consequently the UX becomes better.

(It has nothing to do with Ginkgo though)

On Fri, 14 Sep 2018 at 13:06 Peter Götz notifications@github.com wrote:

Are those "different scenarios" your desired Ginkgo spec? :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/petergtz/pegomock/issues/69#issuecomment-421324242, or mute the thread https://github.com/notifications/unsubscribe-auth/ABmOjdiPF37aI6kpH8sLDAnEZRNyIZ76ks5ua42ygaJpZM4WYy6y .

petergtz commented 5 years ago

@jszroberto I was just joking because of perfect spec description :-).

Anyway, it looks good and I'll see if I can implement that in the next 2 weeks. If you're impatient, feel free to submit a PR. I'll be happy to merge it.

petergtz commented 5 years ago

@jszroberto Can you check out the output-dir branch and see if this is what you want?

Note that I introduced a --output-dir argument instead of what you suggested in order not to introduce a breaking change.

Your original scenario would look like this now:

pegomock generate --output-dir fakes Namespacer
MissingRoberto commented 5 years ago

lgtm!

I think you missed 1 test case:


$ pegomock generate --output-dir otherfolder -o fakes/file_test.go Namespacer 

That should probably fail if -o has a path

petergtz commented 5 years ago

@jszroberto I'm not allowing --output-dir and -o: https://github.com/petergtz/pegomock/commit/da3507330f0cd2fcd9c1c774a630281baa32ea26#diff-0c8edc90b9e9f48cc840abff1c7b93ecR82. I'm not sure there is a useful use case for that scenario.