watch: fix a spurious error (#344)

This commit is contained in:
Nick Santos 2018-09-14 17:13:36 -04:00 committed by Nicolas De loof
parent c8a358a455
commit c5bce8bd42
2 changed files with 40 additions and 5 deletions

View File

@ -277,6 +277,32 @@ func TestWriteGoodLink(t *testing.T) {
f.assertEvents(goodFile, link)
}
func TestWatchBrokenLink(t *testing.T) {
f := newNotifyFixture(t)
defer f.tearDown()
newRoot, err := NewDir(t.Name())
if err != nil {
t.Fatal(err)
}
defer newRoot.TearDown()
link := filepath.Join(newRoot.Path(), "brokenLink")
missingFile := filepath.Join(newRoot.Path(), "missingFile")
err = os.Symlink(missingFile, link)
if err != nil {
t.Fatal(err)
}
err = f.notify.Add(newRoot.Path())
if err != nil {
t.Fatal(err)
}
os.Remove(link)
f.assertEvents(link)
}
type notifyFixture struct {
t *testing.T
root *TempDir

View File

@ -1,6 +1,7 @@
package watch
import (
"fmt"
"io/ioutil"
"log"
"os"
@ -28,26 +29,27 @@ type linuxNotify struct {
func (d *linuxNotify) Add(name string) error {
fi, err := os.Stat(name)
if err != nil && !os.IsNotExist(err) {
return err
return fmt.Errorf("notify.Add(%q): %v", name, err)
}
// if it's a file that doesn't exist watch it's parent
if os.IsNotExist(err) {
parent := filepath.Join(name, "..")
err = d.watcher.Add(parent)
if err != nil {
return err
return fmt.Errorf("notify.Add(%q): %v", name, err)
}
d.watchList[parent] = true
} else if fi.IsDir() {
err = d.watchRecursively(name)
if err != nil {
return err
return fmt.Errorf("notify.Add(%q): %v", name, err)
}
d.watchList[name] = true
} else {
err = d.watcher.Add(name)
if err != nil {
return err
return fmt.Errorf("notify.Add(%q): %v", name, err)
}
d.watchList[name] = true
}
@ -61,7 +63,14 @@ func (d *linuxNotify) watchRecursively(dir string) error {
return err
}
return d.watcher.Add(path)
err = d.watcher.Add(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("watcher.Add(%q): %v", path, err)
}
return nil
})
}