Skip to content

Commit

Permalink
Test appending to file
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Apr 8, 2022
1 parent c35fa79 commit 8919034
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/test_watch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import sys
import threading
from contextlib import contextmanager
Expand Down Expand Up @@ -115,3 +116,34 @@ async def test_awatch_no_yield(mock_rust_notify: 'MockRustType', caplog):
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_watch_directory(tmp_path: Path):
stop_event = asyncio.Event()

async def stop_soon():
await asyncio.sleep(0.4)
stop_event.set()

async def change_dir():
await asyncio.sleep(0.1)
(tmp_path / "file0").write_text("foo")
await asyncio.sleep(0.1)
with open(tmp_path / "file0", "a") as f:
f.write(" bar")
await asyncio.sleep(0.1)
(tmp_path / "file0").unlink()

tasks = [asyncio.create_task(stop_soon()), asyncio.create_task(change_dir())]

changes = []
async for change in awatch(tmp_path, stop_event=stop_event, step=1):
changes.append(change)

assert changes == [
{(Change.added, str(tmp_path / "file0"))},
{(Change.modified, str(tmp_path / "file0"))},
{(Change.deleted, str(tmp_path / "file0"))},
]

await asyncio.gather(*tasks)

0 comments on commit 8919034

Please sign in to comment.