Skip to content

Commit

Permalink
Merge pull request #66 from LandonTClipp/bugfix
Browse files Browse the repository at this point in the history
Fix bug with ErrSkipSubtree at root
  • Loading branch information
LandonTClipp authored Dec 19, 2023
2 parents 11d563c + 9876a3a commit c2f8b21
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
8 changes: 4 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ var (
// ErrLstatNotPossible specifies that the filesystem does not support lstat-ing
ErrLstatNotPossible = fmt.Errorf("lstat is not possible")
// ErrRelativeTo indicates that we could not make one path relative to another
ErrRelativeTo = fmt.Errorf("failed to make path relative to other")
ErrWalk = fmt.Errorf("walk control")
ErrRelativeTo = fmt.Errorf("failed to make path relative to other")
errWalkControl = fmt.Errorf("walk control")
// ErrSkipSubtree indicates to the walk function that the current subtree of
// directories should be skipped. It's recommended to only use this error
// with the AlgorithmPreOrderDepthFirst algorithm, as many other walk algorithms
// will not respect this error due to the nature of the ordering in which the
// algorithms visit each node of the filesystem tree.
ErrWalkSkipSubtree = fmt.Errorf("skip subtree: %w", ErrWalk)
ErrWalkSkipSubtree = fmt.Errorf("skip subtree: %w", errWalkControl)
// ErrStopWalk indicates to the Walk function that the walk should be aborted.
// DEPRECATED: Use ErrWalkStop
ErrStopWalk = ErrWalkStop
// ErrWalkStop indicates to the Walk function that the walk should be aborted.
ErrWalkStop = fmt.Errorf("stop filesystem walk: %w", ErrWalk)
ErrWalkStop = fmt.Errorf("stop filesystem walk: %w", errWalkControl)
)
2 changes: 1 addition & 1 deletion walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func (w *Walk) Walk(walkFn WalkFunc) error {
return ErrInvalidAlgorithm
}
if err := algoFunc(walkFn, w.root, 0); err != nil {
if errors.Is(err, ErrStopWalk) {
if errors.Is(err, errWalkControl) {
return nil
}
return err
Expand Down
11 changes: 10 additions & 1 deletion walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,15 @@ func TestErrWalkSkipSubtree(t *testing.T) {
NewPath("subdir1").Join("subdir2", "foo.txt"),
},
},
{
"PreOrderDFS skip at root",
AlgorithmPreOrderDepthFirst,
nil,
NewPath("foo1.txt"),
[]*Path{
NewPath("foo1.txt"),
},
},
// Note about the PostOrderDFS case. ErrWalkSkipSubtree effectively
// has no meaning to this algorithm because in this case, the algorithm
// visits all children before visiting each node. Thus, our WalkFunc has
Expand All @@ -461,7 +470,7 @@ func TestErrWalkSkipSubtree(t *testing.T) {
} {
t.Run(tt.name, func(t *testing.T) {
root := NewPath(t.TempDir())
walker, err := NewWalk(root, WalkAlgorithm(tt.algorithm), WalkVisitDirs(false), WalkSortChildren(true))
walker, err := NewWalk(root, WalkAlgorithm(tt.algorithm), WalkVisitDirs(false), WalkVisitFiles(true), WalkSortChildren(true))
require.NoError(t, err)

var tree []*Path
Expand Down

0 comments on commit c2f8b21

Please sign in to comment.