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

Use go generate directives for mock generation #3159

Merged
merged 2 commits into from
Apr 24, 2024
Merged

Conversation

dmjb
Copy link
Contributor

@dmjb dmjb commented Apr 24, 2024

Instead of adding commands to the Makefile, use go generate directives in the files which contain the interfaces we want to mock. This approach has the following advantages:

  1. Less context switching: you can declare the need for a mock in the
    code and not have to edit the makefiles.
  2. The same directive can be reused everywhere - you no longer need to
    specify the filepath, interface name and the path to the generated
    mock.
  3. If the interface is renamed or moved, the code generation will still
    work without modification.

I have left some commands in the makefile for generating mocks from code in pkg, and from autogenerated code. We could in theory get rid of these usages as well, but I did not feel there was much point at this time since they are edge cases.

You may note that there are a lot of additional lines of code: this is because I'm using the "source" mode of mockgen instead of the "reflect" mode. The source mode will generate mocks for all interfaces in the file, and not just the specified ones. However, it does so much quicker than the reflect mode - make gen now takes about half the time it did previously.

Summary

Provide a brief overview of the changes and the issue being addressed.
Explain the rationale and any background necessary for understanding the changes.
List dependencies required by this change, if any.

Fixes #(related issue)

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@dmjb dmjb requested a review from a team as a code owner April 24, 2024 10:45
@dmjb dmjb marked this pull request as draft April 24, 2024 10:47
@dmjb dmjb force-pushed the go-mock-annotations branch 2 times, most recently from 3003e37 to d77af86 Compare April 24, 2024 11:22
@dmjb dmjb marked this pull request as ready for review April 24, 2024 11:23
Instead of adding commands to the Makefile, use go generate directives
on top of the interfaces we need mocks for. This approach has the following
advantages:

1) Less context switching: you can declare the need for a mock in the
   code and not have to edit the makefiles.
2) The same directive can be reused everywhere - you no longer need to
   specify the filepath, interface name and the path to the generated
   mock.
3) If the interface is renamed or moved, the code generation will still
   work without modification.

I have left some commands in the makefile for generating mocks from code
in `pkg`, and from autogenerated code. We could in theory get rid of
these usages as well, but I did not feel there was much point at this
time since they are edge cases.
@JAORMX
Copy link
Contributor

JAORMX commented Apr 24, 2024

With this PR:

________________________________________________________
Executed in    9.18 secs    fish           external
   usr time   26.27 secs  395.00 micros   26.27 secs
   sys time    9.51 secs  168.00 micros    9.51 secs

Without this PR:

________________________________________________________
Executed in   35.45 secs    fish           external
   usr time  137.61 secs    0.00 micros  137.61 secs
   sys time   23.82 secs  476.00 micros   23.82 secs

@dmjb dmjb merged commit dfe0916 into main Apr 24, 2024
20 checks passed
@dmjb dmjb deleted the go-mock-annotations branch April 24, 2024 11:49
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.

3 participants