From 86fcc67dd3d665c42b7bd014e411896f7c723eb4 Mon Sep 17 00:00:00 2001 From: erstam Date: Sun, 3 Dec 2023 19:29:44 -0500 Subject: [PATCH] Add a test to validate the proposed fix --- afero_test.go | 2 +- path.go | 4 ++-- path_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/afero_test.go b/afero_test.go index 827c05c..58c6602 100644 --- a/afero_test.go +++ b/afero_test.go @@ -32,7 +32,7 @@ var ( Fss = []Fs{&MemMapFs{}, &OsFs{}} ) -var testRegistry map[Fs][]string = make(map[Fs][]string) +var testRegistry = make(map[Fs][]string) func testDir(fs Fs) string { name, err := TempDir(fs, "", "afero") diff --git a/path.go b/path.go index faf5129..13d4c1d 100644 --- a/path.go +++ b/path.go @@ -59,8 +59,8 @@ func walk(fs Fs, path string, info os.FileInfo, walkFn filepath.WalkFunc) error } for _, name := range names { - if name == path { - // skip current directory to avoid infinite recursion + if name == "" { + // skip empty names to avoid infinite recursion continue } filename := filepath.Join(path, name) diff --git a/path_test.go b/path_test.go index 104a6bc..874b448 100644 --- a/path_test.go +++ b/path_test.go @@ -67,3 +67,27 @@ func TestWalk(t *testing.T) { t.Fail() } } + +func TestWalkRemoveRoot(t *testing.T) { + defer removeAllTestFiles(t) + fs := Fss[0] + rootPath := "." + + err := fs.RemoveAll(rootPath) + if err != nil { + t.Error(err) + } + + testRegistry[fs] = append(testRegistry[fs], rootPath) + setupTestFiles(t, fs, rootPath) + + walkFn := func(path string, info os.FileInfo, err error) error { + fmt.Println(path, info.Name(), info.IsDir(), info.Size(), err) + return err + } + + err = Walk(fs, rootPath, walkFn) + if err != nil { + t.Error(err) + } +}