def test_watch_raise_interrupt(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(1, 'foo.txt')}], exit_code='signal') w = watch('.', raise_interrupt=True) assert next(w) == {(Change.added, 'foo.txt')} with pytest.raises(KeyboardInterrupt): next(w)
def test_watch_polling_not_env(mocker): m = mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotify()) for _ in watch('.'): pass m.assert_called_once_with(['.'], False, False, 30)
def test_watch_no_yield(mock_rust_notify: 'MockRustType', caplog): mock = mock_rust_notify([{(1, 'spam.pyc')}, {(1, 'spam.py'), (2, 'ham.txt')}]) caplog.set_level('INFO', 'watchfiles') assert next(watch('.')) == {(Change.added, 'spam.py'), (Change.modified, 'ham.txt')} assert mock.watch_count == 2 assert caplog.text == 'watchfiles.main INFO: 2 changes detected\n'
def test_simple_function(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(1, 'added.txt'), (2, 'mod.txt'), (3, 'del.txt')}]) def only_added(change: Change, path: str) -> bool: return change == Change.added assert next(watch('.', watch_filter=only_added)) == {(Change.added, 'added.txt')}
def test_wait_stop_event(tmp_path: Path, write_soon): sleep(0.05) write_soon(tmp_path / 'foo.txt') stop_event = threading.Event() for changes in watch(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()
def test_watch(tmp_path: Path, write_soon): sleep(0.05) write_soon(tmp_path / 'foo.txt') changes = None for changes in watch(tmp_path, debounce=50, step=10, watch_filter=None): break assert changes == {(Change.added, str((tmp_path / 'foo.txt')))}
def test_watch_yield_on_timeout(mock_rust_notify: 'MockRustType'): mock = mock_rust_notify(['timeout', {(1, 'spam.py')}]) change_list = [] for changes in watch('.', yield_on_timeout=True): change_list.append(changes) assert change_list == [set(), {(Change.added, 'spam.py')}] assert mock.watch_count == 2
def test_watch_dont_raise_interrupt(mock_rust_notify: 'MockRustType', caplog): caplog.set_level('WARNING', 'watchfiles') mock_rust_notify([{(1, 'foo.txt')}], exit_code='signal') w = watch('.', raise_interrupt=False) assert next(w) == {(Change.added, 'foo.txt')} with pytest.raises(StopIteration): next(w) assert caplog.text == 'watchfiles.main WARNING: KeyboardInterrupt caught, stopping watch\n'
def test_watch_polling_env(mocker): os.environ['WATCHFILES_FORCE_POLLING'] = '1' try: m = mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotify()) for _ in watch('.'): pass m.assert_called_once_with(['.'], False, True, 30) finally: del os.environ['WATCHFILES_FORCE_POLLING']
def test_watch_timeout(mock_rust_notify: 'MockRustType', caplog): mock = mock_rust_notify(['timeout', {(1, 'spam.py')}]) caplog.set_level('DEBUG', 'watchfiles') change_list = [] for changes in watch('.'): 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" )
def test_web_filter(mock_rust_notify: 'MockRustType'): # test case from docs class WebFilter(DefaultFilter): allowed_extensions = '.html', '.css', '.js' def __call__(self, change: Change, path: str) -> bool: return super().__call__(change, path) and path.endswith( self.allowed_extensions) mock_rust_notify([{(1, 'foo.txt'), (2, 'bar.html'), (3, 'spam.xlsx'), (1, '.other.js')}]) assert next(watch('.', watch_filter=WebFilter())) == { (Change.modified, 'bar.html'), (Change.added, '.other.js') }
def test_python_extensions(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(1, 'spam.txt'), (1, 'spam.md'), (1, 'foo.py')}]) f = PythonFilter(extra_extensions=('.md', )) assert next(watch('.', watch_filter=f)) == {(Change.added, 'foo.py'), (Change.added, 'spam.md')}
def test_python(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(2, 'spam.txt'), (2, 'spam.md'), (2, 'foo.py')}]) assert next(watch('.', watch_filter=PythonFilter())) == {(Change.modified, 'foo.py')}
def test_ignore_dir(mock_rust_notify: 'MockRustType'): mock_rust_notify([{(1, '.git'), (1, str(Path('.git') / 'spam')), (1, 'foo.txt')}]) assert next(watch('.')) == {(Change.added, 'foo.txt')}
def test_ignore_file(mock_rust_notify: 'MockRustType'): mock = mock_rust_notify([{(1, 'spam.pyc'), (1, 'spam.swp'), (1, 'foo.txt')}]) assert next(watch('.')) == {(Change.added, 'foo.txt')} assert mock.watch_count == 1