Skip to content

Commit 4f9dc15

Browse files
authoredDec 19, 2023
Merge pull request #740 from LandonTClipp/monorepo
Don't recurse into submodules on `recursive: true`
2 parents 446e0bf + b248492 commit 4f9dc15

File tree

15 files changed

+85
-41
lines changed

15 files changed

+85
-41
lines changed
 

‎.github/workflows/golangci-lint.yml

-27
This file was deleted.

‎.github/workflows/testing.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
os: [ macos-latest, ubuntu-latest]
15-
go_vers: ['1.20']
15+
go_vers: ['1.21']
1616
steps:
1717
- uses: actions/checkout@v2
1818
with:

‎go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module github.com/vektra/mockery/v2
22

3-
go 1.19
3+
go 1.21
4+
5+
toolchain go1.21.0
46

57
require (
6-
github.com/chigopher/pathlib v0.15.0
8+
github.com/chigopher/pathlib v0.19.1
79
github.com/davecgh/go-spew v1.1.1
810
github.com/huandu/xstrings v1.4.0
911
github.com/iancoleman/strcase v0.2.0

‎go.sum

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4040
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4141
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
42-
github.com/chigopher/pathlib v0.15.0 h1:1pg96WL3iC1/YyWV4UJSl3E0GBf4B+h5amBtsbAAieY=
43-
github.com/chigopher/pathlib v0.15.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I=
42+
github.com/chigopher/pathlib v0.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A=
43+
github.com/chigopher/pathlib v0.19.1/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY=
4444
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
4545
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
4646
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -310,6 +310,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
310310
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
311311
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
312312
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
313+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
313314
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
314315
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
315316
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

‎go.work

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
go 1.19
1+
go 1.21
2+
3+
toolchain go1.21.0
24

35
use (
46
.
7+
./pkg/fixtures/example_project/pkg_with_submodules
58
./tools
69
)

‎mkdocs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ markdown_extensions:
4040
- attr_list
4141
- md_in_html
4242
- pymdownx.emoji:
43-
emoji_index: !!python/name:materialx.emoji.twemoji
44-
emoji_generator: !!python/name:materialx.emoji.to_svg
43+
emoji_index: !!python/name:material.extensions.emoji.twemoji
44+
emoji_generator: !!python/name:material.extensions.emoji.to_svg
4545
- pymdownx.details
4646
- pymdownx.highlight:
4747
anchor_linenums: true

‎pkg/config/config.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ func (c *Config) subPackages(
558558

559559
walker, err := pathlib.NewWalk(
560560
searchRoot,
561-
pathlib.WalkAlgorithm(pathlib.AlgorithmBasic),
561+
pathlib.WalkAlgorithm(pathlib.AlgorithmPreOrderDepthFirst),
562562
pathlib.WalkFollowSymlinks(false),
563563
pathlib.WalkVisitDirs(false),
564564
pathlib.WalkVisitFiles(true),
@@ -575,30 +575,45 @@ func (c *Config) subPackages(
575575
visitedDirs[searchRoot.String()] = nil
576576

577577
// Walk the filesystem path, starting at the root of the package we've
578-
// been given. Note that this will always work because Golang downloads
578+
// been given. Note that this will always work because Go downloads
579579
// the package when we call `packages.Load`
580580
walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error {
581+
pathLog := log.With().Stringer("path", path).Logger()
581582
if err != nil {
582583
return err
583584
}
585+
if path.Name() == "go.mod" {
586+
pathLog.Debug().Msg("path contains go.mod file")
587+
// Check if our current depth is 0. We do this to skip sub-modules, but not
588+
// the root module.
589+
relative, err := path.RelativeTo(searchRoot)
590+
if err != nil {
591+
return stackerr.NewStackErrf(err, "determining distance from search root")
592+
}
593+
594+
if len(relative.Parts()) != 1 {
595+
pathLog.Debug().Msg("skipping sub-module")
596+
return pathlib.ErrWalkSkipSubtree
597+
}
598+
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")
599+
}
584600

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

588604
if !c.IncludeAutoGenerated {
589605
autoGenerated, err := isAutoGenerated(path)
590606
if err != nil {
591-
log.Err(err).Stringer("path", path).Msg("failed to determine if file is auto-generated")
607+
pathLog.Err(err).Msg("failed to determine if file is auto-generated")
592608
return err
593609
}
594610
if autoGenerated {
595-
log.Debug().Stringer("path", path).Msg("skipping file as auto-generated")
611+
pathLog.Debug().Msg("skipping file as auto-generated")
596612
return nil
597613
}
598614
}
599615

600-
l := log.With().Stringer("path", path.Parent()).Logger()
601-
l.Debug().Msg("subdirectory has a .go file, adding this path to packages config")
616+
pathLog.Debug().Msg("subdirectory has a .go file, adding this path to packages config")
602617
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
603618
visitedDirs[path.Parent().String()] = nil
604619
}

‎pkg/config/config_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,27 @@ packages:
12311231
recursive: true
12321232
with-expecter: true
12331233
with-expecter: false
1234+
`,
1235+
},
1236+
{
1237+
name: "package with submodule that should be excluded",
1238+
cfgYaml: `
1239+
all: true
1240+
packages:
1241+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules:
1242+
config:
1243+
recursive: True
1244+
`,
1245+
wantCfgMap: `all: true
1246+
packages:
1247+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules:
1248+
config:
1249+
all: true
1250+
recursive: true
1251+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules/subpkg:
1252+
config:
1253+
all: true
1254+
recursive: true
12341255
`,
12351256
},
12361257
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules
2+
3+
go 1.19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package pkg_with_submodules
2+
3+
type Stringer interface {
4+
String() string
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/vektra/mockery/v2/pkg/fixtures/example_project/submodule
2+
3+
go 1.21.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package submodule
2+
3+
type Stringer interface {
4+
String() string
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package subpkg
2+
3+
type Stringer interface {
4+
String() string
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submodules/subpkg/submodule
2+
3+
go 1.19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package submodule
2+
3+
type Stringer interface {
4+
String() string
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.