diff --git a/apps/web/src/app/(docs)/docs/filesystem/watch/page.mdx b/apps/web/src/app/(docs)/docs/filesystem/watch/page.mdx index eaec2496e..7dce8d5e1 100644 --- a/apps/web/src/app/(docs)/docs/filesystem/watch/page.mdx +++ b/apps/web/src/app/(docs)/docs/filesystem/watch/page.mdx @@ -84,4 +84,4 @@ for event in events: if event.type == FilesystemEventType.Write: print(f"wrote to file {event.name}") ``` - \ No newline at end of file + diff --git a/packages/js-sdk/tests/sandbox/files/watch.test.ts b/packages/js-sdk/tests/sandbox/files/watch.test.ts index 3a4b17da8..83fe3743b 100644 --- a/packages/js-sdk/tests/sandbox/files/watch.test.ts +++ b/packages/js-sdk/tests/sandbox/files/watch.test.ts @@ -73,25 +73,32 @@ sandboxTest('watch recursive directory after nested folder addition', async ({ s await sandbox.files.remove(dirname) await sandbox.files.makeDir(dirname) - let trigger: () => void + let triggerFile: () => void + let triggerFolder: () => void - const eventPromise = new Promise((resolve) => { - trigger = resolve + const eventFilePromise = new Promise((resolve) => { + triggerFile = resolve + }) + const eventFolderPromise = new Promise((resolve) => { + triggerFolder = resolve }) const expectedFileName = `${nestedDirname}/${filename}` const handle = await sandbox.files.watchDir(dirname, async (event) => { if (event.type === FilesystemEventType.WRITE && event.name === expectedFileName) { - trigger() + triggerFile() + } else if (event.type === FilesystemEventType.CREATE && event.name === nestedDirname) { + triggerFolder() } }, { recursive: true }) await sandbox.files.makeDir(`${dirname}/${nestedDirname}`) - await sandbox.files.write(`${dirname}/${nestedDirname}/${filename}`, content) + await eventFolderPromise - await eventPromise + await sandbox.files.write(`${dirname}/${nestedDirname}/${filename}`, content) + await eventFilePromise await handle.stop() }) diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py b/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py index abb457bda..af1f56663 100644 --- a/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py +++ b/packages/python-sdk/tests/async/sandbox_async/files/test_watch.py @@ -74,22 +74,27 @@ async def test_watch_recursive_directory_after_nested_folder_addition( await async_sandbox.files.remove(dirname) await async_sandbox.files.make_dir(dirname) - event_triggered = Event() + event_triggered_file = Event() + event_triggered_folder = Event() expected_filename = f"{nested_dirname}/{filename}" def handle_event(e: FilesystemEvent): if e.type == FilesystemEventType.WRITE and e.name == expected_filename: - event_triggered.set() + event_triggered_file.set() + return + if e.type == FilesystemEventType.CREATE and e.name == nested_dirname: + event_triggered_folder.set() handle = await async_sandbox.files.watch_dir( dirname, on_event=handle_event, recursive=True ) await async_sandbox.files.make_dir(f"{dirname}/{nested_dirname}") - await async_sandbox.files.write(f"{dirname}/{nested_dirname}/{filename}", content) + await event_triggered_folder.wait() - await event_triggered.wait() + await async_sandbox.files.write(f"{dirname}/{nested_dirname}/{filename}", content) + await event_triggered_file.wait() await handle.stop() diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py index 779db6ab7..859fc4ec1 100644 --- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py +++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_watch.py @@ -87,11 +87,15 @@ def test_watch_recursive_directory_after_nested_folder_addition(sandbox: Sandbox events = handle.get_new_events() file_changed = False + folder_created = False for event in events: if event.type == FilesystemEventType.WRITE and event.name == expected_filename: file_changed = True - break + continue + if event.type == FilesystemEventType.CREATE and event.name == nested_dirname: + folder_created = True + assert folder_created assert file_changed handle.stop()