|
| 1 | +import asyncio |
1 | 2 | import platform |
2 | 3 | import unittest.mock |
3 | 4 | from pathlib import Path |
@@ -290,3 +291,157 @@ def Test3(): |
290 | 291 | selected.value = current_dir.value / ".." / "unit" / ".." |
291 | 292 | assert current_dir.value == HERE.parent |
292 | 293 | rc.close() |
| 294 | + |
| 295 | + |
| 296 | +def test_file_browser_watch_no_event_loop(tmpdir: Path): |
| 297 | + """Test that watch=True gracefully handles no running event loop.""" |
| 298 | + tmpdir = Path(tmpdir) |
| 299 | + (tmpdir / "test.txt").write_text("test") |
| 300 | + |
| 301 | + # When watch=True but no event loop is available, it should log a warning but not crash |
| 302 | + with unittest.mock.patch.object(solara.components.file_browser.logger, "warning") as mock_warning: |
| 303 | + |
| 304 | + @solara.component |
| 305 | + def Test(): |
| 306 | + return solara.FileBrowser(tmpdir, watch=True) |
| 307 | + |
| 308 | + div, rc = solara.render_fixed(Test(), handle_error=False) |
| 309 | + file_list: solara.components.file_browser.FileListWidget = div.children[1] |
| 310 | + |
| 311 | + # Verify files are still shown correctly |
| 312 | + files = {k["name"] for k in file_list.files} |
| 313 | + assert "test.txt" in files |
| 314 | + |
| 315 | + # Verify warning was logged about no event loop |
| 316 | + mock_warning.assert_called_with("No running event loop, cannot watch directory for changes") |
| 317 | + rc.close() |
| 318 | + |
| 319 | + |
| 320 | +def test_file_browser_watch_disabled_by_default(): |
| 321 | + """Test that watch is disabled by default (no warning when watchfiles not available).""" |
| 322 | + |
| 323 | + @solara.component |
| 324 | + def Test(): |
| 325 | + return solara.FileBrowser(HERE.parent) |
| 326 | + |
| 327 | + # This should work without any issues even if watchfiles is not installed |
| 328 | + div, rc = solara.render_fixed(Test(), handle_error=False) |
| 329 | + file_list: solara.components.file_browser.FileListWidget = div.children[1] |
| 330 | + assert "file_browser_test.py" in file_list |
| 331 | + rc.close() |
| 332 | + |
| 333 | + |
| 334 | +def test_file_browser_watch_no_watchfiles(tmpdir: Path): |
| 335 | + """Test that watch=True logs a warning when watchfiles is not installed.""" |
| 336 | + tmpdir = Path(tmpdir) |
| 337 | + (tmpdir / "test.txt").write_text("test") |
| 338 | + |
| 339 | + with unittest.mock.patch.object(solara.components.file_browser, "watchfiles", None): |
| 340 | + with unittest.mock.patch.object(solara.components.file_browser.logger, "warning") as mock_warning: |
| 341 | + |
| 342 | + @solara.component |
| 343 | + def Test(): |
| 344 | + return solara.FileBrowser(tmpdir, watch=True) |
| 345 | + |
| 346 | + div, rc = solara.render_fixed(Test(), handle_error=False) |
| 347 | + |
| 348 | + # Give the effect a chance to run |
| 349 | + import time |
| 350 | + |
| 351 | + time.sleep(0.1) |
| 352 | + |
| 353 | + mock_warning.assert_called_with("watchfiles not installed, cannot watch directory") |
| 354 | + rc.close() |
| 355 | + |
| 356 | + |
| 357 | +@pytest.mark.asyncio |
| 358 | +async def test_file_browser_watch_detects_new_file(tmpdir: Path): |
| 359 | + """Test that watch=True actually detects when a new file is added.""" |
| 360 | + tmpdir = Path(tmpdir) |
| 361 | + (tmpdir / "initial.txt").write_text("initial") |
| 362 | + |
| 363 | + files_changed_event = asyncio.Event() |
| 364 | + |
| 365 | + @solara.component |
| 366 | + def Test(): |
| 367 | + return solara.FileBrowser(tmpdir, watch=True) |
| 368 | + |
| 369 | + div, rc = solara.render_fixed(Test(), handle_error=False) |
| 370 | + file_list: solara.components.file_browser.FileListWidget = div.children[1] |
| 371 | + |
| 372 | + # Verify initial state |
| 373 | + initial_files = {k["name"] for k in file_list.files} |
| 374 | + assert "initial.txt" in initial_files |
| 375 | + assert "new_file.txt" not in initial_files |
| 376 | + |
| 377 | + # Set up observer to detect when files trait changes |
| 378 | + def on_files_change(change): |
| 379 | + files_changed_event.set() |
| 380 | + |
| 381 | + file_list.observe(on_files_change, "files") |
| 382 | + |
| 383 | + # Give the watcher task a moment to start |
| 384 | + await asyncio.sleep(0.1) |
| 385 | + |
| 386 | + # Create a new file - this should trigger the watcher |
| 387 | + (tmpdir / "new_file.txt").write_text("new content") |
| 388 | + |
| 389 | + # Wait for the watcher to detect the change |
| 390 | + try: |
| 391 | + await asyncio.wait_for(files_changed_event.wait(), timeout=5.0) |
| 392 | + except asyncio.TimeoutError: |
| 393 | + pytest.fail("File change was not detected within timeout") |
| 394 | + |
| 395 | + # Verify the new file is now in the list |
| 396 | + new_files = {k["name"] for k in file_list.files} |
| 397 | + assert "new_file.txt" in new_files |
| 398 | + assert "initial.txt" in new_files |
| 399 | + |
| 400 | + rc.close() |
| 401 | + |
| 402 | + |
| 403 | +@pytest.mark.asyncio |
| 404 | +async def test_file_browser_watch_detects_deleted_file(tmpdir: Path): |
| 405 | + """Test that watch=True detects when a file is deleted.""" |
| 406 | + tmpdir = Path(tmpdir) |
| 407 | + (tmpdir / "file1.txt").write_text("content1") |
| 408 | + (tmpdir / "file2.txt").write_text("content2") |
| 409 | + |
| 410 | + files_changed_event = asyncio.Event() |
| 411 | + |
| 412 | + @solara.component |
| 413 | + def Test(): |
| 414 | + return solara.FileBrowser(tmpdir, watch=True) |
| 415 | + |
| 416 | + div, rc = solara.render_fixed(Test(), handle_error=False) |
| 417 | + file_list: solara.components.file_browser.FileListWidget = div.children[1] |
| 418 | + |
| 419 | + # Verify initial state |
| 420 | + initial_files = {k["name"] for k in file_list.files} |
| 421 | + assert "file1.txt" in initial_files |
| 422 | + assert "file2.txt" in initial_files |
| 423 | + |
| 424 | + # Set up observer |
| 425 | + def on_files_change(change): |
| 426 | + files_changed_event.set() |
| 427 | + |
| 428 | + file_list.observe(on_files_change, "files") |
| 429 | + |
| 430 | + # Give the watcher task a moment to start |
| 431 | + await asyncio.sleep(0.1) |
| 432 | + |
| 433 | + # Delete a file |
| 434 | + (tmpdir / "file2.txt").unlink() |
| 435 | + |
| 436 | + # Wait for the watcher to detect the change |
| 437 | + try: |
| 438 | + await asyncio.wait_for(files_changed_event.wait(), timeout=5.0) |
| 439 | + except asyncio.TimeoutError: |
| 440 | + pytest.fail("File deletion was not detected within timeout") |
| 441 | + |
| 442 | + # Verify file2 is gone |
| 443 | + new_files = {k["name"] for k in file_list.files} |
| 444 | + assert "file1.txt" in new_files |
| 445 | + assert "file2.txt" not in new_files |
| 446 | + |
| 447 | + rc.close() |
0 commit comments