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

overrides.FileName = strings.TrimSpace(matches[1])
} else if matches := templateRe.FindStringSubmatch(text); len(matches) > 1 {
overrides.Template = strings.TrimSpace(matches[1])
}
Copy link
Member

@LandonTClipp LandonTClipp Aug 19, 2025

Choose a reason for hiding this comment

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

This is really good, but I think we can do even better. Obviously the problem with this way of doing it is that we must have manual support for each individual config parameter. Instead, I think we could design this in a way that it will support all config parameters as defined in config.Config.

The way you'd want to go about this is to parse the yaml in the doc comment into this config struct, then merge this into the interface's Config struct. You already kind of do this below in your Override method, but it's cleaner to use the merge method already available.

Additionally, it'd be better if the override discovery logic lives here in GetInterfaceConfig instead of needing a separate call. I'd eventually like to bring the showconfig command into parity with what you've written here so that overrides are properly represented. Obviously you'd need to pass the *ast.GenDecl syntax to make that work, which is fine.

Overall great work, thank you for tackling this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry It took me so long for getting back to this! Yeah, I totally agree that this was a little bit too manual. I've refactor it a little bit to extract a yaml from the comment section and parse it into a config - merging it with the file based pkg/interface config if they exist.

Regarding the last piece:

Additionally, it'd be better if the override discovery logic lives here in GetInterfaceConfig instead of needing a separate call.

If we were to do that, we wouldn't have loaded the config from the comments before the ShouldGenerateInterface check, this means that we would need to have the interface defined in the config file (unless we move this check to happen later).

Comment on lines 10 to 13
generateRe = regexp.MustCompile(`mockery_generate: (true|false)`)
structNameRe = regexp.MustCompile(`mockery_struct_name: ([^\s]+)$`)
fileNameRe = regexp.MustCompile(`mockery_file_name: ([^\s]+)$`)
templateRe = regexp.MustCompile(`mockery_template: ([^\s]+)$`)

Choose a reason for hiding this comment

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

The go doc language spec talks about directives and states that

Tools that define their own directives should use the form //toolname:directive.

It seems like the proposed directives should conform to this syntax too, so comments would be:

//mockery:<parameter name> <value>

If that makes sense?

Copy link
Member

Choose a reason for hiding this comment

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

That makes a lot of sense. I'm not sure if this PR is going to be updated, we might need someone else to take it over.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey 👋 (sorry for being missing)!

I like that! I took a chance to refactor the initial pr with @LandonTClipp initial comment and this suggestion! Let me know what do you think.

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.


// Requester is an interface that defines a method for making HTTP requests.
//
//mockery:generate: true
Copy link
Member

Choose a reason for hiding this comment

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

My reading of the code is that the generate: true config is not actually doing anything other than serving as a trigger that says "hey, there is a mockery: directive here". We could have put anything here, like //mockery:foo: bar and the same thing would happen. Is that accurate? If so, the generate: config should have a real meaning, as in, the presence of this config (and not others) triggers the mock to be generated. Furthermore, I think this should really just be //mockery:generate to conform with the same standard as //go:generate.

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