54 lines
885 B
Go
54 lines
885 B
Go
|
package fsnotify_test
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/fsnotify/fsnotify"
|
||
|
"github.com/photoview/photoview/api/test_utils"
|
||
|
)
|
||
|
|
||
|
func TestMain(m *testing.M) {
|
||
|
os.Exit(test_utils.IntegrationTestRun(m))
|
||
|
}
|
||
|
|
||
|
func TestFSNotify(t *testing.T) {
|
||
|
// Create new watcher.
|
||
|
watcher, err := fsnotify.NewWatcher()
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer watcher.Close()
|
||
|
|
||
|
// Start listening for events.
|
||
|
go func() {
|
||
|
for {
|
||
|
select {
|
||
|
case event, ok := <-watcher.Events:
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
log.Println("event:", event)
|
||
|
if event.Has(fsnotify.Write) {
|
||
|
log.Println("modified file:", event.Name)
|
||
|
}
|
||
|
case err, ok := <-watcher.Errors:
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
log.Println("error:", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
// Add a path.
|
||
|
err = watcher.Add("/tmp")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Block main goroutine forever.
|
||
|
<-make(chan struct{})
|
||
|
}
|