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

feat: add pre-commit hook support #795

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ccoVeille
Copy link
Contributor

Description

Add pre-commit.com support in mockery.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Version of Go used when building/testing:

  • 1.11
  • 1.12
  • 1.13
  • 1.14
  • 1.15
  • 1.16
  • 1.17
  • 1.18
  • 1.19
  • 1.20
  • Not relevant

How Has This Been Tested?

Locally, by following the guide I provide in installation.md file

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Comment on lines 6 to 8
additional_dependencies:
- github.com/vektra/mockery/v2@latest
entry: mockery

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using additional_dependencies, the pre-commit-hook will already have access to the file system. We should just be able to use go run . to enact the mockery ... command I believe.

Also, we may only want this hook to run when .go files are in the commit. What do you think?

Suggested change
additional_dependencies:
- github.com/vektra/mockery/v2@latest
entry: mockery
entry: go run .
files: \.go$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure it will launch the code you expect to run.

I will make some tests.

But, I think keeping mockery call here would be clearer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using additional dependencies like this won't allow people to specify version in pre commit file because no matter what version they reference, you're pulling latest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I'm unsure if there is something that can be done here, I will have to check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now,it seems to me, that it would imply to maintain the version in sync in this file vs the tag. Quite a pain

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible let the users assume the responsibility for installing mockery? If that's the case this should suffice

---
- id: generate-mocks
  name: generate mocks
  description: Generate mocks with mockery
  language: golang
  entry: mockery
  pass_filenames: false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course, it makes sense.

But then having 2 rules could be a something to consider

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your approach is the most commonly used in fact

https://github.com/search?q=path%3A%22%2F.pre-commit-hooks.yaml%22+golang&type=code

.pre-commit-config.yaml Outdated Show resolved Hide resolved
docs/installation.md Outdated Show resolved Hide resolved
docs/installation.md Outdated Show resolved Hide resolved
docs/installation.md Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
@ccoVeille
Copy link
Contributor Author

anyway @liveFreeOrCode could you confirm it "works" as this ?

@ccoVeille ccoVeille marked this pull request as draft July 18, 2024 20:21
@ccoVeille
Copy link
Contributor Author

@liveFreeOrCode (or anyone) could you confirm ? thanks

Then I will remove the oddities present to make it works with my branch, and switch to vektra/mokery one

@ccoVeille
Copy link
Contributor Author

ccoVeille commented Jul 31, 2024

Hi @liveFreeOrCode

You commented the PR, thanks.

But you didn't reply my question. Does it work on your computer now with current code I provided.

I understand you might dislike the current implementation. I agree it could be improved. But right now,I would like to get a simple reply to the following question.

Does it work?

@liveFreeOrCode
Copy link

Hi @liveFreeOrCode

You commented the PR, thanks.

But you didn't reply my question. Does it work on your computer now with current code I provided.

I understand you might dislike the current implementation. I agree it could be improved. But right now,I would like to get a simple reply to the following question.

Does it work?

Yeah sorry I'm traveling without my PC. I can't test until next week

@ccoVeille
Copy link
Contributor Author

OK thanks for replying, we can also wait

@LandonTClipp
Copy link
Collaborator

Closing due to inactivity, feel free to resurrect it if you find the time to finish it.

@ccoVeille
Copy link
Contributor Author

Hi @LandonTClipp

Please reopen.

I was waiting for someone feedbacks.

The code is ready.

@sx-klaviyo
Copy link

I was just searching for pre-commit hooks for Mockery and found this 👀 Would love to see this reopened and merged!

@ccoVeille
Copy link
Contributor Author

Just ping me back if nothing happen, I can reopen the PR

@LandonTClipp LandonTClipp reopened this Dec 12, 2024
@sx-klaviyo
Copy link

Hi @ccoVeille! Just a reminder that this PR is opened now

@ccoVeille
Copy link
Contributor Author

@sx-klaviyo

The MR was waiting for people to test since July.

@liveFreeOrCode said he would, but he didn't.

