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.

53 lines
1.7 KiB

from pathlib import Path
import pytest
from gcode.tools.fs import resolve_path
def test_relative_inside_cwd(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
(tmp_path / "a.txt").write_text("x", encoding="utf-8")
assert resolve_path("a.txt") == (tmp_path / "a.txt").resolve()
def test_rejects_parent_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
with pytest.raises(ValueError, match="已拒绝"):
resolve_path("..")
def test_rejects_absolute_outside(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
outsider = tmp_path.parent / "outside-gcode-test.txt"
with pytest.raises(ValueError, match="已拒绝"):
resolve_path(str(outsider))
def test_allows_user_home_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from gcode.config import get_home_dir
proj = tmp_path / "proj"
proj.mkdir()
monkeypatch.chdir(proj)
home = get_home_dir()
home.mkdir(parents=True, exist_ok=True)
target = home / "note.txt"
target.write_text("ok", encoding="utf-8")
assert resolve_path(str(target), proj) == target.resolve()
def test_list_dir_skips_git(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
from gcode.safety import SafetyGate
from gcode.tools.fs import make_fs_tools
monkeypatch.chdir(tmp_path)
(tmp_path / ".git").mkdir()
(tmp_path / "src").mkdir()
(tmp_path / "readme.txt").write_text("x", encoding="utf-8")
tools = {t.name: t for t in make_fs_tools(SafetyGate(), tmp_path)}
out = tools["list_dir"].invoke({"path": "."})
assert "readme.txt" in out
assert "src" in out
assert ".git" not in out