From 4b4188d1b11e7009238637c4a884cd24d9e08c92 Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Sun, 4 Jun 2023 13:00:02 +0100 Subject: [PATCH 1/2] Don't attempt to open directories --- internal/count/count.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/count/count.go b/internal/count/count.go index be72f2d..b1a6e2d 100644 --- a/internal/count/count.go +++ b/internal/count/count.go @@ -152,10 +152,13 @@ func worker(counts chan<- Result, files <-chan string, wg *sync.WaitGroup) { for file := range files { f, err := os.Open(file) if err != nil { - // If we can't open the file, just put an empty count on the results - // channel and move on - // TODO: Could probably improve this - counts <- Result{Name: file} + // If we can't open the (possibly dir) path + // then just continue + continue + } + info, _ := f.Stat() //nolint: errcheck // The file is already open here so we can ignore the error + // Skip directories + if info.IsDir() { continue } result, err := One(f, file) From 0faacde78eb4459f592d8c3b62b745978524ae48 Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Sun, 4 Jun 2023 13:03:48 +0100 Subject: [PATCH 2/2] Test that dirs are ignored --- internal/count/count_test.go | 1 + internal/count/testdata/dir/.gitkeep | 0 2 files changed, 1 insertion(+) create mode 100644 internal/count/testdata/dir/.gitkeep diff --git a/internal/count/count_test.go b/internal/count/count_test.go index 6f30e21..03fc30b 100644 --- a/internal/count/count_test.go +++ b/internal/count/count_test.go @@ -113,6 +113,7 @@ func TestCountAll(t *testing.T) { filepath.Join("testdata", "moby_dick.txt"), filepath.Join("testdata", "another.txt"), filepath.Join("testdata", "onemore.txt"), + filepath.Join("testdata", "dir"), } results, err := count.All(files) diff --git a/internal/count/testdata/dir/.gitkeep b/internal/count/testdata/dir/.gitkeep new file mode 100644 index 0000000..e69de29