aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/path_test.go
diff options
context:
space:
mode:
authorPaschalis Tsilias <paschalis.tsilias@gmail.com>2021-11-13 02:18:30 +0200
committerGopher Robot <gobot@golang.org>2022-08-25 18:50:37 +0000
commit95a786da1265d84290c1a0d1186352f71475ff9f (patch)
treec5dd8a434728b36da18f6c552af916eed331997d /src/path/filepath/path_test.go
parent6a801d3082c6ab28372c115d13ef0c238e3535ae (diff)
downloadgo-95a786da1265d84290c1a0d1186352f71475ff9f.tar.xz
path/filepath, io/fs: add SkipAll
Fixes #47209 Change-Id: If75b0dd38f2c30a23517205d80c7a6683a5c921c Reviewed-on: https://go-review.googlesource.com/c/go/+/363814 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/path/filepath/path_test.go')
-rw-r--r--src/path/filepath/path_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index a783d6be28..9bdc58ea35 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -638,6 +638,64 @@ func TestWalkSkipDirOnFile(t *testing.T) {
})
}
+func TestWalkSkipAllOnFile(t *testing.T) {
+ td := t.TempDir()
+
+ if err := os.MkdirAll(filepath.Join(td, "dir", "subdir"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.MkdirAll(filepath.Join(td, "dir2"), 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ touch(t, filepath.Join(td, "dir", "foo1"))
+ touch(t, filepath.Join(td, "dir", "foo2"))
+ touch(t, filepath.Join(td, "dir", "subdir", "foo3"))
+ touch(t, filepath.Join(td, "dir", "foo4"))
+ touch(t, filepath.Join(td, "dir2", "bar"))
+ touch(t, filepath.Join(td, "last"))
+
+ remainingWereSkipped := true
+ walker := func(path string) error {
+ if strings.HasSuffix(path, "foo2") {
+ return filepath.SkipAll
+ }
+
+ if strings.HasSuffix(path, "foo3") ||
+ strings.HasSuffix(path, "foo4") ||
+ strings.HasSuffix(path, "bar") ||
+ strings.HasSuffix(path, "last") {
+ remainingWereSkipped = false
+ }
+ return nil
+ }
+
+ walkFn := func(path string, _ fs.FileInfo, _ error) error { return walker(path) }
+ walkDirFn := func(path string, _ fs.DirEntry, _ error) error { return walker(path) }
+
+ check := func(t *testing.T, walk func(root string) error, root string) {
+ t.Helper()
+ remainingWereSkipped = true
+ if err := walk(root); err != nil {
+ t.Fatal(err)
+ }
+ if !remainingWereSkipped {
+ t.Errorf("SkipAll on file foo2 did not block processing of remaining files and directories")
+ }
+ }
+
+ t.Run("Walk", func(t *testing.T) {
+ Walk := func(_ string) error { return filepath.Walk(td, walkFn) }
+ check(t, Walk, td)
+ check(t, Walk, filepath.Join(td, "dir"))
+ })
+ t.Run("WalkDir", func(t *testing.T) {
+ WalkDir := func(_ string) error { return filepath.WalkDir(td, walkDirFn) }
+ check(t, WalkDir, td)
+ check(t, WalkDir, filepath.Join(td, "dir"))
+ })
+}
+
func TestWalkFileError(t *testing.T) {
td := t.TempDir()