Skip to content

Commit 179edb8

Browse files
Corentin-proTobiasRzepka
authored andcommitted
tests: Add follow symlink tests (gorakhargosh#1087)
Co-authored-by: Corentin <[email protected]>
1 parent 1c4b901 commit 179edb8

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/watchdog/observers/fsevents.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,6 @@ def schedule(
337337
if isinstance(path, str):
338338
path = unicodedata.normalize("NFC", path)
339339

340-
return super().schedule(event_handler, path, recursive=recursive, follow_symlink=follow_symlink,
341-
event_filter=event_filter)
340+
return super().schedule(
341+
event_handler, path, recursive=recursive, follow_symlink=follow_symlink, event_filter=event_filter
342+
)

tests/shell.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def mkdir(path, *, parents=False):
4242
os.mkdir(path)
4343

4444

45+
def symlink(source, destination, *, target_is_directory: bool = False):
46+
os.symlink(source, destination, target_is_directory=target_is_directory)
47+
48+
4549
def rm(path, *, recursive=False):
4650
"""Deletes files or directories."""
4751
if os.path.isdir(path):

tests/test_inotify_buffer.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from watchdog.observers.inotify_buffer import InotifyBuffer
1515

16-
from .shell import mkdir, mount_tmpfs, mv, rm, touch, unmount
16+
from .shell import mkdir, mount_tmpfs, mv, rm, symlink, touch, unmount
1717

1818

1919
def wait_for_move_event(read_event):
@@ -65,6 +65,21 @@ def test_move_internal(p):
6565
inotify.close()
6666

6767

68+
@pytest.mark.timeout(5)
69+
def test_move_internal_symlink_followed(p):
70+
mkdir(p("dir", "dir1"), parents=True)
71+
mkdir(p("dir", "dir2"))
72+
touch(p("dir", "dir1", "a"))
73+
symlink(p("dir"), p("symdir"), target_is_directory=True)
74+
75+
inotify = InotifyBuffer(p("symdir").encode(), recursive=True, follow_symlink=True)
76+
mv(p("dir", "dir1", "a"), p("dir", "dir2", "b"))
77+
frm, to = wait_for_move_event(inotify.read_event)
78+
assert frm.src_path == p("symdir", "dir1", "a").encode()
79+
assert to.src_path == p("symdir", "dir2", "b").encode()
80+
inotify.close()
81+
82+
6883
@pytest.mark.timeout(10)
6984
def test_move_internal_batch(p):
7085
n = 100
@@ -102,6 +117,21 @@ def test_delete_watched_directory(p):
102117
inotify.close()
103118

104119

120+
@pytest.mark.timeout(5)
121+
def test_delete_watched_directory_symlink_followed(p):
122+
mkdir(p("dir", "dir2"), parents=True)
123+
symlink(p("dir"), p("symdir"), target_is_directory=True)
124+
125+
inotify = InotifyBuffer(p("symdir").encode(), follow_symlink=True)
126+
rm(p("dir", "dir2"), recursive=True)
127+
128+
# Wait for the event to be picked up
129+
inotify.read_event()
130+
131+
# Ensure InotifyBuffer shuts down cleanly without raising an exception
132+
inotify.close()
133+
134+
105135
@pytest.mark.timeout(5)
106136
@pytest.mark.skipif("GITHUB_REF" not in os.environ, reason="sudo password prompt")
107137
def test_unmount_watched_directory_filesystem(p):

0 commit comments

Comments
 (0)