diff --git a/watcher.go b/watcher.go index ecb2fcd..4da4dfe 100644 --- a/watcher.go +++ b/watcher.go @@ -67,7 +67,8 @@ func (e Op) String() string { // directory and the type of event that's occurred and the full path of the file. type Event struct { Op - Path string + Path string + OldPath string os.FileInfo } @@ -636,14 +637,14 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, select { case <-cancel: return - case evt <- Event{Write, path, info}: + case evt <- Event{Write, path, path, info}: } } if oldInfo.Mode() != info.Mode() { select { case <-cancel: return - case evt <- Event{Chmod, path, info}: + case evt <- Event{Chmod, path, path, info}: } } } @@ -654,7 +655,8 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, if sameFile(info1, info2) { e := Event{ Op: Move, - Path: fmt.Sprintf("%s -> %s", path1, path2), + Path: path2, + OldPath: path1, FileInfo: info1, } // If they are from the same directory, it's a rename @@ -680,14 +682,14 @@ func (w *Watcher) pollEvents(files map[string]os.FileInfo, evt chan Event, select { case <-cancel: return - case evt <- Event{Create, path, info}: + case evt <- Event{Create, path, "", info}: } } for path, info := range removes { select { case <-cancel: return - case evt <- Event{Remove, path, info}: + case evt <- Event{Remove, path, path, info}: } } } diff --git a/watcher_test.go b/watcher_test.go index 77e767a..4453b99 100644 --- a/watcher_test.go +++ b/watcher_test.go @@ -581,6 +581,15 @@ func TestEventAddFile(t *testing.T) { files[event.Name()] = true events++ + // Check Path and OldPath content + newFile := filepath.Join(testDir, event.Name()) + if event.Path != newFile { + t.Errorf("Event.Path should be %s but got %s", newFile, event.Path) + } + if event.OldPath != "" { + t.Errorf("Event.OldPath should be empty on create, but got %s", event.OldPath) + } + if events == len(files) { return } @@ -678,6 +687,9 @@ func TestEventRenameFile(t *testing.T) { testDir, teardown := setup(t) defer teardown() + srcFilename := "file.txt" + dstFilename := "file1.txt" + w := New() w.FilterOps(Rename) @@ -688,8 +700,8 @@ func TestEventRenameFile(t *testing.T) { // Rename a file. if err := os.Rename( - filepath.Join(testDir, "file.txt"), - filepath.Join(testDir, "file1.txt"), + filepath.Join(testDir, srcFilename), + filepath.Join(testDir, dstFilename), ); err != nil { t.Error(err) } @@ -705,6 +717,17 @@ func TestEventRenameFile(t *testing.T) { if event.Op != Rename { t.Errorf("expected event to be Rename, got %s", event.Op) } + + // Check Path and OldPath content + oldFile := filepath.Join(testDir, srcFilename) + newFile := filepath.Join(testDir, dstFilename) + if event.Path != newFile { + t.Errorf("Event.Path should be %s but got %s", newFile, event.Path) + } + if event.OldPath != oldFile { + t.Errorf("Event.OldPath should %s but got %s", oldFile, event.OldPath) + } + case <-time.After(time.Millisecond * 250): t.Fatal("received no rename event") }