-
Notifications
You must be signed in to change notification settings - Fork 413
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
Support directory exclusion when generating mocks #458
Conversation
Directories which include a file named ".mockery_skip" are excluded from mock generation, as are their subdirectories.
nextFiles, err := ioutil.ReadDir(path) | ||
if err != nil { | ||
log.Err(err).Msgf("Error reading directory") | ||
return |
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.
Because the doWalk function doesn't provide a way to bubble the error up, I feel like this should be a fatal error. But, we really should fix doWalk
not returning errors.
} | ||
for _, nextFile := range nextFiles { | ||
if nextFile.Name() == ".mockery_skip" { | ||
continue OUTER |
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 tend to really avoid labels if at all possible. Actually, I think we can stat
for .mockery_skip
at the top of doWalk
and simply return if it exists.
Shameless plug but you might want to look into https://pkg.go.dev/github.com/chigopher/pathlib and then you can just do pathlib.Path(dir).Join(".mockery_skip").Exists()
to check.
@alec-w checking back up on this PR, I think this is a great feature and was wondering if you'd have time to take a look at the comments I have above? |
Excluding Directories | ||
--------------------- | ||
|
||
Directories can be excluded from mock generation by placing a file `.mockery_skip` inside them. Mocks will not be generated for any types present in a directory containing such a file nor for any in its sub-directories. |
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.
Actually, let's call this .mockery_skip_all
. I envision we might want a .mockery_skip
that skips just a single directory, whereas the _all
can skip the entire tree.
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.
By "skip the entire tree" vs "just a single directory" is the intended meaning that
- "skip the entire tree" is recursively down the tree from this point
- "just a single directory" is don't generate mocks for files in this directory but do for those in directories beneath this one
? (checking I've got that right in my head)
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.
Yep that sounds right, I feel like we might want both options at some point but we can just focus on "skip the entire tree" as you were originally doing. Thanks!
gv := NewGatheringVisitor() | ||
|
||
// Generating mocks, ignoring the fixtures directory | ||
skipMarker, err := os.Create(getFixturePath(".mockery_skip")) |
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.
Let's use pathlib for this.
@LandonTClipp sorry, yes I should have time to address the comments on this (and the other PR) this weekend. Sorry for the delay, and appreciate the feedback 👍 |
@alec-w checking back up on this, I think a number of people would love this feature, do you think you have some time to address the comments? Everyone's busy these days so no worries if not! |
For the folks that still want this feature, we will kind of get this "for free" in the feature implemented here: #548 So I will close this out in favor of that pull request. |
Description
Sometimes mocks are wanted for the majority of a project, but specific directories are to be excluded (e.g.
internal
).This adds support for excluding a directory and its sub-directories from mock generation by placing a file named
.mockery_skip
inside it.Type of change
Version of Golang used when building/testing:
How Has This Been Tested?
New test added that adds a
.mockery_skip
file into the the fixtures directory and then runs the same walker test asTestWalkerHere
but asserting that the generated mocks were the reduced set.Checklist