There are indeed some code reviews that were provided, but before going further and address them, I would like someone to test.

The code was supposed to work. I would appreciate it if you could, before I iterate on addressing feedbacks.

Because without a tester, it's uneasy to validate things out of my understanding/knowlege

@sx-klaviyo
Copy link

I'll try to test it this week and let you know!

@sx-klaviyo
Copy link

sx-klaviyo commented Dec 20, 2024

@ccoVeille I confirm that your .pre-commit-hooks.yaml is working!
I also left a comment in a the additional_dependency and entry conversation: #795 (comment) please let me know if that looks good to you

@ccoVeille
Copy link
Contributor Author

Great. Thank you for testing.

For records, I'm unsure I will have time to fix that before Christmas break.

So be patient

@sx-klaviyo
Copy link

sx-klaviyo commented Jan 21, 2025

Hi @ccoVeille, just wondering if you are back from the Christmas break? Please let me know if I can help with anything!

@ccoVeille ccoVeille force-pushed the pre-commit branch 2 times, most recently from 943dcde to 3b14ef9 Compare January 21, 2025 22:51
@ccoVeille
Copy link
Contributor Author

Thanks for the reminder.

Winter break was indeed a great period.

Please let me know, if you think it's OK now.

@ccoVeille
Copy link
Contributor Author

ccoVeille commented Jan 21, 2025

@sx-klaviyo In order to test locally, please use the following set up

Add the following lines to your .pre-commit-config.yaml file

repos:
  - repo: https://github.com/ccoveille-forks/vektra-mockery
    rev: v2 # This is a mutable reference to the latest version
    hooks:
      - id: generate-mocks

Then test the other ones

repos:
  - repo: https://github.com/ccoveille-forks/vektra-mockery
    rev: v2 # This is a mutable reference to the latest version
    hooks:
      - id: generate-mocks-local

Both should work, I created a fake 2.51.1 tag

Please check and let me know.

@sx-klaviyo
Copy link

sx-klaviyo commented Jan 24, 2025

Thank you for the instructions!

I believe the repo link should be https://github.com/ccoveille-forks/vektra-mockery
Also, it only worked when I use this:

  - repo: https://github.com/ccoveille-forks/vektra-mockery
    rev: v2.51.1-alpha # by explicitly using the tag that you created
    hooks:
      - id: generate-mocks

Using v2 is not working for me. Also, i'm not sure if generate-mocks-local is a valid hook: https://github.com/ccoveille-forks/vektra-mockery/blob/0db88da09d747f44f7bdcd2b680e7c77fb4f506e/.pre-commit-hooks.yaml (this is the commit on master branch as I'm making this comment)

@ccoVeille
Copy link
Contributor Author

ccoVeille commented Jan 26, 2025

I had to test a lot to finally find why

I worked in a branch in my fork, but here is what the documentation of pre-commit says:

https://pre-commit.com/#updating-hooks-automatically

You can update your hooks to the latest version automatically by running pre-commit autoupdate. By default, this will bring the hooks to the latest tag on the default branch.

I had to switch the default branch of my fork to be the one of my PR ... so the tag I created would be the one precommit looks for.

Now everything works as expected. Please check by following steps:

repos:
  - repo: https://github.com/ccoveille-forks/vektra-mockery
    rev: v2
    hooks:
      - id: generate-mocks

Then

$ pre-commit install
$ pre-commit autoupgrade

Then try to commit a Go file

Then proceed with the exact same steps with

repos:
  - repo: https://github.com/ccoveille-forks/vektra-mockery
    rev: v2
    hooks:
      - id: generate-mocks-local

Then

$ pre-commit install
$ pre-commit autoupgrade

Then try to commit a Go file

And it should be OK (famous last words)

Please let me know @sx-klaviyo and even @liveFreeOrCode

So much time lost because of the "last tag from the default branch" joke. It's logic, But it's a pain to spot.

I might open a bug report/feature request on pre-commit code

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

Successfully merging this pull request may close these issues.

Feature request: pre-commit hooks
4 participants