Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--print flag does not redirect output to stdout #912

Open
semihbkgr opened this issue Feb 7, 2025 · 3 comments
Open

--print flag does not redirect output to stdout #912

semihbkgr opened this issue Feb 7, 2025 · 3 comments

Comments

@semihbkgr
Copy link

I think the --print flag is not working as expected. According to the documentation:

Use print: True to have the resulting code printed out instead of written to disk.

But when I use print, the generated code still gets written to a file instead of being printed to stdout. I tried:

  • Using --print as a cli flag
  • Setting print: true in the config file

In both cases, the output was still written to a file. I’m not sure if I’m missing something or if this is a bug.

Installation: Homebrew
Version: v2.50.0

@LandonTClipp
Copy link
Collaborator

I'll take a look (or someone else can, if they want).

I'm doubtful as to the utility of this anyway, because most .mockery.yml files will specify many output files. Printing all of those to stdout won't really do much for you. It's not like you can redirect that to your own file and get valid Go code.

The only way --print would output valid Go code is if you're generating only a single mock file. Maybe the solution here is to just remove it from the config options since it looks like it never worked in the packages: config scheme.

@semihbkgr
Copy link
Author

Hey @LandonTClipp

Thanks for taking a look! I totally get your point about the --print flag printing out lots of mock files when there are multiple outputs specified in the config. But in my case, I think the flag could still be useful for checking if mocks are up-to-date in CI pipelines.

Here’s what I had in mind:

test "$( mockery --print | sha256sum | awk '{print $1}' )" = "$( cat mocks/**/*.go | sha256sum | awk '{print $1}' )" || exit 1

This would help me check if the generated mocks match the existing ones by comparing the sha256 hashes, and exit with an error if they’re out of sync. It’s a nice way to automate mock validation without touching any files unnecessarily in an automated way.

I get that it might not be the most common use case, but it’d definitely be helpful for anyone needing to ensure their mocks are in sync with the interfaces. Just wanted to share why I think keeping the --print flag would be useful!

@LandonTClipp
Copy link
Collaborator

That definitely makes sense, although given how the configuration model is used to specify all interfaces to be mocked, I don't know how you would implement this. To do the hash comparison, you would need some way to tell mockery specifically which output file you want generated. Without doing that, --print would print every single file it's generating, which is not going to be useful to compare against individual files you already have on disk. Not an impossible task, but I would just want to hear a more specific proposal on how to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants