|
| 1 | +import asyncio |
1 | 2 | import sys
|
2 | 3 | import threading
|
3 | 4 | from contextlib import contextmanager
|
@@ -115,3 +116,34 @@ async def test_awatch_no_yield(mock_rust_notify: 'MockRustType', caplog):
|
115 | 116 | assert changes == {(Change.added, 'spam.py')}
|
116 | 117 | assert mock.watch_count == 2
|
117 | 118 | assert caplog.text == "watchfiles.main DEBUG: 1 change detected: {(<Change.added: 1>, 'spam.py')}\n"
|
| 119 | + |
| 120 | + |
| 121 | +async def test_watch_directory(tmp_path: Path): |
| 122 | + stop_event = asyncio.Event() |
| 123 | + |
| 124 | + async def stop_soon(): |
| 125 | + await asyncio.sleep(0.4) |
| 126 | + stop_event.set() |
| 127 | + |
| 128 | + async def change_dir(): |
| 129 | + await asyncio.sleep(0.1) |
| 130 | + (tmp_path / 'file0').write_text('foo') |
| 131 | + await asyncio.sleep(0.1) |
| 132 | + with open(tmp_path / 'file0', 'a') as f: |
| 133 | + f.write(' bar') |
| 134 | + await asyncio.sleep(0.1) |
| 135 | + (tmp_path / 'file0').unlink() |
| 136 | + |
| 137 | + tasks = [asyncio.create_task(stop_soon()), asyncio.create_task(change_dir())] |
| 138 | + |
| 139 | + changes = [] |
| 140 | + async for change in awatch(tmp_path, stop_event=stop_event, step=1): |
| 141 | + changes.append(change) |
| 142 | + |
| 143 | + assert changes == [ |
| 144 | + {(Change.added, str(tmp_path / 'file0'))}, |
| 145 | + {(Change.modified, str(tmp_path / 'file0'))}, |
| 146 | + {(Change.deleted, str(tmp_path / 'file0'))}, |
| 147 | + ] |
| 148 | + |
| 149 | + await asyncio.gather(*tasks) |
0 commit comments