async def test_await_stop_event(tmp_path: Path, write_soon): sleep(0.05) write_soon(tmp_path / 'foo.txt') stop_event = anyio.Event() async for changes in awatch(tmp_path, debounce=50, step=10, watch_filter=None, stop_event=stop_event): assert changes == {(Change.added, str((tmp_path / 'foo.txt')))} stop_event.set()
async def test_awatch_interrupt_warning(mock_rust_notify: 'MockRustType', caplog): mock_rust_notify([{(1, 'foo.txt')}]) count = 0 with pytest.warns(DeprecationWarning, match='raise_interrupt is deprecated, KeyboardInterrupt will cause this'): async for _ in awatch('.', raise_interrupt=False): count += 1 assert count == 1
async def test_awatch_unexpected_signal(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(1, 'foo.txt')}], exit_code='signal') count = 0 with pytest.raises(RuntimeError, match='watch thread unexpectedly received a signal'): async for _ in awatch('.'): count += 1 assert count == 1
async def test_awatch_yield_on_timeout(mock_rust_notify: 'MockRustType'): mock = mock_rust_notify(['timeout', {(1, 'spam.py')}]) change_list = [] async for changes in awatch('.', yield_on_timeout=True): change_list.append(changes) assert change_list == [set(), {(Change.added, 'spam.py')}] assert mock.watch_count == 2
async def test_awatch_no_yield(mock_rust_notify: 'MockRustType', caplog): mock = mock_rust_notify([{(1, 'spam.pyc')}, {(1, 'spam.py')}]) caplog.set_level('DEBUG', 'watchfiles') changes = None async for changes in awatch('.'): pass assert changes == {(Change.added, 'spam.py')} assert mock.watch_count == 2 assert caplog.text == "watchfiles.main DEBUG: 1 change detected: {(<Change.added: 1>, 'spam.py')}\n"
async def test_awatch_interrupt_raise(mocker): mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotifyRaise()) count = 0 stop_event = threading.Event() with pytest.raises(KeyboardInterrupt, match='test error'): async for _ in awatch('.', stop_event=stop_event): count += 1 # event is set because it's set while handling the KeyboardInterrupt assert stop_event.is_set() assert count == 1
async def test_awatch_timeout(mock_rust_notify: 'MockRustType', caplog): mock = mock_rust_notify(['timeout', {(1, 'spam.py')}]) caplog.set_level('DEBUG', 'watchfiles') change_list = [] async for changes in awatch('.'): change_list.append(changes) assert change_list == [{(Change.added, 'spam.py')}] assert mock.watch_count == 2 assert caplog.text == ( "watchfiles.main DEBUG: rust notify timeout, continuing\n" # noqa: Q000 "watchfiles.main DEBUG: 1 change detected: {(<Change.added: 1>, 'spam.py')}\n" )
async def start(self, app: web.Application) -> None: self._app = app self.stopper = asyncio.Event() self._awatch = awatch(self._path, stop_event=self.stopper) self._task = asyncio.create_task(self._run())
async def test_awatch(tmp_path: Path, write_soon): sleep(0.05) write_soon(tmp_path / 'foo.txt') async for changes in awatch(tmp_path, debounce=50, step=10, watch_filter=None): assert changes == {(Change.added, str((tmp_path / 'foo.txt')))} break