oclaw/tests/test_workspace_profile_tool.py
oliver 2b32d11f43 feat(tools): add grep and workspace profile, fix write path
- Add grep_tool: ripgrep with Python fallback; avoid --json with -l/-c.
- Add workspace_profile_tool: scan, languages, tests, packages, git, CI hints; tests.
- write_file: resolve relative paths via workspace root (no data/workspace prefix).
- Update path guard and local public tool tests for new write behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 23:02:52 +08:00

59 lines
2.2 KiB
Python

"""Unit tests for workspace_profile_tool helpers and handler."""
from __future__ import annotations
from pathlib import Path
import pytest
from runtime.tools.path_guard import clear_workspace_path_access_for_tests, workspace_path_access_scope
from runtime.tools.public import workspace_profile_tool as mod
def test_walkable_dir_github_vs_vendor() -> None:
assert mod._walkable_dir(".github") is True
assert mod._walkable_dir(".gitlab") is True
assert mod._walkable_dir("node_modules") is False
assert mod._walkable_dir(".cache") is False
def test_path_suggests_ci() -> None:
assert mod._path_suggests_ci(".github/workflows/ci.yml")
assert mod._path_suggests_ci("pkg/.circleci/config.yml")
assert mod._path_suggests_ci("bitbucket-pipelines.yml")
assert mod._path_suggests_ci("ci/Jenkinsfile")
assert mod._path_suggests_ci(str(Path("x") / ".woodpecker" / "ci.yaml"))
assert not mod._path_suggests_ci("src/main.py")
def test_looks_like_test_file() -> None:
assert mod._looks_like_test_file("tests/unit/test_x.py")
assert mod._looks_like_test_file(str(Path("src") / "tests" / "a.py"))
assert mod._looks_like_test_file("test_foo.py")
assert not mod._looks_like_test_file("src/main.py")
def test_detect_package_manager_nuget_csproj() -> None:
files = [
{"path": "src/App.csproj", "size": 12, "ext": ".csproj", "suffix": ".csproj"},
]
out = mod._detect_package_manager(files)
assert out["detected"] is True
assert "nuget" in out["managers"]
assert "App.csproj" in out["config_files"]
def test_workspace_profile_max_files_clamped(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("OPS_WORKSPACE_ROOT", str(tmp_path))
(tmp_path / "a.txt").write_text("hello", encoding="utf-8")
clear_workspace_path_access_for_tests()
spec = mod.workspace_profile_tool()
with workspace_path_access_scope(None, None):
r = spec.handler({"root": ".", "max_files": 0})
assert r.get("ok") is True
assert (r.get("profile") or {}).get("total_files_scanned") == 1
with workspace_path_access_scope(None, None):
r2 = spec.handler({"root": ".", "max_files": 999_999})
assert r2.get("ok") is True
assert (r2.get("profile") or {}).get("truncated") is False