From 9876a3aec0a8aee8263f929d7652f1eee0c0f1bd Mon Sep 17 00:00:00 2001 From: LandonTClipp Date: Tue, 19 Dec 2023 14:20:03 -0600 Subject: [PATCH] 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. --- errors.go | 8 ++++---- walk.go | 2 +- walk_test.go | 11 ++++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/errors.go b/errors.go index d037225..28b6b9a 100644 --- a/errors.go +++ b/errors.go @@ -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) ) diff --git a/walk.go b/walk.go index 60357a4..c50a9c9 100644 --- a/walk.go +++ b/walk.go @@ -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 diff --git a/walk_test.go b/walk_test.go index 67a9aac..c0912de 100644 --- a/walk_test.go +++ b/walk_test.go @@ -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 @@ -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