Skip to content

Commit

Permalink
Factor out logic to exclude sub-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Feb 15, 2024
1 parent 3b25f39 commit 8d53849
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,21 @@ func isAutoGenerated(path *pathlib.Path) (bool, error) {
return false, nil
}

func shouldExcludeModule(ctx context.Context, root *pathlib.Path, goModPath *pathlib.Path) (bool, error) {
log := zerolog.Ctx(ctx)
relative, err := goModPath.RelativeTo(root)
if err != nil {
return false, stackerr.NewStackErrf(err, "determining distance from search root")
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L535-L536

Added lines #L535 - L536 were not covered by tests

if len(relative.Parts()) != 1 {
log.Debug().Msg("skipping sub-module")
return true, nil
}
log.Debug().Int("parts_len", len(relative.Parts())).Str("parts", fmt.Sprintf("%v", relative.Parts())).Msg("not skipping module as this is the root path")
return false, nil
}

func (c *Config) subPackages(
ctx context.Context,
pkgPath string,
Expand Down Expand Up @@ -587,16 +602,14 @@ func (c *Config) subPackages(
pathLog.Debug().Msg("path contains go.mod file")
// Check if our current depth is 0. We do this to skip sub-modules, but not
// the root module.
relative, err := path.RelativeTo(searchRoot)
shouldExclude, err := shouldExcludeModule(ctx, searchRoot, path)
if err != nil {
return stackerr.NewStackErrf(err, "determining distance from search root")
return err

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L607

Added line #L607 was not covered by tests
}

if len(relative.Parts()) != 1 {
pathLog.Debug().Msg("skipping sub-module")
if shouldExclude {
return pathlib.ErrWalkSkipSubtree
}
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")
}

_, haveVisitedDir := visitedDirs[path.Parent().String()]
Expand All @@ -615,14 +628,20 @@ func (c *Config) subPackages(
}

pathLog.Debug().Msg("subdirectory has a .go file")
goModExists, err := path.Parent().Join("go.mod").Exists()
goModPath := path.Parent().Join("go.mod")
goModExists, err := goModPath.Exists()
if err != nil {
pathLog.Err(err).Msg("failed to determine if go.mod exists")
return err
}
if goModExists {
pathLog.Debug().Msg("found .go file, but go.mod exists. Skipping.")
return pathlib.ErrWalkSkipSubtree
shouldExclude, err := shouldExcludeModule(ctx, searchRoot, goModPath)
if err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/config/config.go#L640-L641

Added lines #L640 - L641 were not covered by tests
if shouldExclude {
return pathlib.ErrWalkSkipSubtree
}
}
subdirectoriesWithGoFiles = append(subdirectoriesWithGoFiles, path.Parent())
visitedDirs[path.Parent().String()] = nil
Expand Down

0 comments on commit 8d53849

Please sign in to comment.