Skip to content

Commit

Permalink
Merge pull request #734 from vektra/revert-731-walk-skips-modules
Browse files Browse the repository at this point in the history
Revert "Don't recurse into submodules"
  • Loading branch information
LandonTClipp committed Nov 15, 2023
2 parents 0c5b6a4 + fa5b6cb commit c7a65e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 63 deletions.
73 changes: 35 additions & 38 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -502,6 +502,7 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
if _, keyInSubPkg := subPkgConfigSection[key]; !keyInSubPkg {
subPkgConfigSection[key] = val
}

}
}

Expand Down Expand Up @@ -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{}

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.")

}
22 changes: 2 additions & 20 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -1290,6 +1271,7 @@ want
------
%v`, string(cfgAsStr), tt.wantCfgMap)
}

})
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/fixtures/example_project/pkg_with_submod/foo.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/fixtures/example_project/pkg_with_submod/submod/foo.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/fixtures/example_project/pkg_with_submod/submod/go.mod

This file was deleted.

0 comments on commit c7a65e2

Please sign in to comment.