Skip to content

Commit

Permalink
Merge branch 'paths' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
radovskyb committed Aug 17, 2019
2 parents f33c874 + 7f55b91 commit 37d22d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
14 changes: 8 additions & 6 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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}:
}
}
}
Expand All @@ -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
Expand All @@ -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}:
}
}
}
Expand Down
27 changes: 25 additions & 2 deletions watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
Expand All @@ -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")
}
Expand Down

0 comments on commit 37d22d7

Please sign in to comment.