You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

from pathlib import Path
import pytest
from gcode.safety import SafetyGate
from gcode.tools.fs import make_fs_tools
def _grep(tmp_path: Path, **kwargs):
tools = {t.name: t for t in make_fs_tools(SafetyGate(), tmp_path)}
return tools["grep"].invoke(kwargs)
def test_grep_finds_line(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
(tmp_path / "src").mkdir()
(tmp_path / "src" / "app.py").write_text("def init_agent():\n return 1\n", encoding="utf-8")
out = _grep(tmp_path, query="init_agent")
assert "src" in out and "app.py" in out
assert ":1:" in out
assert "init_agent" in out
def test_grep_skips_git(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
(tmp_path / ".git").mkdir()
(tmp_path / ".git" / "HEAD").write_text("init_agent", encoding="utf-8")
(tmp_path / "ok.py").write_text("x = 1\n", encoding="utf-8")
out = _grep(tmp_path, query="init_agent")
assert "No matches" in out
assert ".git" not in out
def test_grep_rejects_outside(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
out = _grep(tmp_path, query="foo", path="..")
assert "已拒绝" in out or "Error" in out
def test_grep_glob_filters(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
(tmp_path / "a.py").write_text("needle\n", encoding="utf-8")
(tmp_path / "a.txt").write_text("needle\n", encoding="utf-8")
out = _grep(tmp_path, query="needle", glob="*.py")
assert "a.py" in out
assert "a.txt" not in out