From 460fd63cccd2f1d16fc4b1b761545b1649e14e28 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Sat, 12 Mar 2022 16:18:23 -0800 Subject: io/fs, path/filepath: honor SkipDir on second WalkDirFunc error call Fixes #51617 Change-Id: I03e9e575d9bad1481e7e4f051b50a077ba5f2fe0 Reviewed-on: https://go-review.googlesource.com/c/go/+/392154 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Emmanuel Odeke --- src/path/filepath/path_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/path/filepath/path_test.go') diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go index cfd0c8244d..1456ea737a 100644 --- a/src/path/filepath/path_test.go +++ b/src/path/filepath/path_test.go @@ -1526,3 +1526,38 @@ func TestEvalSymlinksAboveRootChdir(t *testing.T) { t.Logf("EvalSymlinks(%q) = %q", check, resolved) } } + +func TestIssue51617(t *testing.T) { + dir := t.TempDir() + for _, sub := range []string{"a", filepath.Join("a", "bad"), filepath.Join("a", "next")} { + if err := os.Mkdir(filepath.Join(dir, sub), 0755); err != nil { + t.Fatal(err) + } + } + bad := filepath.Join(dir, "a", "bad") + if err := os.Chmod(bad, 0); err != nil { + t.Fatal(err) + } + defer os.Chmod(bad, 0700) // avoid errors on cleanup + var saw []string + err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return filepath.SkipDir + } + if d.IsDir() { + rel, err := filepath.Rel(dir, path) + if err != nil { + t.Fatal(err) + } + saw = append(saw, rel) + } + return nil + }) + if err != nil { + t.Fatal(err) + } + want := []string{".", "a", filepath.Join("a", "bad"), filepath.Join("a", "next")} + if !reflect.DeepEqual(saw, want) { + t.Errorf("got directories %v, want %v", saw, want) + } +} -- cgit v1.3