hermes-agent/tests/tools/test_resolve_path.py
alt-glitch 4b16341975 refactor(restructure): rewrite all imports for hermes_agent package
Rewrite all import statements, patch() targets, sys.modules keys,
importlib.import_module() strings, and subprocess -m references to use
hermes_agent.* paths.

Strip sys.path.insert hacks from production code (rely on editable install).
Update COMPONENT_PREFIXES for logger filtering.
Fix 3 hardcoded getLogger() calls to use __name__.
Update transport and tool registry discovery paths.
Update plugin module path strings.
Add legacy process-name patterns for gateway PID detection.
Add main() to skills_sync for console_script entry point.
Fix _get_bundled_dir() path traversal after move.

Part of #14182, #14183
2026-04-23 08:35:34 +05:30

52 lines
2.1 KiB
Python

"""Tests for _resolve_path() — TERMINAL_CWD-aware path resolution in file_tools."""
import os
from pathlib import Path
import pytest
class TestResolvePath:
"""Verify _resolve_path respects TERMINAL_CWD for worktree isolation."""
def test_relative_path_uses_terminal_cwd(self, monkeypatch, tmp_path):
"""Relative paths resolve against TERMINAL_CWD, not process CWD."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from hermes_agent.tools.files.tools import _resolve_path
result = _resolve_path("foo/bar.py")
assert result == (tmp_path / "foo" / "bar.py")
def test_absolute_path_ignores_terminal_cwd(self, monkeypatch, tmp_path):
"""Absolute paths are unaffected by TERMINAL_CWD."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from hermes_agent.tools.files.tools import _resolve_path
result = _resolve_path("/etc/hosts")
assert result == Path("/etc/hosts")
def test_falls_back_to_cwd_without_terminal_cwd(self, monkeypatch):
"""Without TERMINAL_CWD, falls back to os.getcwd()."""
monkeypatch.delenv("TERMINAL_CWD", raising=False)
from hermes_agent.tools.files.tools import _resolve_path
result = _resolve_path("some_file.txt")
assert result == Path(os.getcwd()) / "some_file.txt"
def test_tilde_expansion(self, monkeypatch, tmp_path):
"""~ is expanded before TERMINAL_CWD join (already absolute)."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from hermes_agent.tools.files.tools import _resolve_path
result = _resolve_path("~/notes.txt")
# After expanduser, ~/notes.txt becomes absolute → TERMINAL_CWD ignored
assert result == Path.home() / "notes.txt"
def test_result_is_resolved(self, monkeypatch, tmp_path):
"""Output path has no '..' components."""
monkeypatch.setenv("TERMINAL_CWD", str(tmp_path))
from hermes_agent.tools.files.tools import _resolve_path
result = _resolve_path("a/../b/file.txt")
assert ".." not in str(result)
assert result == (tmp_path / "b" / "file.txt")