fix(file_tools): resolve bookkeeping paths against live terminal cwd
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -22,8 +23,9 @@ class TestResolvePath:
|
||||
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
|
||||
from tools.file_tools import _resolve_path
|
||||
|
||||
result = _resolve_path("/etc/hosts")
|
||||
assert result == Path("/etc/hosts")
|
||||
absolute = (tmp_path / "already-absolute.txt").resolve()
|
||||
result = _resolve_path(str(absolute))
|
||||
assert result == absolute
|
||||
|
||||
def test_falls_back_to_cwd_without_terminal_cwd(self, monkeypatch):
|
||||
"""Without TERMINAL_CWD, falls back to os.getcwd()."""
|
||||
@@ -50,3 +52,34 @@ class TestResolvePath:
|
||||
result = _resolve_path("a/../b/file.txt")
|
||||
assert ".." not in str(result)
|
||||
assert result == (tmp_path / "b" / "file.txt")
|
||||
|
||||
def test_relative_path_prefers_live_file_ops_cwd(self, monkeypatch, tmp_path):
|
||||
"""Live env.cwd must win after the terminal session changes directory."""
|
||||
start_dir = tmp_path / "start"
|
||||
live_dir = tmp_path / "worktree"
|
||||
start_dir.mkdir()
|
||||
live_dir.mkdir()
|
||||
monkeypatch.setenv("TERMINAL_CWD", str(start_dir))
|
||||
|
||||
from tools import file_tools
|
||||
|
||||
task_id = "live-cwd"
|
||||
fake_ops = SimpleNamespace(
|
||||
env=SimpleNamespace(cwd=str(live_dir)),
|
||||
cwd=str(start_dir),
|
||||
)
|
||||
|
||||
with file_tools._file_ops_lock:
|
||||
previous = file_tools._file_ops_cache.get(task_id)
|
||||
file_tools._file_ops_cache[task_id] = fake_ops
|
||||
|
||||
try:
|
||||
result = file_tools._resolve_path("nested/file.txt", task_id=task_id)
|
||||
finally:
|
||||
with file_tools._file_ops_lock:
|
||||
if previous is None:
|
||||
file_tools._file_ops_cache.pop(task_id, None)
|
||||
else:
|
||||
file_tools._file_ops_cache[task_id] = previous
|
||||
|
||||
assert result == live_dir / "nested" / "file.txt"
|
||||
|
||||
Reference in New Issue
Block a user