diff --git a/fsevents.go b/fsevents.go index 6ff0797..fd0bd86 100644 --- a/fsevents.go +++ b/fsevents.go @@ -158,7 +158,9 @@ func (es *EventStream) Start() { // in C callback cbInfo := registry.Add(es) es.registryID = cbInfo - es.uuid = GetDeviceUUID(es.Device) + if es.Device != 0 { + es.uuid = GetDeviceUUID(es.Device) + } es.start(es.Paths, cbInfo) } diff --git a/fsevents_test.go b/fsevents_test.go index eda75c2..1396a6a 100644 --- a/fsevents_test.go +++ b/fsevents_test.go @@ -10,6 +10,36 @@ import ( "time" ) +func newEventStream(t *testing.T, path string, useDev bool) *EventStream { + es := &EventStream{ + Paths: []string{path}, + Latency: 500 * time.Millisecond, + Flags: FileEvents, + } + + if useDev { + dev, err := DeviceForPath(path) + if err != nil { + t.Fatal(err) + } + + es.Device = dev + } + + return es +} + +func processEvents(t *testing.T, es *EventStream, wait chan Event) { + for msg := range es.Events { + for _, event := range msg { + t.Logf("Event: %#v", event) + wait <- event + es.Stop() + return + } + } +} + func TestBasicExample(t *testing.T) { path, err := ioutil.TempDir("", "fsexample") if err != nil { @@ -17,33 +47,36 @@ func TestBasicExample(t *testing.T) { } defer os.RemoveAll(path) - dev, err := DeviceForPath(path) + es := newEventStream(t, path, true) + + es.Start() + + wait := make(chan Event) + go processEvents(t, es, wait) + + err = ioutil.WriteFile(filepath.Join(path, "example.txt"), []byte("example"), 0600) if err != nil { t.Fatal(err) } - es := &EventStream{ - Paths: []string{path}, - Latency: 500 * time.Millisecond, - Device: dev, - Flags: FileEvents, + <-wait +} + +func TestNoDevice(t *testing.T) { + path, err := ioutil.TempDir("", "fsexample") + if err != nil { + t.Fatal(err) } + defer os.RemoveAll(path) + + es := newEventStream(t, path, false) es.Start() wait := make(chan Event) - go func() { - for msg := range es.Events { - for _, event := range msg { - t.Logf("Event: %#v", event) - wait <- event - es.Stop() - return - } - } - }() + go processEvents(t, es, wait) - err = ioutil.WriteFile(filepath.Join(path, "example.txt"), []byte("example"), 0700) + err = ioutil.WriteFile(filepath.Join(path, "example.txt"), []byte("example"), 0600) if err != nil { t.Fatal(err) }