fix(tools): support recursive glob filters in file searches
This commit is contained in:
1 parent
4c2c4396b0
commit
104917aaec
2 files changed
+48
No files matched your search
@@ -107,6 +107,22 @@ def _match_glob(rel_path: str, name: str, pattern: str) -> bool:
|
|||||||
if not normalized:
|
if not normalized:
|
||||||
return False
|
return False
|
||||||
if "/" in normalized or normalized.startswith("**"):
|
if "/" in normalized or normalized.startswith("**"):
|
||||||
|
pattern_parts = PurePosixPath(normalized).parts
|
||||||
|
if "**" in pattern_parts:
|
||||||
|
path_parts = PurePosixPath(rel_path).parts
|
||||||
|
# Keep relative patterns suffix-matched, as with PurePath.match.
|
||||||
|
matched = [True] * (len(path_parts) + 1)
|
||||||
|
for part in pattern_parts:
|
||||||
|
if part == "**":
|
||||||
|
# A globstar consumes zero or more complete path segments.
|
||||||
|
for index in range(1, len(matched)):
|
||||||
|
matched[index] = matched[index] or matched[index - 1]
|
||||||
|
else:
|
||||||
|
matched = [False] + [
|
||||||
|
matched[index] and fnmatch.fnmatchcase(path_part, part)
|
||||||
|
for index, path_part in enumerate(path_parts)
|
||||||
|
]
|
||||||
|
return matched[-1]
|
||||||
return PurePosixPath(rel_path).match(normalized)
|
return PurePosixPath(rel_path).match(normalized)
|
||||||
return fnmatch.fnmatch(name, normalized)
|
return fnmatch.fnmatch(name, normalized)
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,38 @@ async def test_find_files_filters_by_query_glob_and_type(tmp_path: Path) -> None
|
|||||||
assert result.splitlines() == ["src/settings_view.tsx"]
|
assert result.splitlines() == ["src/settings_view.tsx"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("tool_name", ["find_files", "grep"])
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("glob", "expected"),
|
||||||
|
[
|
||||||
|
("**/*.py", ["main.py", "src/api.py", "src/nested/deep/worker.py"]),
|
||||||
|
("src/**/*.py", ["src/api.py", "src/nested/deep/worker.py"]),
|
||||||
|
("src/**", ["src/api.py", "src/nested/deep/worker.py"]),
|
||||||
|
("src/*.py", ["src/api.py"]),
|
||||||
|
("src/**/deep/*.py", ["src/nested/deep/worker.py"]),
|
||||||
|
(r"src\**\*.py", ["src/api.py", "src/nested/deep/worker.py"]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_search_recursive_glob_matches_zero_or_more_directories(
|
||||||
|
tmp_path: Path, tool_name: str, glob: str, expected: list[str]
|
||||||
|
) -> None:
|
||||||
|
for name in ["main.py", "src/api.py", "src/nested/deep/worker.py", "notes.md"]:
|
||||||
|
path = tmp_path / name
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
path.write_text("needle\n", encoding="utf-8")
|
||||||
|
|
||||||
|
if tool_name == "find_files":
|
||||||
|
result = await FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
|
||||||
|
path=".", glob=glob,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
result = await GrepTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
|
||||||
|
pattern="needle", path=".", glob=glob, output_mode="files_with_matches",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert sorted(result.splitlines()) == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_find_files_can_include_directories(tmp_path: Path) -> None:
|
async def test_find_files_can_include_directories(tmp_path: Path) -> None:
|
||||||
(tmp_path / "src" / "settings").mkdir(parents=True)
|
(tmp_path / "src" / "settings").mkdir(parents=True)
|
||||||
|
|||||||
Reference in new issue
Block a user