diff --git a/openhack/tools/filesystem.py b/openhack/tools/filesystem.py index e0ebb9a..500a16c 100644 --- a/openhack/tools/filesystem.py +++ b/openhack/tools/filesystem.py @@ -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", @@ -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: diff --git a/tests/test_filesystem_tools.py b/tests/test_filesystem_tools.py index 2ef3c8c..855d538 100644 --- a/tests/test_filesystem_tools.py +++ b/tests/test_filesystem_tools.py @@ -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