Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion openhack/tools/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
".pytest_cache", ".tox", "eggs", "*.egg-info",
]

_TEST_PATH_DIR_NAMES = {
"test", "tests", "__tests__", "spec", "__mocks__", "fixtures",
"mocks", "__fixtures__", "e2e", "cypress", "playwright", "node_modules",
}

def _is_test_or_vendor_path(rel_path: str) -> bool:
"""Check whether a path is a test/vendor file, by path component.

Matches whole directory names and test-file naming conventions only, so
source directories that merely contain the substring "test" (e.g.
src/latest/, src/contest/) are still searched.
"""
parts = [p.lower() for p in rel_path.replace("\\", "/").split("/")]
if any(part in _TEST_PATH_DIR_NAMES for part in parts[:-1]):
return True
basename = parts[-1] if parts else ""
return (
basename.startswith("test_")
or basename.startswith("conftest.")
or basename.endswith("fixture")
or "_test." in basename
or ".test." in basename
or ".spec." in basename
)


_GREP_SOURCE_INCLUDES = [
"*.py", "*.js", "*.ts", "*.tsx", "*.jsx",
"*.rb", "*.go", "*.rs", "*.java", "*.php",
Expand Down Expand Up @@ -289,7 +315,7 @@ def grep(self, pattern: str, path: str = ".", include: Optional[str] = None) ->
rel = self._display_path(Path(fp))
except ValueError:
rel = fp
if "node_modules" in rel or "test" in rel.lower():
if _is_test_or_vendor_path(rel):
continue
matches.append({"file": rel, "line": 0, "content": ""})
if len(matches) >= 100:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_filesystem_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,25 @@ def test_no_matches(self, tmp_path):
fs = FileSystemTools(jail_dir=tmp_path)
result = fs.grep("nonexistent_string_xyz")
assert len(result["matches"]) == 0

def test_grep_keeps_paths_with_test_substring(self, tmp_path):
"""Directories like src/latest/ or src/contest/ contain the substring
"test" but are not test directories; grep must not silently drop them."""
write_file(tmp_path, "src/latest/handler.py", "eval(user_input)\n")
write_file(tmp_path, "src/contest/util.py", "eval(user_input)\n")
fs = FileSystemTools(jail_dir=tmp_path)
result = fs.grep("eval")
files = {m["file"] for m in result["matches"]}
assert any("latest" in f for f in files), files
assert any("contest" in f for f in files), files

def test_grep_still_skips_real_test_files(self, tmp_path):
write_file(tmp_path, "src/core/app.py", "eval(user_input)\n")
write_file(tmp_path, "tests/test_app.py", "eval(user_input)\n")
write_file(tmp_path, "src/app.test.js", "eval(user_input)\n")
fs = FileSystemTools(jail_dir=tmp_path)
result = fs.grep("eval")
files = {m["file"] for m in result["matches"]}
assert any("core" in f for f in files), files
assert not any("test_app" in f for f in files), files
assert not any(f.endswith(".test.js") for f in files), files