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 #706.

fix deprecations in mkdocs

remove explicit lint step
  • Loading branch information
LandonTClipp committed Dec 19, 2023
1 parent 446e0bf commit b248492
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 41 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/golangci-lint.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest]
go_vers: ['1.20']
go_vers: ['1.21']
steps:
- uses: actions/checkout@v2
with:
Expand Down
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.1
github.com/davecgh/go-spew v1.1.1
github.com/huandu/xstrings v1.4.0
github.com/iancoleman/strcase v0.2.0
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
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.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A=
github.com/chigopher/pathlib v0.19.1/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 Expand Up @@ -310,6 +310,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
5 changes: 4 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
go 1.19
go 1.21

toolchain go1.21.0

use (
.
./pkg/fixtures/example_project/pkg_with_submodules
./tools
)
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ markdown_extensions:
- attr_list
- md_in_html
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_index: !!python/name:material.extensions.emoji.twemoji
emoji_generator: !!python/name:material.extensions.emoji.to_svg
- pymdownx.details
- pymdownx.highlight:
anchor_linenums: true
Expand Down
27 changes: 21 additions & 6 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,30 +575,45 @@ 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 {
pathLog := log.With().Stringer("path", path).Logger()
if err != nil {
return err
}
if path.Name() == "go.mod" {
pathLog.Debug().Msg("path contains go.mod file")
// Check if our current depth is 0. We do this to skip sub-modules, but not
// the root module.
relative, err := path.RelativeTo(searchRoot)
if err != nil {
return stackerr.NewStackErrf(err, "determining distance from search root")
}

Check warning on line 592 in pkg/config/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L591-L592

Added lines #L591 - L592 were not covered by tests

if len(relative.Parts()) != 1 {
pathLog.Debug().Msg("skipping sub-module")
return pathlib.ErrWalkSkipSubtree
}
pathLog.Debug().Int("parts_len", len(relative.Parts())).Str("parts", fmt.Sprintf("%v", relative.Parts())).Msg("not skipping module as this is the root path")
}

_, haveVisitedDir := visitedDirs[path.Parent().String()]
if !haveVisitedDir && strings.HasSuffix(path.Name(), ".go") {

if !c.IncludeAutoGenerated {
autoGenerated, err := isAutoGenerated(path)
if err != nil {
log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated")
pathLog.Err(err).Msg("failed to determine if file is auto-generated")

Check warning on line 607 in pkg/config/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L607

Added line #L607 was not covered by tests
return err
}
if autoGenerated {
log.Debug().Stringer("path", path).Msg("skipping file as auto-generated")
pathLog.Debug().Msg("skipping file as auto-generated")

Check warning on line 611 in pkg/config/config.go

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L611

Added line #L611 was not covered by tests
return nil
}
}

l := log.With().Stringer("path", path.Parent()).Logger()
l.Debug().Msg("subdirectory has a .go file, adding this path to packages config")
pathLog.Debug().Msg("subdirectory has a .go file, adding this path to packages config")
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
visitedDirs[path.Parent().String()] = nil
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,27 @@ 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
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules/subpkg:
config:
all: true
recursive: true
`,
},
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/fixtures/example_project/pkg_with_submodules/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules

go 1.19
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subpkg

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/pkg_with_submodules/subpkg/submodule

go 1.19
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 b248492

Please sign in to comment.