-
-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathtest_emitter.py
530 lines (425 loc) · 17 KB
/
test_emitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# -*- coding: utf-8 -*-
#
# Copyright 2014 Thomas Amland <thomas.amland@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import unicode_literals
import os
import time
import pytest
import logging
from functools import partial
from . import Queue, Empty
from .shell import mkdir, touch, mv, rm
from watchdog.utils import platform
from watchdog.utils.unicode_paths import str_cls
from watchdog.events import (
FileDeletedEvent,
FileModifiedEvent,
FileCreatedEvent,
FileMovedEvent,
DirDeletedEvent,
DirModifiedEvent,
DirCreatedEvent,
DirMovedEvent
)
from watchdog.observers.api import ObservedWatch
if platform.is_linux():
from watchdog.observers.inotify import (
InotifyEmitter as Emitter,
InotifyFullEmitter,
)
elif platform.is_darwin():
pytestmark = pytest.mark.skip("FIXME: issue #546.")
from watchdog.observers.fsevents2 import FSEventsEmitter as Emitter
elif platform.is_windows():
from watchdog.observers.read_directory_changes import (
WindowsApiEmitter as Emitter
)
elif platform.is_bsd():
from watchdog.observers.kqueue import (
KqueueEmitter as Emitter
)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@pytest.fixture(autouse=True)
def setup_teardown(tmpdir):
global p, emitter, event_queue
p = partial(os.path.join, tmpdir)
event_queue = Queue()
yield
emitter.stop()
emitter.join(5)
assert not emitter.is_alive()
def start_watching(path=None, use_full_emitter=False, recursive=True):
path = p('') if path is None else path
global emitter
if platform.is_linux() and use_full_emitter:
emitter = InotifyFullEmitter(event_queue, ObservedWatch(path, recursive=recursive))
else:
emitter = Emitter(event_queue, ObservedWatch(path, recursive=recursive))
if platform.is_darwin():
# FSEvents will report old events (like create for mkdtemp in test
# setup. Waiting for a considerable time seems to 'flush' the events.
time.sleep(10)
emitter.start()
def rerun_filter(exc, *args):
time.sleep(5)
return issubclass(exc[0], Empty) and platform.is_windows()
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_create():
start_watching()
open(p('a'), 'a').close()
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('a')
assert isinstance(event, FileCreatedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert os.path.normpath(event.src_path) == os.path.normpath(p(''))
assert isinstance(event, DirModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_delete():
touch(p('a'))
start_watching()
rm(p('a'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('a')
assert isinstance(event, FileDeletedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert os.path.normpath(event.src_path) == os.path.normpath(p(''))
assert isinstance(event, DirModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_modify():
touch(p('a'))
start_watching()
touch(p('a'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('a')
assert isinstance(event, FileModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_move():
mkdir(p('dir1'))
mkdir(p('dir2'))
touch(p('dir1', 'a'))
start_watching()
mv(p('dir1', 'a'), p('dir2', 'b'))
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'a')
assert event.dest_path == p('dir2', 'b')
assert isinstance(event, FileMovedEvent)
else:
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'a')
assert isinstance(event, FileDeletedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2', 'b')
assert isinstance(event, FileCreatedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path in [p('dir1'), p('dir2')]
assert isinstance(event, DirModifiedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path in [p('dir1'), p('dir2')]
assert isinstance(event, DirModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_move_to():
mkdir(p('dir1'))
mkdir(p('dir2'))
touch(p('dir1', 'a'))
start_watching(p('dir2'))
mv(p('dir1', 'a'), p('dir2', 'b'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2', 'b')
assert isinstance(event, FileCreatedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2')
assert isinstance(event, DirModifiedEvent)
@pytest.mark.skipif(not platform.is_linux(), reason="InotifyFullEmitter only supported in Linux")
def test_move_to_full():
mkdir(p('dir1'))
mkdir(p('dir2'))
touch(p('dir1', 'a'))
start_watching(p('dir2'), use_full_emitter=True)
mv(p('dir1', 'a'), p('dir2', 'b'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, FileMovedEvent)
assert event.dest_path == p('dir2', 'b')
assert event.src_path is None # Should equal None since the path was not watched
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_move_from():
mkdir(p('dir1'))
mkdir(p('dir2'))
touch(p('dir1', 'a'))
start_watching(p('dir1'))
mv(p('dir1', 'a'), p('dir2', 'b'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, FileDeletedEvent)
assert event.src_path == p('dir1', 'a')
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1')
assert isinstance(event, DirModifiedEvent)
@pytest.mark.skipif(not platform.is_linux(), reason="InotifyFullEmitter only supported in Linux")
def test_move_from_full():
mkdir(p('dir1'))
mkdir(p('dir2'))
touch(p('dir1', 'a'))
start_watching(p('dir1'), use_full_emitter=True)
mv(p('dir1', 'a'), p('dir2', 'b'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, FileMovedEvent)
assert event.src_path == p('dir1', 'a')
assert event.dest_path is None # Should equal None since path not watched
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_separate_consecutive_moves():
mkdir(p('dir1'))
touch(p('dir1', 'a'))
touch(p('b'))
start_watching(p('dir1'))
mv(p('dir1', 'a'), p('c'))
mv(p('b'), p('dir1', 'd'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'a')
assert isinstance(event, FileDeletedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1')
assert isinstance(event, DirModifiedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'd')
assert isinstance(event, FileCreatedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1')
assert isinstance(event, DirModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_delete_self():
mkdir(p('dir1'))
start_watching(p('dir1'))
rm(p('dir1'), True)
if platform.is_darwin():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1')
assert isinstance(event, FileDeletedEvent)
@pytest.mark.skipif(platform.is_windows(),
reason="Windows create another set of events for this test")
def test_fast_subdirectory_creation_deletion():
root_dir = p('dir1')
sub_dir = p('dir1', 'subdir1')
times = 30
mkdir(root_dir)
start_watching(root_dir)
for _ in range(times):
mkdir(sub_dir)
rm(sub_dir, True)
count = {DirCreatedEvent: 0,
DirModifiedEvent: 0,
DirDeletedEvent: 0}
etype_for_dir = {DirCreatedEvent: sub_dir,
DirModifiedEvent: root_dir,
DirDeletedEvent: sub_dir}
for _ in range(times * 4):
event = event_queue.get(timeout=5)[0]
logger.debug(event)
etype = type(event)
count[etype] += 1
assert event.src_path == etype_for_dir[etype]
assert count[DirCreatedEvent] >= count[DirDeletedEvent]
assert count[DirCreatedEvent] + count[DirDeletedEvent] >= count[DirModifiedEvent]
assert count == {DirCreatedEvent: times,
DirModifiedEvent: times * 2,
DirDeletedEvent: times}
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_passing_unicode_should_give_unicode():
start_watching(str_cls(p("")))
touch(p('a'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event.src_path, str_cls)
@pytest.mark.skipif(platform.is_windows(),
reason="Windows ReadDirectoryChangesW supports only"
" unicode for paths.")
def test_passing_bytes_should_give_bytes():
start_watching(p('').encode())
touch(p('a'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event.src_path, bytes)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_recursive_on():
mkdir(p('dir1', 'dir2', 'dir3'), True)
start_watching()
touch(p('dir1', 'dir2', 'dir3', 'a'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'dir2', 'dir3', 'a')
assert isinstance(event, FileCreatedEvent)
if not platform.is_windows():
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'dir2', 'dir3')
assert isinstance(event, DirModifiedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'dir2', 'dir3', 'a')
assert isinstance(event, FileModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
def test_recursive_off():
mkdir(p('dir1'))
start_watching(recursive=False)
touch(p('dir1', 'a'))
with pytest.raises(Empty):
event_queue.get(timeout=5)
@pytest.mark.skipif(platform.is_windows(),
reason="Windows create another set of events for this test")
def test_renaming_top_level_directory():
start_watching()
mkdir(p('a'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirCreatedEvent)
assert event.src_path == p('a')
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirModifiedEvent)
assert event.src_path == p()
mkdir(p('a', 'b'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirCreatedEvent)
assert event.src_path == p('a', 'b')
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirModifiedEvent)
assert event.src_path == p('a')
mv(p('a'), p('a2'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('a')
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirModifiedEvent)
assert event.src_path == p()
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirModifiedEvent)
assert event.src_path == p()
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirMovedEvent)
assert event.src_path == p('a', 'b')
open(p('a2', 'b', 'c'), 'a').close()
# DirModifiedEvent may emitted, but sometimes after waiting time is out.
events = []
while True:
events.append(event_queue.get(timeout=5)[0])
if event_queue.empty():
break
assert all([isinstance(e, (FileCreatedEvent, FileMovedEvent, DirModifiedEvent)) for e in events])
for event in events:
if isinstance(event, FileCreatedEvent):
assert event.src_path == p('a2', 'b', 'c')
elif isinstance(event, FileMovedEvent):
assert event.dest_path == p('a2', 'b', 'c')
assert event.src_path == p('a', 'b', 'c')
elif isinstance(event, DirModifiedEvent):
assert event.src_path == p('a2', 'b')
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
@pytest.mark.skipif(not platform.is_windows(),
reason="Non-Windows create another set of events for this test")
def test_renaming_top_level_directory_on_windows():
start_watching()
mkdir(p('a'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirCreatedEvent)
assert event.src_path == p('a')
mkdir(p('a', 'b'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirCreatedEvent)
assert event.src_path == p('a', 'b')
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirCreatedEvent)
assert event.src_path == p('a', 'b')
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirModifiedEvent)
assert event.src_path == p('a')
mv(p('a'), p('a2'))
event = event_queue.get(timeout=5)[0]
assert isinstance(event, DirMovedEvent)
assert event.src_path == p('a', 'b')
open(p('a2', 'b', 'c'), 'a').close()
events = []
while True:
events.append(event_queue.get(timeout=5)[0])
if event_queue.empty():
break
assert all([isinstance(e, (FileCreatedEvent, FileMovedEvent, DirMovedEvent, DirModifiedEvent)) for e in events])
for event in events:
if isinstance(event, FileCreatedEvent):
assert event.src_path == p('a2', 'b', 'c')
elif isinstance(event, FileMovedEvent):
assert event.dest_path == p('a2', 'b', 'c')
assert event.src_path == p('a', 'b', 'c')
elif isinstance(event, DirMovedEvent):
assert event.dest_path == p('a2')
assert event.src_path == p('a')
elif isinstance(event, DirModifiedEvent):
assert event.src_path == p('a2', 'b')
@pytest.mark.skipif(platform.is_windows(),
reason="Windows create another set of events for this test")
def test_move_nested_subdirectories():
mkdir(p('dir1/dir2/dir3'), parents=True)
touch(p('dir1/dir2/dir3', 'a'))
start_watching(p(''))
mv(p('dir1/dir2'), p('dir2'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'dir2')
assert isinstance(event, DirMovedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1')
assert isinstance(event, DirModifiedEvent)
event = event_queue.get(timeout=5)[0]
assert p(event.src_path, '') == p('')
assert isinstance(event, DirModifiedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1/dir2/dir3')
assert isinstance(event, DirMovedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1/dir2/dir3', 'a')
assert isinstance(event, FileMovedEvent)
touch(p('dir2/dir3', 'a'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2/dir3', 'a')
assert isinstance(event, FileModifiedEvent)
@pytest.mark.flaky(max_runs=5, min_passes=1, rerun_filter=rerun_filter)
@pytest.mark.skipif(not platform.is_windows(),
reason="Non-Windows create another set of events for this test")
def test_move_nested_subdirectories_on_windows():
mkdir(p('dir1/dir2/dir3'), parents=True)
touch(p('dir1/dir2/dir3', 'a'))
start_watching(p(''))
mv(p('dir1/dir2'), p('dir2'))
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir1', 'dir2')
assert isinstance(event, FileDeletedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2')
assert isinstance(event, DirCreatedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2', 'dir3')
assert isinstance(event, DirCreatedEvent)
event = event_queue.get(timeout=5)[0]
assert event.src_path == p('dir2', 'dir3', 'a')
assert isinstance(event, FileCreatedEvent)
touch(p('dir2/dir3', 'a'))
events = []
while True:
events.append(event_queue.get(timeout=5)[0])
if event_queue.empty():
break
assert all([isinstance(e, (FileModifiedEvent, DirModifiedEvent)) for e in events])
for event in events:
if isinstance(event, FileModifiedEvent):
assert event.src_path == p('dir2', 'dir3', 'a')
elif isinstance(event, DirModifiedEvent):
assert event.src_path in [p('dir2'), p('dir2', 'dir3')]