Skip to content

Conversation

@paivagustavo
Copy link
Contributor

@paivagustavo paivagustavo commented Aug 16, 2025

Description

Adds support for configuring mocks through directive comments on the interface. The comment will be parsed as a yaml configuration, any configuration supported on the file based config is also supported by this:

//mockery:struct_name: <struct_name>
//mockery:file_name: <file_name>
//mockery:template: <template>
//mockery:<key>: <value>

These configuration will take precedene of over any other configuration.

I haven't made any changes to documentation yet as I wanted to wait for a confirmation that this pr will be accepted before doing so.

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.24

How Has This Been Tested?

Added a new package with these annotations in the fixture folder, testing all the new configurations.

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

Copy link
Member

@LandonTClipp LandonTClipp left a comment

Choose a reason for hiding this comment

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

This is much, much better! I need to sit with this and ponder the implications of this PR a bit more. I might submit more comments in the coming days. Overall, this is probably close to being done.

Comment on lines +750 to +766
var yamlConfig []string

// Extract all mockery directive comments and build a YAML document
for _, doc := range decl.Doc.List {
// Look for directive comments `//mockery:<config-key>: <value>` and convert them to YAML
if value, found := strings.CutPrefix(doc.Text, "//mockery:"); found && value != "" {
yamlConfig = append(yamlConfig, value)
}
}

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.ParseDirective function?

Something like this might be nicer, but I appreciate it may be a bit more work...

//mockery:generate -structname MyMock -filename my_mock.go

Copy link
Member

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.

@LandonTClipp
Copy link
Member

@paivagustavo I have a strange request, can you give me write permission on your fork? I have some tweaks I want to add to your branch regarding the ShouldGenerateInterface logic. I'm excited about this PR but I just wanted to collaborate a little more directly.

@paivagustavo
Copy link
Contributor Author

@paivagustavo I have a strange request, can you give me write permission on your fork? I have some tweaks I want to add to your branch regarding the ShouldGenerateInterface logic. I'm excited about this PR but I just wanted to collaborate a little more directly.

Of course @LandonTClipp, I've added you! feel free to do any change :)

@paivagustavo
Copy link
Contributor Author

@LandonTClipp your changes looks good to me! I've added one last commit to remove a println that I've added during some tests :). Thank you for having patience with me!

@LandonTClipp
Copy link
Member

No worries, I think we're ready to merge this now once the tests pass. I bumped the minor version so it will get released automatically. Thanks again, this is great!

@LandonTClipp LandonTClipp merged commit f5daeff into vektra:v3 Nov 12, 2025
6 checks passed
@jamesphillpotts-fr
Copy link

Nice change, thanks @paivagustavo @LandonTClipp! 🎉

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.

Support mock generation via interface Annotations

3 participants