-
Notifications
You must be signed in to change notification settings - Fork 449
Add support for overriding configuration through comments on interfaces #1105
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
Merged
+860
−24
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ## Description | ||
|
|
||
| An alternative way to configure mocks is to use the `#!go //mockery:generate` directive. Mockery parses the doc comments and allows you to override configuration of specific interfaces in the source code. For example: | ||
|
|
||
| ```yaml title=".mockery.yml" | ||
| all: false | ||
| packages: | ||
| github.com/vektra/mockery/v3/internal/fixtures/directive_comments_example: | ||
| ``` | ||
|
|
||
| We set the top-level `#!yaml all: false` (which is the default value, anyway) to ensure that interfaces are by default not generated. We can then specify the doc comment directive to include the interface: | ||
|
|
||
| ```go title="interface.go" | ||
| package directivecommentsexample | ||
|
|
||
| // Requester is an interface that defines a method for making HTTP requests. | ||
| // | ||
| //mockery:generate: true | ||
| type Requester interface { | ||
| Get(path string) (string, error) | ||
| } | ||
| ``` | ||
|
|
||
| <div class="result" markdown> | ||
| ``` title="" | ||
| 2025-11-11T14:40:22.897665000-05:00 INF adding interface to collection collection=/Users/landonclipp/git/LandonTClipp/mockery/internal/fixtures/directive_comments_example/mocks_test.go interface=Requester package-path=github.com/vektra/mockery/v3/internal/fixtures/directive_comments_example version=v0.0.0-dev | ||
| ``` | ||
| </div> | ||
|
|
||
| We can also specify any config value that mockery supports. For example, let's rename the mock's structname: | ||
|
|
||
| ``` | ||
| // Requester is an interface that defines a method for making HTTP requests. | ||
| // | ||
| //mockery:generate: true | ||
| //mockery:structname: MockFoo | ||
| type Requester interface { | ||
| Get(path string) (string, error) | ||
| } | ||
| ``` | ||
|
|
||
| The new `structname` is applied as expected: | ||
|
|
||
| ```go | ||
| // MockFoo is an autogenerated mock type for the Requester type | ||
| type MockFoo struct { | ||
| mock.Mock | ||
| } | ||
| ``` | ||
|
|
||
| !!! note | ||
|
|
||
| The `#!yaml generate:` parameter is only effectual from within the doc comment itself. It has no effect if specified within the mockery config file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see that this is an easy and convenient way to translate into mockery config, but it might be nice to have some alternative syntax, rather than spreading yaml through multiple directive lines, especially if you could take advantage of the provided
ast.ParseDirectivefunction?Something like this might be nicer, but I appreciate it may be a bit more work...
//mockery:generate -structname MyMock -filename my_mock.goThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I err on the side of being obvious versus being clever. Emulating command line args with mockery actually having any command line args is confusing and weird. I'm okay with the way this is currently implemented.