8000 Fixing folder events in file watcher by tanema · Pull Request #889 · Shopify/themekit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixing folder events in file watcher #889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of servi 8000 ce and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/file/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,14 @@ func (w *Watcher) watchFsEvents() {
}

func (w *Watcher) onEvent(event watcher.Event) bool {
if event.IsDir() {
if isEventType(event.Op, watcher.Create) {
w.fsWatcher.Add(event.Path)
}
return false
}
events := map[string]Event{}
for _, event := range w.translateEvent(event) {
events[event.Path] = event
for _, e := range w.translateEvent(event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] perhaps rename e to translatedEvent?

events[e.Path] = e
}
if len(events) == 0 {
return false
}

drainTimer := time.NewTimer(drainTimeout)
defer drainTimer.Stop()
for {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual issue is on line 143 where we get a second stage of events, the directory event filtering from line 125 was not duplicated there.

Expand Down Expand Up @@ -164,7 +162,11 @@ func (w *Watcher) updateChecksum(e Event) {

func (w *Watcher) translateEvent(event watcher.Event) []Event {
oldPath, currentPath := w.parsePath(event.OldPath), w.parsePath(event.Path)
if isEventType(event.Op, watcher.Rename, watcher.Move) {
if event.IsDir() {
if isEventType(event.Op, watcher.Create) {
w.fsWatcher.Add(event.Path)
}
} else if isEventType(event.Op, watcher.Rename, watcher.Move) {
return []Event{{Op: Remove, Path: oldPath}, {Op: Update, Path: currentPath, LastKnownChecksum: w.checksums[currentPath]}}
} else if isEventType(event.Op, watcher.Remove) {
return []Event{{Op: Remove, Path: currentPath}}
Expand Down
37 changes: 37 additions & 0 deletions src/file/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,43 @@ func TestFileWatcher_OnEvent(t *testing.T) {
}
}

func TestFileWatcher_translateEvent(t *testing.T) {
testcases := []struct {
filename, oldname string
op watcher.Op
expectedOp OptSlice
}{
{expectedOp: OptSlice{Update}, filename: "_testdata/project/templates/template.liquid", op: watcher.Write},
{expectedOp: OptSlice{Update}, filename: "_testdata/project/templates/customers/test.liquid", op: watcher.Create},
{expectedOp: OptSlice{}, filename: "_testdata/project/config", op: watcher.Create},
{expectedOp: OptSlice{Remove}, filename: "_testdata/project/templates/customers/test.liquid", op: watcher.Remove},
{expectedOp: OptSlice{Remove, Update}, filename: "_testdata/project/assets/application.js.liquid", oldname: "_testdata/project/assets/application.js", op: watcher.Rename},
{expectedOp: OptSlice{Remove, Update}, filename: "_testdata/project/assets/application.js.liquid", oldname: "_testdata/project/assets/application.js", op: watcher.Move},
}

for _, testcase := range testcases {
info, _ := os.Stat(testcase.filename)
evt := watcher.Event{
Op: testcase.op,
Path: testcase.filename,
OldPath: testcase.oldname,
FileInfo: info,
}

w := createTestWatcher(t)
events := w.translateEvent(evt)
assert.Equal(t, len(testcase.expectedOp), len(events))
recievedEvents := OptSlice{}
for _, e := range events {
recievedEvents = append(recievedEvents, e.Op)
}
sort.Sort(testcase.expectedOp)
sort.Sort(recievedEvents)
assert.Equal(t, testcase.expectedOp, recievedEvents)
w.Stop()
}
}

func TestFileWatcher_debouncing(t *testing.T) {
w := createTestWatcher(t)
w.Events = make(chan Event, 10)
Expand Down
0