forked from malthe/macfsevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
607 lines (499 loc) · 16.1 KB
/
tests.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import unittest
class BaseTestCase(unittest.TestCase):
def setUp(self):
self.tempdir = self._make_tempdir()
def tearDown(self):
import os
os.rmdir(self.tempdir)
def _make_temporary(self, directory=None):
import os
import tempfile
if directory is None:
directory = self.tempdir
f = tempfile.NamedTemporaryFile(dir=directory)
f.flush()
path = os.path.realpath(os.path.dirname(f.name)).rstrip('/') + '/'
return f, path
def _make_tempdir(self):
import os
import tempfile
tempdir = tempfile.gettempdir()
f = tempfile.NamedTemporaryFile(dir=tempdir)
tempdir = os.path.join(tempdir, os.path.basename(f.name))
f.close()
os.mkdir(tempdir)
return tempdir
class PathObservationTestCase(BaseTestCase):
def test_single_file_added(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f.close()
time.sleep(1.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
self.assertEquals(events, [(path, 0)])
def test_multiple_files_added(self):
events = []
def callback(*args):
events.append(args)
from fsevents import Observer
observer = Observer()
from fsevents import Stream
observer.start()
# wait until activation
import time
while not observer.isAlive():
time.sleep(0.1)
time.sleep(0.1)
# two files in same directory
import os
path1 = os.path.realpath(self._make_tempdir()) + '/'
f = self._make_temporary(path1)[0]
g = self._make_temporary(path1)[0]
# one file in a separate directory
path2 = os.path.realpath(self._make_tempdir()) + '/'
h = self._make_temporary(path2)[0]
stream = Stream(callback, path1, path2)
observer.schedule(stream)
try:
del events[:]
f.close()
g.close()
h.close()
time.sleep(0.2)
self.assertEqual(sorted(events), sorted([(path1, 0), (path2, 0)]))
finally:
f.close()
g.close()
h.close()
os.rmdir(path1)
os.rmdir(path2)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
def test_single_file_added_multiple_streams(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream1 = Stream(callback, path)
stream2 = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.schedule(stream1)
observer.schedule(stream2)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
time.sleep(0.1)
del events[:]
f.close()
time.sleep(0.2)
# stop and join observer
observer.stop()
observer.unschedule(stream1)
observer.unschedule(stream2)
observer.join()
self.assertEquals(events, [(path, 0), (path, 0)])
def test_single_file_added_with_observer_unscheduled(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.start()
import time
while not observer.isAlive():
time.sleep(0.1)
observer.schedule(stream)
observer.unschedule(stream)
# add single file
del events[:]
f.close()
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.join()
self.assertEquals(events, [])
def test_single_file_added_with_observer_rescheduled(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.start()
import time
while not observer.isAlive():
time.sleep(0.1)
observer.schedule(stream)
observer.unschedule(stream)
observer.schedule(stream)
# add single file
del events[:]
f.close()
time.sleep(0.2)
# stop and join observer
observer.stop()
observer.join()
self.assertEquals(events, [(path, 0)])
def test_single_file_added_to_subdirectory(self):
events = []
def callback(*args):
events.append(args)
import os
directory = self._make_tempdir()
subdirectory = os.path.realpath(os.path.join(directory, 'subdir')) + '/'
os.mkdir(subdirectory)
import time
time.sleep(0.1)
try:
from fsevents import Stream
stream = Stream(callback, directory)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f = open(os.path.join(subdirectory, "test"), "w")
f.write("abc")
f.close()
time.sleep(0.2)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
self.assertEquals(len(events), 1)
self.assertEquals(events, [(subdirectory, 0)])
finally:
os.unlink(f.name)
os.rmdir(subdirectory)
os.rmdir(directory)
def test_single_file_added_unschedule_then_stop(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f.close()
time.sleep(0.2)
# stop and join observer
observer.unschedule(stream)
observer.stop()
observer.join()
self.assertEquals(events, [(path, 0)])
def test_start_then_watch(self):
events = []
def callback(*args):
events.append(args)
f, path = self._make_temporary()
from fsevents import Stream
stream = Stream(callback, path)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f.close()
time.sleep(0.2)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
self.assertEquals(events, [(path, 0)])
def test_start_no_watch(self):
events = []
def callback(*args):
events.append(args)
from fsevents import Observer
observer = Observer()
f, path = self._make_temporary()
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
f.close()
time.sleep(0.2)
# stop and join observer
observer.stop()
observer.join()
self.assertEquals(events, [])
class FileObservationTestCase(BaseTestCase):
def test_single_file_created(self):
events = []
def callback(event):
events.append(event)
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(0.1)
import os
f = open(os.path.join(self.tempdir, "test"), "w")
f.write("abc")
f.flush()
f.close()
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
os.unlink(f.name)
from fsevents import IN_CREATE
self.assertEquals(len(events), 1)
self.assertEquals(events[0].mask, IN_CREATE)
self.assertEquals(events[0].name, os.path.realpath(f.name))
def test_single_file_deleted(self):
events = []
def callback(event):
events.append(event)
import os
f = open(os.path.join(self.tempdir, "test"), "w")
f.write("abc")
f.flush()
f.close()
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(2.1)
os.unlink(f.name)
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
from fsevents import IN_DELETE
self.assertEquals(len(events), 1)
self.assertEquals(events[0].mask, IN_DELETE)
self.assertEquals(events[0].name, os.path.realpath(f.name))
def test_single_file_moved(self):
events = []
def callback(event):
events.append(event)
import os
f = open(os.path.join(self.tempdir, "test"), "w")
f.write("abc")
f.flush()
f.close()
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(2.1)
new = "%s.new" % f.name
os.rename(f.name, new)
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
os.unlink(new)
from fsevents import IN_MOVED_FROM
from fsevents import IN_MOVED_TO
self.assertEquals(len(events), 2)
self.assertEquals(events[0].mask, IN_MOVED_FROM)
self.assertEquals(events[0].name, os.path.realpath(f.name))
self.assertEquals(events[1].mask, IN_MOVED_TO)
self.assertEquals(events[1].name, os.path.realpath(new))
self.assertEquals(events[0].cookie, events[1].cookie)
def test_single_file_modified(self):
events = []
def callback(event):
events.append(event)
import os
f = open(os.path.join(self.tempdir, "test"), "w")
f.write("abc")
f.flush()
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(2.1)
f.write("abc")
f.flush()
f.close()
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
os.unlink(f.name)
from fsevents import IN_MODIFY
self.assertEquals(len(events), 1)
self.assertEquals(events[0].mask, IN_MODIFY)
self.assertEquals(events[0].name, os.path.realpath(f.name))
def test_single_file_created_and_modified(self):
events = []
def callback(event):
events.append(event)
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(2.1)
import os
f = open(os.path.join(self.tempdir, "test"), "w")
f.write("abc")
f.flush()
time.sleep(1.0)
f.write("def")
f.flush()
f.close()
time.sleep(0.1)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
os.unlink(f.name)
from fsevents import IN_CREATE, IN_MODIFY
self.assertEquals(len(events), 2)
self.assertEquals(events[0].mask, IN_CREATE)
self.assertEquals(events[0].name, os.path.realpath(f.name))
self.assertEquals(events[1].mask, IN_MODIFY)
self.assertEquals(events[1].name, os.path.realpath(f.name))
def test_single_directory_deleted(self):
events = []
def callback(event):
events.append(event)
import os
new1 = os.path.join(self.tempdir, "newdir1")
new2 = os.path.join(self.tempdir, "newdir2")
try:
os.mkdir(new1)
os.mkdir(new2)
import time
time.sleep(0.2)
from fsevents import Stream
stream = Stream(callback, self.tempdir, file_events=True)
from fsevents import Observer
observer = Observer()
observer.schedule(stream)
observer.start()
# add single file
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(0.1)
os.rmdir(new2)
time.sleep(1.0)
# stop and join observer
observer.stop()
observer.unschedule(stream)
observer.join()
from fsevents import IN_DELETE
self.assertEquals(len(events), 1)
self.assertEquals(events[0].mask, IN_DELETE)
self.assertEquals(events[0].name, os.path.realpath(new2))
finally:
os.rmdir(new1)
def test_existing_directories_are_not_reported(self):
import os
from fsevents import Stream, Observer
events = []
def callback(event):
events.append(event)
stream = Stream(callback, self.tempdir, file_events=True)
new1 = os.path.join(self.tempdir, "newdir1")
new2 = os.path.join(self.tempdir, "newdir2")
os.mkdir(new1)
observer = Observer()
observer.schedule(stream)
observer.start()
import time
while not observer.isAlive():
time.sleep(0.1)
del events[:]
time.sleep(1)
os.mkdir(new2)
try:
time.sleep(1.1)
observer.stop()
observer.unschedule(stream)
observer.join()
from fsevents import IN_CREATE
self.assertEquals(len(events), 1)
self.assertEquals(events[0].mask, IN_CREATE)
self.assertEquals(events[0].name, os.path.realpath(new2))
finally:
os.rmdir(new1)
os.rmdir(new2)