Skip to content

Commit 9876a3a

Browse files
committed
Fix bug with ErrSkipSubtree at root
A bug existed where the ErrSkipSubtree error would be returned from `Walk()` if the error was specified at the root of the tree.
1 parent 11d563c commit 9876a3a

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ var (
1313
// ErrLstatNotPossible specifies that the filesystem does not support lstat-ing
1414
ErrLstatNotPossible = fmt.Errorf("lstat is not possible")
1515
// ErrRelativeTo indicates that we could not make one path relative to another
16-
ErrRelativeTo = fmt.Errorf("failed to make path relative to other")
17-
ErrWalk = fmt.Errorf("walk control")
16+
ErrRelativeTo = fmt.Errorf("failed to make path relative to other")
17+
errWalkControl = fmt.Errorf("walk control")
1818
// ErrSkipSubtree indicates to the walk function that the current subtree of
1919
// directories should be skipped. It's recommended to only use this error
2020
// with the AlgorithmPreOrderDepthFirst algorithm, as many other walk algorithms
2121
// will not respect this error due to the nature of the ordering in which the
2222
// algorithms visit each node of the filesystem tree.
23-
ErrWalkSkipSubtree = fmt.Errorf("skip subtree: %w", ErrWalk)
23+
ErrWalkSkipSubtree = fmt.Errorf("skip subtree: %w", errWalkControl)
2424
// ErrStopWalk indicates to the Walk function that the walk should be aborted.
2525
// DEPRECATED: Use ErrWalkStop
2626
ErrStopWalk = ErrWalkStop
2727
// ErrWalkStop indicates to the Walk function that the walk should be aborted.
28-
ErrWalkStop = fmt.Errorf("stop filesystem walk: %w", ErrWalk)
28+
ErrWalkStop = fmt.Errorf("stop filesystem walk: %w", errWalkControl)
2929
)

walk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func (w *Walk) Walk(walkFn WalkFunc) error {
395395
return ErrInvalidAlgorithm
396396
}
397397
if err := algoFunc(walkFn, w.root, 0); err != nil {
398-
if errors.Is(err, ErrStopWalk) {
398+
if errors.Is(err, errWalkControl) {
399399
return nil
400400
}
401401
return err

walk_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ func TestErrWalkSkipSubtree(t *testing.T) {
441441
NewPath("subdir1").Join("subdir2", "foo.txt"),
442442
},
443443
},
444+
{
445+
"PreOrderDFS skip at root",
446+
AlgorithmPreOrderDepthFirst,
447+
nil,
448+
NewPath("foo1.txt"),
449+
[]*Path{
450+
NewPath("foo1.txt"),
451+
},
452+
},
444453
// Note about the PostOrderDFS case. ErrWalkSkipSubtree effectively
445454
// has no meaning to this algorithm because in this case, the algorithm
446455
// visits all children before visiting each node. Thus, our WalkFunc has
@@ -461,7 +470,7 @@ func TestErrWalkSkipSubtree(t *testing.T) {
461470
} {
462471
t.Run(tt.name, func(t *testing.T) {
463472
root := NewPath(t.TempDir())
464-
walker, err := NewWalk(root, WalkAlgorithm(tt.algorithm), WalkVisitDirs(false), WalkSortChildren(true))
473+
walker, err := NewWalk(root, WalkAlgorithm(tt.algorithm), WalkVisitDirs(false), WalkVisitFiles(true), WalkSortChildren(true))
465474
require.NoError(t, err)
466475

467476
var tree []*Path

0 commit comments

Comments
 (0)