From f21f9d5a85644fcb453a1eddf58d85276bca118d Mon Sep 17 00:00:00 2001 From: Parker Duckworth Date: Thu, 2 Jun 2022 16:04:17 -0500 Subject: [PATCH 1/2] ignore fs.WalkDir errs in FindFiles --- script.go | 20 +++++++++++++------- script_test.go | 8 -------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/script.go b/script.go index d3a643d..a222466 100644 --- a/script.go +++ b/script.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io" + "io/fs" "os" "os/exec" "path/filepath" @@ -219,20 +220,25 @@ func File(name string) *Pipe { // // test/1.txt // test/2.txt -func FindFiles(path string) *Pipe { +func FindFiles(root string) *Pipe { var fileNames []string - walkFn := func(path string, info os.FileInfo, err error) error { + walkFn := func(path string, d fs.DirEntry, err error) error { if err != nil { return err } - if !info.IsDir() { - fileNames = append(fileNames, path) + if !d.IsDir() { + fileNames = append(fileNames, filepath.Join(root, path)) } return nil } - if err := filepath.Walk(path, walkFn); err != nil { - return NewPipe().WithError(err) - } + + // ignoring any errors here to more closely align + // behavior with `find`. for example, we wouldn't + // expect `find` to return an error without results + // if it encountered any permissions errors during + // its execution. see [github issue #99] + // (https://github.com/bitfield/script/issues/99) + _ = fs.WalkDir(os.DirFS(root), ".", walkFn) return Slice(fileNames) } diff --git a/script_test.go b/script_test.go index a7e9430..d50fdc1 100644 --- a/script_test.go +++ b/script_test.go @@ -1017,14 +1017,6 @@ func TestFindFiles_RecursesIntoSubdirectories(t *testing.T) { } } -func TestFindFiles_InNonexistentPathReturnsError(t *testing.T) { - t.Parallel() - p := script.FindFiles("nonexistent_path") - if p.Error() == nil { - t.Fatal("want error for nonexistent path") - } -} - func TestIfExists_ProducesErrorPlusNoOutputForNonexistentFile(t *testing.T) { t.Parallel() want := "" From 640a46ba220453bbc15a765b41ee2e971655f27d Mon Sep 17 00:00:00 2001 From: Parker Duckworth Date: Fri, 3 Jun 2022 10:40:25 -0500 Subject: [PATCH 2/2] add error case to FindFiles tests --- script_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/script_test.go b/script_test.go index d50fdc1..a9ca821 100644 --- a/script_test.go +++ b/script_test.go @@ -1017,6 +1017,25 @@ func TestFindFiles_RecursesIntoSubdirectories(t *testing.T) { } } +// FindFiles has been updated to ignore all errors +// see [github issue #99] +// (https://github.com/bitfield/script/issues/99) +func TestFindFiles_FailsSilently(t *testing.T) { + t.Parallel() + want := "\n" + p := script.FindFiles("testdata/nonexistent") + if p.Error() != nil { + t.Fatal(p.Error()) + } + got, err := p.String() + if err != nil { + t.Fatal(err) + } + if want != got { + t.Error(cmp.Diff(want, got)) + } +} + func TestIfExists_ProducesErrorPlusNoOutputForNonexistentFile(t *testing.T) { t.Parallel() want := ""