Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_awatch_log(mocker, caplog):
mock_log_enabled = mocker.patch('watchgod.main.logger.isEnabledFor')
mock_log_enabled.return_value = True
class FakeWatcher:
def __init__(self, path):
self.files = [1, 2, 3]
def check(self):
return {'r1'}
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1):
assert v == {'r1'}
break
assert 'DEBUG xxx time=Xms debounced=Xms files=3 changes=1 (1)' in re.sub(r'\dms', 'Xms', caplog.text)
async def test_awatch_stop():
class FakeWatcher:
def __init__(self, path):
self._results = iter([
{'r1'},
set(),
{'r2'},
])
def check(self):
return next(self._results)
stop_event = asyncio.Event()
stop_event.set()
ans = []
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event):
ans.append(v)
assert ans == []
class FakeWatcher:
def __init__(self, path):
self._results = iter([
set(),
set(),
{'r1'},
set(),
{'r2'},
set(),
])
def check(self):
return next(self._results)
ans = []
async for v in awatch('xxx', watcher_cls=FakeWatcher, debounce=5, normal_sleep=2, min_sleep=1):
ans.append(v)
if len(ans) == 2:
break
assert ans == [{'r1'}, {'r2'}]
def test_regexp_no_args(tmpdir):
mktree(tmpdir, tree)
watcher_no_args = RegExpWatcher(str(tmpdir))
changes = watcher_no_args.check()
assert changes == set()
sleep(0.01)
tmpdir.join('foo/spam.py').write('xxx')
tmpdir.join('foo/bar.txt').write('change')
tmpdir.join('foo/recursive_dir/foo.js').write('change')
assert watcher_no_args.check() == {
(Change.modified, str(tmpdir.join('foo/spam.py'))),
(Change.modified, str(tmpdir.join('foo/bar.txt'))),
(Change.added, str(tmpdir.join('foo/recursive_dir/foo.js')))
}
async def awatch_alt(*args, **kwargs):
yield {(Change.deleted, str(tmpdir.join('pages/other/whatever.png')))}
async def awatch_alt(*args, **kwargs):
yield {(Change.modified, str(tmpdir.join('pages/foobar.md')))}
yield {(Change.modified, str(tmpdir.join('pages/features/whatever.md')))}
yield {(Change.modified, str(tmpdir.join('harrier.yml')))}
yield {(Change.added, str(tmpdir.join('theme/sass/main.scss')))}
tmpdir.join('harrier.yml').write('foo: 2')
yield {(Change.modified, str(tmpdir.join('harrier.yml')))}
def test_add(tmpdir):
watcher = AllWatcher(str(tmpdir))
changes = watcher.check()
assert changes == set()
sleep(0.01)
tmpdir.join('foo.txt').write('foobar')
changes = watcher.check()
assert changes == {(Change.added, str(tmpdir.join('foo.txt')))}
def test_watch_stop():
class FakeWatcher:
def __init__(self, path):
self._results = iter([
{'r1'},
set(),
{'r2'},
])
def check(self):
return next(self._results)
stop_event = threading.Event()
stop_event.set()
ans = []
for c in watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1, stop_event=stop_event):
ans.append(c)
assert ans == []
def test_watch_watcher_kwargs(mocker):
class FakeWatcher:
def __init__(self, path, arg1=None, arg2=None):
self._results = iter([
{arg1},
set(),
{arg2},
set(),
])
def check(self):
return next(self._results)
kwargs = dict(arg1='foo', arg2='bar')
iter_ = watch('xxx', watcher_cls=FakeWatcher, watcher_kwargs=kwargs, debounce=5, normal_sleep=2, min_sleep=1)
assert next(iter_) == {kwargs['arg1']}
assert next(iter_) == {kwargs['arg2']}
def test_watch_keyboard_error():
class FakeWatcher:
def __init__(self, path):
pass
def check(self):
raise KeyboardInterrupt()
iter = watch('xxx', watcher_cls=FakeWatcher, debounce=5, min_sleep=1)
assert list(iter) == []