|
4 | 4 | from pathlib import Path |
5 | 5 | from unittest.mock import patch |
6 | 6 |
|
| 7 | +from src.exceptions import SearchError |
7 | 8 | from src.tools import search_directory |
8 | 9 |
|
9 | 10 | # --------------------------------------------------------------------------- |
@@ -322,7 +323,7 @@ def test_fuzzy_mode_finds_approximate_match(self, tmp_path: Path) -> None: |
322 | 323 |
|
323 | 324 | class TestSearchDirectoryErrorPaths: |
324 | 325 | def test_unreadable_file_is_skipped_silently(self, tmp_path: Path) -> None: |
325 | | - """search_file raising any exception causes the file to be silently skipped.""" |
| 326 | + """OSError from search_file (unreadable / binary) causes the file to be skipped.""" |
326 | 327 | (tmp_path / "good.txt").write_text("needle\n") |
327 | 328 | (tmp_path / "bad.txt").write_text("needle\n") |
328 | 329 |
|
@@ -373,3 +374,23 @@ def test_read_file_lines_failure_yields_empty_context(self, tmp_path: Path) -> N |
373 | 374 | m = result["results"][0]["matches"][0] |
374 | 375 | assert m["context_before"] == [] |
375 | 376 | assert m["context_after"] == [] |
| 377 | + |
| 378 | + def test_search_error_propagates_as_error_dict(self, tmp_path: Path) -> None: |
| 379 | + """SearchError (invalid regex, regex+fuzzy) is NOT swallowed — propagates |
| 380 | + through @handle_tool_errors and surfaces as an error dict.""" |
| 381 | + (tmp_path / "f.txt").write_text("some content\n") |
| 382 | + |
| 383 | + with patch( |
| 384 | + "src.tools.search_file", |
| 385 | + side_effect=SearchError("regex+fuzzy not allowed"), |
| 386 | + ): |
| 387 | + result = search_directory(str(tmp_path), ".*", regex=True) |
| 388 | + |
| 389 | + assert "error" in result |
| 390 | + assert "regex+fuzzy not allowed" in result["error"] |
| 391 | + |
| 392 | + def test_invalid_regex_returns_error_dict(self, tmp_path: Path) -> None: |
| 393 | + """An invalid regex pattern surfaces as an error dict, not empty results.""" |
| 394 | + (tmp_path / "f.txt").write_text("hello\n") |
| 395 | + result = search_directory(str(tmp_path), "[unclosed", regex=True) |
| 396 | + assert "error" in result |
0 commit comments