add error when matchedFolders is empty

This commit is contained in:
Christian Schmied 2024-10-28 11:20:58 +01:00
parent 097c4b3d84
commit e4852b7cc7
Signed by: christian.schmied
GPG key ID: 5BD239286A047A18
2 changed files with 34 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package folderwatcher package folderwatcher
import ( import (
"fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"os" "os"
"path/filepath" "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 { 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) err = watcher.Add(path)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -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")
}
}