Repository: fsnotify/fsnotify
Pull request #498 (2022-08-10 16:11:55)

Add test to see what happens if you watch a symlink


Patch:
diff --git a/backend_inotify.go b/backend_inotify.go
index 7444191..855e654 100644
--- a/backend_inotify.go
+++ b/backend_inotify.go
@@ -312,7 +312,7 @@ func (w *Watcher) Remove(name string) error {
 	return nil
 }
 
-// WatchList returns the directories and files that are being monitered.
+// WatchList returns all paths added with Add() (and are not yet removed).
 func (w *Watcher) WatchList() []string {
 	w.mu.Lock()
 	defer w.mu.Unlock()
diff --git a/backend_kqueue.go b/backend_kqueue.go
index feb5705..409c645 100644
--- a/backend_kqueue.go
+++ b/backend_kqueue.go
@@ -332,7 +332,7 @@ func (w *Watcher) Remove(name string) error {
 	return nil
 }
 
-// WatchList returns the directories and files that are being monitered.
+// WatchList returns all paths added with Add() (and are not yet removed).
 func (w *Watcher) WatchList() []string {
 	w.mu.Lock()
 	defer w.mu.Unlock()
diff --git a/backend_windows.go b/backend_windows.go
index c52272b..a254053 100644
--- a/backend_windows.go
+++ b/backend_windows.go
@@ -252,7 +252,7 @@ func (w *Watcher) Remove(name string) error {
 	return <-in.reply
 }
 
-// WatchList returns the directories and files that are being monitered.
+// WatchList returns all paths added with Add() (and are not yet removed).
 func (w *Watcher) WatchList() []string {
 	w.mu.Lock()
 	defer w.mu.Unlock()
diff --git a/mkdoc.zsh b/mkdoc.zsh
index fd44b25..b6dff6e 100755
--- a/mkdoc.zsh
+++ b/mkdoc.zsh
@@ -119,6 +119,11 @@ close=$(<<EOF
 EOF
 )
 
+watchlist=$(<<EOF
+// WatchList returns all paths added with Add() (and are not yet removed).
+EOF
+)
+
 events=$(<<EOF
 	// Events sends the filesystem change events.
 	//
@@ -194,5 +199,6 @@ set-cmt '^func NewWatcher('                 $new
 set-cmt '^func (w \*Watcher) Add('          $add
 set-cmt '^func (w \*Watcher) Remove('       $remove
 set-cmt '^func (w \*Watcher) Close('        $close
+set-cmt '^func (w \*Watcher) WatchList('    $watchlist
 set-cmt '^[[:space:]]*Events *chan Event$'  $events
 set-cmt '^[[:space:]]*Errors *chan error$'  $errors


Test patch:
diff --git a/fsnotify_test.go b/fsnotify_test.go
index 993864e..be093ec 100644
--- a/fsnotify_test.go
+++ b/fsnotify_test.go
@@ -152,6 +152,61 @@ func TestWatch(t *testing.T) {
 		}, `
 			write    /file
 		`},
+
+		{"watch a symlink to a file", func(t *testing.T, w *Watcher, tmp string) {
+			if runtime.GOOS == "darwin" {
+				// TODO
+				// WRITE "/private/var/folders/.../TestWatchwatch_a_symlink_to_a_file183391030/001/file"
+				// Pretty sure this is caused by the broken symlink-follow
+				// behaviour too.
+				t.Skip("broken on macOS")
+			}
+
+			file := filepath.Join(tmp, "file")
+			link := filepath.Join(tmp, "link")
+			touch(t, file)
+			symlink(t, file, link)
+			addWatch(t, w, link)
+
+			cat(t, "hello", file)
+		}, `
+			write    /link
+
+			# TODO: Symlinks followed on kqueue; it shouldn't do this, but I'm
+			# afraid changing it will break stuff. See #227, #390
+			kqueue:
+				write    /file
+
+			# TODO: see if we can fix this.
+			windows:
+				empty
+		`},
+
+		{"watch a symlink to a dir", func(t *testing.T, w *Watcher, tmp string) {
+			if runtime.GOOS == "darwin" {
+				// TODO
+				// CREATE "/private/var/.../TestWatchwatch_a_symlink_to_a_dir2551725268/001/dir/file"
+				// Pretty sure this is caused by the broken symlink-follow
+				// behaviour too.
+
+				t.Skip("broken on macOS")
+			}
+
+			dir := filepath.Join(tmp, "dir")
+			link := filepath.Join(tmp, "link")
+			mkdir(t, dir)
+			symlink(t, dir, link)
+			addWatch(t, w, link)
+
+			touch(t, dir, "file")
+		}, `
+			create    /link/file
+
+			# TODO: Symlinks followed on kqueue; it shouldn't do this, but I'm
+			# afraid changing it will break stuff. See #227, #390
+			kqueue:
+				create /dir/file
+		`},
 	}
 
 	for _, tt := range tests {