From e4852b7cc743eef7a08555bc623b40e69047de1d Mon Sep 17 00:00:00 2001 From: Christian Schmied Date: Mon, 28 Oct 2024 11:20:58 +0100 Subject: [PATCH] add error when matchedFolders is empty --- folderwatcher.go | 12 ++++++++++++ folderwatcher_notExistingFolder_test.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 folderwatcher_notExistingFolder_test.go diff --git a/folderwatcher.go b/folderwatcher.go index e454da3..6e4203f 100644 --- a/folderwatcher.go +++ b/folderwatcher.go @@ -1,6 +1,7 @@ package folderwatcher import ( + "fmt" "github.com/sirupsen/logrus" "os" "path/filepath" @@ -52,7 +53,18 @@ func NewFolderWatcher(conf Config, includeExisting bool, quit chan struct{}) (*F } } + if len(matchedFolders) == 0 { + return nil, fmt.Errorf("no folders found matching '%s'", conf.Folder) + } + for _, path := range matchedFolders { + stat, err := os.Stat(path) + if err != nil { + return nil, err + } + if !stat.IsDir() { + return nil, fmt.Errorf("%s is not a folder", path) + } err = watcher.Add(path) if err != nil { return nil, err diff --git a/folderwatcher_notExistingFolder_test.go b/folderwatcher_notExistingFolder_test.go new file mode 100644 index 0000000..8cbdce8 --- /dev/null +++ b/folderwatcher_notExistingFolder_test.go @@ -0,0 +1,22 @@ +package folderwatcher + +import ( + "os" + "testing" +) + +func TestNotExistingFolder(t *testing.T) { + tmpPath, err := os.MkdirTemp("", ".folderwatcher_test") + if err != nil { + t.Fatal(err) + } + _ = os.RemoveAll(tmpPath) + conf := Config{ + Folder: tmpPath, + } + + _, err = NewFolderWatcher(conf, true, nil) + if err == nil { + t.Fatal("expected error") + } +}