Skip to content

Commit

Permalink
Don't recurse into submodule
Browse files Browse the repository at this point in the history
Closes #706
  • Loading branch information
devnev committed Nov 12, 2023
1 parent deb4860 commit fc10b9c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 37 deletions.
73 changes: 38 additions & 35 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -224,7 +226,6 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
emptyMap := map[string]any{}
packageSection[packageName] = emptyMap
return emptyMap, nil

}

// GetPackageConfig returns a struct representation of the package's config
Expand Down Expand Up @@ -487,7 +488,6 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
// own `config` section and merge with the parent package
// if so.
subPkgConfig, err := c.getPackageConfigMap(ctx, subPkgPath)

if err != nil {
log.Err(err).Msg("could not get child package config")
return fmt.Errorf("failed to get sub-package config: %w", err)
Expand All @@ -502,7 +502,6 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg {
subPkgConfigSection[key] = val
}

}
}

Expand Down Expand Up @@ -556,17 +555,6 @@ func (c *Config) subPackages(
packageRootPath := searchRoot
subPackages := []string{}

walker, err := pathlib.NewWalk(
searchRoot,
pathlib.WalkAlgorithm(pathlib.AlgorithmBasic),
pathlib.WalkFollowSymlinks(false),
pathlib.WalkVisitDirs(false),
pathlib.WalkVisitFiles(true),
)
if err != nil {
return nil, fmt.Errorf("failed to create filesystem walker: %w", err)
}

visitedDirs := map[string]any{}
subdirectoriesWithGoFiles := []*pathlib.Path{}

Expand All @@ -577,31 +565,49 @@ func (c *Config) subPackages(
// Walk the filesystem path, starting at the root of the package we've
// been given. Note that this will always work because Golang downloads
// the package when we call `packages.Load`
walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error {
walkErr := filepath.Walk(searchRoot.String(), func(pathStr string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

_, 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")
return err
}
if autoGenerated {
log.Debug().Stringer("path", path).Msg("skipping file as auto-generated")
return nil
}
path := pathlib.NewPath(pathStr)
if info.IsDir() {
gomodPath := path.Join("go.mod")
gomodExists, err := gomodPath.Exists()
if err != nil {
log.Err(err).Stringer("path", gomodPath).Msg("failed to determine if go.mod exists")
return err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L578-L580

Added lines #L578 - L580 were not covered by tests
if gomodExists {
log.Debug().Stringer("path", path).Msg("skipping directory as sub-module")
return filepath.SkipDir
}
return nil
}

if _, haveVisitedDir := visitedDirs[path.Parent().String()]; haveVisitedDir {
return nil
}

l := log.With().Stringer("path", path.Parent()).Logger()
l.Debug().Msg("subdirectory has a .go file, adding this path to packages config")
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
visitedDirs[path.Parent().String()] = nil
if !strings.HasSuffix(path.Name(), ".go") {
return nil

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L593

Added line #L593 was not covered by tests
}

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")
return err
}
if autoGenerated {
log.Debug().Stringer("path", path).Msg("skipping file as auto-generated")
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L597-L605

Added lines #L597 - L605 were not covered by tests
}

log.Debug().Stringer("path", path.Parent()).Msg("subdirectory has a .go file, adding this path to packages config")
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
visitedDirs[path.Parent().String()] = nil
return nil
})
if walkErr != nil {
Expand Down Expand Up @@ -684,7 +690,6 @@ func (c *Config) discoverRecursivePackages(ctx context.Context) error {
log.Trace().Msg("done discovering recursive packages")

return nil

}

func contains[T comparable](slice []T, elem T) bool {
Expand Down Expand Up @@ -817,7 +822,6 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
}

return nil

}

func (c *Config) getInterfacesForPackage(ctx context.Context, pkgPath string) ([]string, error) {
Expand Down Expand Up @@ -875,5 +879,4 @@ func (c *Config) LogUnsupportedPackagesConfig(ctx context.Context) {
Str("url", logging.DocsURL("/configuration/#parameter-descriptions")).
Logger()
l.Error().Msg("use of unsupported options detected. mockery behavior is undefined.")

}
22 changes: 20 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,12 +1231,31 @@ packages:
recursive: true
with-expecter: true
with-expecter: false
`,
},
{
name: "test recursive with submodule",
cfgYaml: `
with-expecter: False
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod:
config:
recursive: True
with-expecter: True
all: True
`,
wantCfgMap: `packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_submod:
config:
all: true
recursive: true
with-expecter: true
with-expecter: false
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

ctx := context.Background()
tmpdir := pathlib.NewPath(t.TempDir())
cfg := tmpdir.Join("config.yaml")
Expand Down Expand Up @@ -1271,7 +1290,6 @@ want
------
%v`, string(cfgAsStr), tt.wantCfgMap)
}

})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/fixtures/example_project/pkg_with_submod/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pkg_with_subpkgs
1 change: 1 addition & 0 deletions pkg/fixtures/example_project/pkg_with_submod/submod/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package submod
3 changes: 3 additions & 0 deletions pkg/fixtures/example_project/pkg_with_submod/submod/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module submod

go 1.21.3

0 comments on commit fc10b9c

Please sign in to comment.