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

Support directory exclusion when generating mocks #458

Closed
wants to merge 1 commit into from

Conversation

alec-w
Copy link

@alec-w alec-w commented Apr 26, 2022

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

  • 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 Golang used when building/testing:

  • 1.11
  • 1.12
  • 1.13
  • 1.14
  • 1.15
  • 1.16
  • 1.17
  • 1.18

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 as TestWalkerHere but asserting that the generated mocks were the reduced set.

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

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
Copy link
Collaborator

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
Copy link
Collaborator

@LandonTClipp LandonTClipp May 4, 2022

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.

@LandonTClipp
Copy link
Collaborator

@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.
Copy link
Collaborator

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.

Copy link
Author

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)

Copy link
Collaborator

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"))
Copy link
Collaborator

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.

@alec-w
Copy link
Author

alec-w commented May 26, 2022

@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?

@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 👍

@LandonTClipp
Copy link
Collaborator

@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!

@LandonTClipp
Copy link
Collaborator

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.

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.

3 participants