Skip to content

Commit

Permalink
Don't recurse into submodules on recursive: true
Browse files Browse the repository at this point in the history
This PR changes mockery to not recurse into paths that contain
a `go.mod` submodule. This is to help support monorepos that contain
many modules. In this case, mockery should not be recursing into these
because it causes issues, like in issue vektra#706.
  • Loading branch information
LandonTClipp committed Dec 19, 2023
1 parent 446e0bf commit a1525c9
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module github.com/vektra/mockery/v2

go 1.19
go 1.21

toolchain go1.21.0

require (
github.com/chigopher/pathlib v0.15.0
github.com/chigopher/pathlib v0.19.0
github.com/davecgh/go-spew v1.1.1
github.com/huandu/xstrings v1.4.0
github.com/iancoleman/strcase v0.2.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chigopher/pathlib v0.15.0 h1:1pg96WL3iC1/YyWV4UJSl3E0GBf4B+h5amBtsbAAieY=
github.com/chigopher/pathlib v0.15.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I=
github.com/chigopher/pathlib v0.18.0 h1:JJipJZFl+o+BBavTYnWHBlOp0hYqETRqHFuHyPX+C50=
github.com/chigopher/pathlib v0.18.0/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY=
github.com/chigopher/pathlib v0.19.0 h1:E+vKIGDJK58jLElqFSaFhtwgMlfHcPmtM/WiXwoCfRY=
github.com/chigopher/pathlib v0.19.0/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.19
go 1.21

toolchain go1.21.0

use (
.
Expand Down
15 changes: 13 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ func (c *Config) subPackages(

walker, err := pathlib.NewWalk(
searchRoot,
pathlib.WalkAlgorithm(pathlib.AlgorithmBasic),
pathlib.WalkAlgorithm(pathlib.AlgorithmPreOrderDepthFirst),
pathlib.WalkFollowSymlinks(false),
pathlib.WalkVisitDirs(false),
pathlib.WalkVisitFiles(true),
Expand All @@ -575,12 +575,23 @@ func (c *Config) subPackages(
visitedDirs[searchRoot.String()] = nil

// Walk the filesystem path, starting at the root of the package we've
// been given. Note that this will always work because Golang downloads
// been given. Note that this will always work because Go downloads
// the package when we call `packages.Load`
walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path.Name() == "go.mod" {
// Check if our current depth is 0. We do this to skip sub-modules, but
// not the current module we're generating mocks for.
relative, err := path.Parent().RelativeTo(searchRoot)
if err != nil {
return stackerr.NewStackErrf(err, "determining distance from search root")
}
if len(relative.Parts()) != 0 {
return pathlib.ErrWalkSkipSubtree
}
}

_, haveVisitedDir := visitedDirs[path.Parent().String()]
if !haveVisitedDir && strings.HasSuffix(path.Name(), ".go") {
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,23 @@ packages:
recursive: true
with-expecter: true
with-expecter: false
`,
},
{
name: "package with submodule that should be excluded",
cfgYaml: `
all: true
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules:
config:
recursive: True
`,
wantCfgMap: `all: true
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules:
config:
all: true
recursive: true
`,
},
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/fixtures/example_project/pkg_with_submodules/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pkg_with_submodules

type Stringer interface {
String() string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/vektra/mockery/v2/pkg/fixtures/example_project/submodule

go 1.21.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package submodule

type Stringer interface {
String() string
}

0 comments on commit a1525c9

Please sign in to comment.