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

feat: Error message for nonexistent mocks #907

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

ravsii
Copy link
Contributor

@ravsii ravsii commented Jan 31, 2025

Description

Added a log message that warns user that he specified an interface that doesn't exist.

WRN no such interface dry-run=false interface=InterfaceDoesntExist qualified-name=github.com/vektra/mockery/v2/pkg/ version=v0.0.0-dev

I did add an extra Has function for parser because main loop shouldn't have that kind of logic, given that parser already parsed the interfaces and has all the metainfo for them.

I also decided to not add any flags because it seems like something that shouldn't be turned off as a separate feature, but it's still possible to set log-level = error and remove this for those who want it.

Please let me know if I missed something! I will also do a PR for v3 after this one will be accepted.

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.22
  • 1.23

How Has This Been Tested?

There's only one log message that was added, I'm know sure how/if we should test it. I can add tests for parser.Has, if needed, but the function itself is pretty simple.

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
Collaborator

@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 good. I agree with not providing an option to turn the warning off. Good work!

Comment on lines +227 to +244
func (p *Parser) Has(packageName, interfaceName string) bool {
for _, entry := range p.files {
if entry.pkg.PkgPath != packageName {
continue
}

for _, ifaceName := range entry.interfaces {
if ifaceName != interfaceName {
continue
}

return true
}
}

return false
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good, obviously this is a polynomial runtime because of the linear iteration through all interfaces during each query, but it's probably fine? Simple is good 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know this is far from optimal, but since the parser stores entries as a slice, I don't think creating an additional map for that specific (and only) case is needed. (But could be done, feel free to ping me).

I hope nobody has tens of thousands of interfaces in a single project :D

@LandonTClipp LandonTClipp merged commit 14a9cc6 into vektra:master Jan 31, 2025
4 checks passed
LandonTClipp added a commit to LandonTClipp/mockery that referenced this pull request Feb 1, 2025
PR vektra#907 used a feature of zerolog that did not exist in the version
mockery depended on. It's still a mystery how CICD allowed this to happen,
but I will release this fix before that investigation is complete.
LandonTClipp added a commit that referenced this pull request Feb 1, 2025
PR #907 used a feature of zerolog that did not exist in the version
mockery depended on. It's still a mystery how CICD allowed this to happen,
but I will release this fix before that investigation is complete.
@ravsii ravsii mentioned this pull request Feb 1, 2025
13 tasks
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.

Getting an error when mock does not exist?
2 participants