From fa5b6cb75d2a67dfa5b5d884c2d74f6bcdfd69c6 Mon Sep 17 00:00:00 2001 From: Landon Clipp Date: Wed, 15 Nov 2023 10:11:52 -0600 Subject: [PATCH] Revert "Don't recurse into submodules" --- 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, 37 insertions(+), 63 deletions(-) delete mode 100644 pkg/fixtures/example_project/pkg_with_submod/foo.go delete mode 100644 pkg/fixtures/example_project/pkg_with_submod/submod/foo.go delete 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 437f1c3d..c709f4ba 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,9 +5,7 @@ import ( "context" "errors" "fmt" - "io/fs" "os" - "path/filepath" "reflect" "regexp" "strings" @@ -226,6 +224,7 @@ 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 @@ -488,6 +487,7 @@ 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,6 +502,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg { subPkgConfigSection[key] = val } + } } @@ -555,6 +556,17 @@ 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{} @@ -565,49 +577,31 @@ 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 := filepath.Walk(searchRoot.String(), func(pathStr string, info fs.FileInfo, err error) error { + walkErr := walker.Walk(func(path *pathlib.Path, info os.FileInfo, err error) error { if err != nil { return err } - 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 - } - - if !strings.HasSuffix(path.Name(), ".go") { - return nil - } + _, 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 + 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 + 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 + } return nil }) if walkErr != nil { @@ -690,6 +684,7 @@ 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 { @@ -822,6 +817,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error { } return nil + } func (c *Config) getInterfacesForPackage(ctx context.Context, pkgPath string) ([]string, error) { @@ -879,4 +875,5 @@ 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 0e419865..ce5cad6b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -1231,31 +1231,12 @@ 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") @@ -1290,6 +1271,7 @@ 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 deleted file mode 100644 index 480f9084..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/foo.go +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 7e353e3a..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/submod/foo.go +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 7d2e1ee1..00000000 --- a/pkg/fixtures/example_project/pkg_with_submod/submod/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module submod - -go 1.21.3