vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
5.93k stars 401 forks source link

Feature request: pre-commit hooks #791

Open liveFreeOrCode opened 1 month ago

liveFreeOrCode commented 1 month ago

PLEASE READ

DO NOT submit tickets without first using the latest version of Golang, clearing your local golang package cache, and re-building mockery using the latest Golang version and the latest version of mockery. Please provide evidence this has been done in your issue. Failure to provide this evidence will likely result in your issue being closed.

Description

It would be nice to have supported pre-commit hooks capable of generating mocks as part of the normal CD process. Currently I accomplish this as a local hook, but that local hook uses my local golang installation, and if I have a mockery version installed already, the "additional_dependencies" won't be honored.

repos:
  - repo: local
    hooks:
      - id: generate-mocks
        name: generate mocks
        language: golang
        additional_dependencies:
          - github.com/vektra/mockery/v2@v2.43.2
        entry: mockery
        pass_filenames: false

Mockery Version

latest

Golang Version

go version go1.22.4 linux/amd64

NOTE: Please upgrade to the latest golang version before submitting tickets!
LandonTClipp commented 1 month ago

Sorry I don't understand the question. Are you asking about git pre-commit hooks?

ccoVeille commented 1 month ago

I recognize the pattern of https://pre-commit.com

So, it's about adding a YAML file to the repository.

I don't think there will be a need to maintain the file on version upgrade. Maybe using @latest would be enough.

I can help if you want. I can open a PR.

I have already set up such a thing.

The only thing I'm worried about is the fact, a pre-commit hooks might generate the mock, but I'm unsure how it could stop the commit.

But maybe, I will find it out by testing, or the author of this issue would give us some explanation.

Said, otherwise I can help in coding, the need is clear to me as I know pre-commit tool. But I'm unsure what author wants to achieve exactly.

Adding the generated mocked file to the commit, prevent the commit when the mocked files are not up to date.

Please note for maintainers of the repository, it won't have much consequence than adding a file to the repository, maybe add a section about it in the documentation.

Only a few people might use the feature, but it's a convenient way for people to use mockery

ccoVeille commented 1 month ago

Please note, the work is almost complete with the YAML file provided by @liveFreeOrCode

So maybe, he is just asking for a GO to code it by himself.

LandonTClipp commented 1 month ago

Feel free to open a PR. I have never used this tool so I will need education on how this practically speaking will be used by mockery users. I understand the basic concept based on the docs I read of this tool, but perhaps a PR would help solidify the idea for me, in addition to a description of how pre-commit would be configured in a separate repo to pull the YAML config that we will define here.

ccoVeille commented 1 month ago

I will unless @liveFreeOrCode post a message.

@LandonTClipp May I ask you to assign me this issue for now?

LandonTClipp commented 1 month ago

Assigned, thank you!

liveFreeOrCode commented 1 month ago

👋 Hi @ccoVeille. Thank you for taking this on. I usually drop in a feature request first in issues before I start any work to make sure I work out any gotchas with the maintainers, and to see if it's in line with the product's vision. That said, I probably wasn't going to be able to get to this anytime soon, so I do really appreciate you being able to jump on this.

The only thing I'm worried about is the fact, a pre-commit hooks might generate the mock, but I'm unsure how it could stop the commit.

pre-commit will block a commit if any of the hooks modify a file. So once mockery updates or adds a new mock, the pre-commit output will notify the user that this commit hook modified files, and it will stop the commit so the developer can review the changes that were made by mockery.

ccoVeille commented 1 month ago

I remembered it was something like that indeed. But I wasn't sure, and was awaiting a feedback from you to know how you were using the local hook you quoted