def test_iter(self, tmp_path): file1 = tmp_path / "file1" file1.touch() with BasicWatcher([str(tmp_path)]) as watcher: it = IterateInThread(watcher) file2 = tmp_path / "file2" file2.touch() # Check that we get notified about file2 _, event_type, path = next(it) print(event_type, path) while path != str(file2): # On MacOS, for whatever reason, we get events about # the creation of tmp_path and file1. Skip them. _, event_type, path = next(it) print(event_type, path) file1_renamed = tmp_path / "file1_renamed" file1.rename(file1_renamed) # Check for notification of renamed file. while path != str(file1_renamed): # Depending on platform, we may get more than one # event for file1. (E.g. on Linux we get both a # 'created' and a 'closed' event.) # (Also, on MacOS, for reasons not understood, # we appear to get a 'created' event for file1.) _, event_type, path = next(it) print(event_type, path) assert it.queue.empty()
def test_raises_error_if_started_twice(self, paths): with BasicWatcher(paths) as watcher: with pytest.raises(RuntimeError, match="already started"): watcher.start()
def test_perverse_usage(self, paths): # This exercises a bug which occurred when BasicWatcher was # called with repeated (failing) values in observer_classes. observer_classes = (BrokenObserver, BrokenObserver, PollingObserver) with BasicWatcher(paths, observer_classes=observer_classes) as watcher: assert isinstance(watcher.observer, BaseObserver)
def test_default_observer_is_polling(self, paths, capsys): observer_classes = (BrokenObserver, BrokenObserver) with pytest.raises(OSError, match=r"crapout"): with BasicWatcher(paths, observer_classes=observer_classes): pass assert capsys.readouterr() == ("", "")
def test_default_observer_broken(self, paths, capsys): observer_classes = (BrokenObserver, PollingObserver) with BasicWatcher(paths, observer_classes=observer_classes) as watcher: assert watcher.observer.__class__ is PollingObserver assert "crapout" in capsys.readouterr().out
def test_creates_observer(self, paths): with BasicWatcher(paths) as watcher: assert isinstance(watcher.observer, BaseObserver)