Skip to content

Commit dda0362

Browse files
nicksndeloof
authored andcommitted
watch: fix inotify tests on windows (docker#3140)
1 parent ddc88ec commit dda0362

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

pkg/watch/notify_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !windows
2-
31
package watch
42

53
import (
@@ -41,6 +39,9 @@ func TestNoWatches(t *testing.T) {
4139
}
4240

4341
func TestEventOrdering(t *testing.T) {
42+
if runtime.GOOS == "windows" {
43+
t.Skip("Skipping on windows for now")
44+
}
4445
f := newNotifyFixture(t)
4546
defer f.tearDown()
4647

@@ -73,6 +74,9 @@ func TestEventOrdering(t *testing.T) {
7374
// of directories, creates files in them, then deletes
7475
// them all quickly. Make sure there are no errors.
7576
func TestGitBranchSwitch(t *testing.T) {
77+
if runtime.GOOS == "windows" {
78+
t.Skip("Skipping on windows for now")
79+
}
7680
f := newNotifyFixture(t)
7781
defer f.tearDown()
7882

@@ -143,10 +147,11 @@ func TestWatchesAreRecursive(t *testing.T) {
143147
f.events = nil
144148
// change sub directory
145149
changeFilePath := filepath.Join(subPath, "change")
146-
_, err := os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
150+
h, err := os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
147151
if err != nil {
148152
t.Fatal(err)
149153
}
154+
defer h.Close()
150155

151156
f.assertEvents(changeFilePath)
152157
}
@@ -168,10 +173,12 @@ func TestNewDirectoriesAreRecursivelyWatched(t *testing.T) {
168173

169174
// change something inside sub directory
170175
changeFilePath := filepath.Join(subPath, "change")
171-
_, err := os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
176+
h, err := os.OpenFile(changeFilePath, os.O_RDONLY|os.O_CREATE, 0666)
172177
if err != nil {
173178
t.Fatal(err)
174179
}
180+
defer h.Close()
181+
175182
f.assertEvents(subPath, changeFilePath)
176183
}
177184

@@ -278,6 +285,9 @@ func TestSingleFile(t *testing.T) {
278285
}
279286

280287
func TestWriteBrokenLink(t *testing.T) {
288+
if runtime.GOOS == "windows" {
289+
t.Skip("Symlink creation requires admin privileges on Windows")
290+
}
281291
f := newNotifyFixture(t)
282292
defer f.tearDown()
283293

@@ -292,6 +302,9 @@ func TestWriteBrokenLink(t *testing.T) {
292302
}
293303

294304
func TestWriteGoodLink(t *testing.T) {
305+
if runtime.GOOS == "windows" {
306+
t.Skip("Symlink creation requires admin privileges on Windows")
307+
}
295308
f := newNotifyFixture(t)
296309
defer f.tearDown()
297310

@@ -311,6 +324,9 @@ func TestWriteGoodLink(t *testing.T) {
311324
}
312325

313326
func TestWatchBrokenLink(t *testing.T) {
327+
if runtime.GOOS == "windows" {
328+
t.Skip("Symlink creation requires admin privileges on Windows")
329+
}
314330
f := newNotifyFixture(t)
315331
defer f.tearDown()
316332

@@ -339,6 +355,10 @@ func TestWatchBrokenLink(t *testing.T) {
339355
}
340356

341357
func TestMoveAndReplace(t *testing.T) {
358+
if runtime.GOOS == "windows" {
359+
t.Skip("Skipping on windows for now")
360+
}
361+
342362
f := newNotifyFixture(t)
343363
defer f.tearDown()
344364

pkg/watch/watcher_naive.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"runtime"
910

1011
"github.com/pkg/errors"
1112
"github.com/windmilleng/fsnotify"
@@ -135,7 +136,7 @@ func (d *naiveNotify) loop() {
135136
defer close(d.wrappedEvents)
136137
for e := range d.events {
137138
if e.Op&fsnotify.Create != fsnotify.Create {
138-
if d.shouldNotify(e.Name) {
139+
if d.shouldNotify(e.Name) && !isSpuriousWindowsDirChange(e) {
139140
d.wrappedEvents <- FileEvent{e.Name}
140141
}
141142
continue
@@ -262,6 +263,22 @@ func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*naiveNoti
262263
return wmw, nil
263264
}
264265

266+
// Windows' inotify implementation sometimes fires
267+
// of spurious WRITE events on directories when the
268+
// files inside change.
269+
func isSpuriousWindowsDirChange(e fsnotify.Event) bool {
270+
if runtime.GOOS != "windows" {
271+
return false
272+
}
273+
274+
if e.Op != fsnotify.Write {
275+
return false
276+
}
277+
278+
eIsDir, _ := isDir(e.Name)
279+
return eIsDir
280+
}
281+
265282
func isDir(pth string) (bool, error) {
266283
fi, err := os.Lstat(pth)
267284
if os.IsNotExist(err) {

pkg/watch/watcher_naive_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// +build !darwin,!windows
1+
// +build !darwin
22

33
package watch
44

55
import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"runtime"
910
"strconv"
1011
"strings"
1112
"testing"
@@ -92,6 +93,9 @@ func TestDontWatchEachFile(t *testing.T) {
9293
f.events = nil
9394

9495
pid := os.Getpid()
96+
if runtime.GOOS == "windows" {
97+
return // skip the inotify count
98+
}
9599

96100
output, err := exec.Command("bash", "-c", fmt.Sprintf(
97101
"find /proc/%d/fd -lname anon_inode:inotify -printf '%%hinfo/%%f\n' | xargs cat | grep -c '^inotify'", pid)).Output()

0 commit comments

Comments
 (0)