Skip to content

Commit

Permalink
Test for recursive folder creation, add newline after code to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dobrac committed Dec 19, 2024
1 parent 262d80e commit f847483
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/app/(docs)/docs/filesystem/watch/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ for event in events:
if event.type == FilesystemEventType.Write:
print(f"wrote to file {event.name}")
```
</CodeGroup>
</CodeGroup>
19 changes: 13 additions & 6 deletions packages/js-sdk/tests/sandbox/files/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve) => {
trigger = resolve
const eventFilePromise = new Promise<void>((resolve) => {
triggerFile = resolve
})
const eventFolderPromise = new Promise<void>((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()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f847483

Please sign in to comment.