add error when matchedFolders is empty
This commit is contained in:
parent
097c4b3d84
commit
e4852b7cc7
2 changed files with 34 additions and 0 deletions
|
@ -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
|
||||
|
|
22
folderwatcher_notExistingFolder_test.go
Normal file
22
folderwatcher_notExistingFolder_test.go
Normal 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")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue