From fc10b9c1924e91f08d01380d08af70e60731c8aa Mon Sep 17 00:00:00 2001 From: Mark Nevill Date: Sun, 12 Nov 2023 12:02:48 +0000 Subject: [PATCH] Don't recurse into submodule Closes #706 --- pkg/config/config.go | 73 ++++++++++--------- pkg/config/config_test.go | 22 +++++- .../example_project/pkg_with_submod/foo.go | 1 + .../pkg_with_submod/submod/foo.go | 1 + .../pkg_with_submod/submod/go.mod | 3 + 5 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 pkg/fixtures/example_project/pkg_with_submod/foo.go create mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/foo.go create mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/go.mod diff --git a/pkg/config/config.go b/pkg/config/config.go index c709f4ba..437f1c3d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,7 +5,9 @@ import ( "context" "errors" "fmt" + "io/fs" "os" + "path/filepath" "reflect" "regexp" "strings" @@ -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 @@ -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) @@ -502,7 +502,6 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg { subPkgConfigSection[key] = val } - } } @@ -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{} @@ -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 + } + 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 } + + 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 + } + } + + 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 { @@ -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 { @@ -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) { @@ -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.") - } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index ce5cad6b..0e419865 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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") @@ -1271,7 +1290,6 @@ want ------ %v`, string(cfgAsStr), tt.wantCfgMap) } - }) } } diff --git a/pkg/fixtures/example_project/pkg_with_submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/foo.go new file mode 100644 index 00000000..480f9084 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/foo.go @@ -0,0 +1 @@ +package pkg_with_subpkgs diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go b/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go new file mode 100644 index 00000000..7e353e3a --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go @@ -0,0 +1 @@ +package submod diff --git a/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod b/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod new file mode 100644 index 00000000..7d2e1ee1 --- /dev/null +++ b/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod @@ -0,0 +1,3 @@ +module submod + +go 1.21.3