hermes-agent/tests/hermes_cli/test_dep_ensure.py
kshitijk4poor 66827f8947 chore: prune unused imports and duplicate import redefinitions
Remove unused imports (F401) and duplicate/shadowed import
redefinitions (F811) across the codebase using ruff's safe
autofixes. No behavioral changes -- imports only.

- ~1400 safe autofixes applied across 644 files (net -1072 lines)
- __init__.py re-exports preserved (excluded from F401 removal so
  public re-export surfaces stay intact)
- Re-exports that are imported or monkeypatched by tests but look
  unused in their defining module are kept with explicit # noqa:
  F401 (gateway/run.py load_dotenv; run_agent re-exports from
  agent.message_sanitization, agent.context_compressor,
  agent.retry_utils, agent.prompt_builder, agent.process_bootstrap,
  agent.codex_responses_adapter)
- Unsafe F841 (unused-variable) fixes deliberately skipped -- those
  can change behavior when the RHS has side effects
- ruff lints remain disabled in pyproject.toml (only PLW1514 is
  selected); this is a one-time cleanup, not a config change

Verification:
- python -m compileall: clean
- pytest --collect-only: all 27161 tests collect (zero import errors)
- core entry points import clean (run_agent, model_tools, cli,
  toolsets, hermes_state, batch_runner, gateway)
- static scan: every name any test imports directly from an edited
  module still resolves
2026-05-28 22:26:25 -07:00

162 lines
7.5 KiB
Python

from unittest.mock import patch
def test_ensure_dependency_skips_when_present():
"""ensure_dependency is a no-op when the dep is already available."""
from hermes_cli.dep_ensure import ensure_dependency
with patch("hermes_cli.dep_ensure.shutil") as mock_shutil:
mock_shutil.which.return_value = "/usr/bin/node"
result = ensure_dependency("node", interactive=False)
assert result is True
def test_ensure_dependency_returns_false_when_missing_noninteractive():
"""ensure_dependency returns False for missing dep in non-interactive mode."""
from hermes_cli.dep_ensure import ensure_dependency
with patch("hermes_cli.dep_ensure.shutil") as mock_shutil:
mock_shutil.which.return_value = None
with patch("hermes_cli.dep_ensure._find_install_script", return_value=(None, None)):
result = ensure_dependency("node", interactive=False)
assert result is False
def test_find_install_script_from_checkout(tmp_path):
"""_find_install_script finds scripts/install.sh in a git checkout."""
from hermes_cli.dep_ensure import _find_install_script
scripts_dir = tmp_path / "scripts"
scripts_dir.mkdir()
(scripts_dir / "install.sh").write_text("#!/bin/bash", encoding="utf-8")
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False):
path, shell = _find_install_script(package_dir=tmp_path / "hermes_cli", repo_root=tmp_path)
assert path is not None
assert path.name == "install.sh"
assert shell == "bash"
def test_find_install_script_from_wheel(tmp_path):
"""_find_install_script finds bundled install.sh in a wheel."""
from hermes_cli.dep_ensure import _find_install_script
bundled = tmp_path / "hermes_cli" / "scripts"
bundled.mkdir(parents=True)
(bundled / "install.sh").write_text("#!/bin/bash", encoding="utf-8")
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False):
path, shell = _find_install_script(package_dir=tmp_path / "hermes_cli", repo_root=tmp_path)
assert path is not None
assert path.name == "install.sh"
assert shell == "bash"
def test_find_install_script_prefers_ps1_on_windows(tmp_path):
"""On Windows, _find_install_script should find install.ps1."""
scripts_dir = tmp_path / "hermes_cli" / "scripts"
scripts_dir.mkdir(parents=True)
(scripts_dir / "install.ps1").write_text("# fake")
(scripts_dir / "install.sh").write_text("# fake")
from hermes_cli.dep_ensure import _find_install_script
with patch("hermes_cli.dep_ensure._IS_WINDOWS", True):
path, shell = _find_install_script(package_dir=tmp_path / "hermes_cli")
assert path == scripts_dir / "install.ps1"
assert shell == "powershell"
def test_find_install_script_returns_sh_on_posix(tmp_path):
"""On POSIX, _find_install_script should find install.sh."""
scripts_dir = tmp_path / "hermes_cli" / "scripts"
scripts_dir.mkdir(parents=True)
(scripts_dir / "install.ps1").write_text("# fake")
(scripts_dir / "install.sh").write_text("# fake")
from hermes_cli.dep_ensure import _find_install_script
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False):
path, shell = _find_install_script(package_dir=tmp_path / "hermes_cli")
assert path == scripts_dir / "install.sh"
assert shell == "bash"
def test_find_install_script_falls_back_to_repo_root(tmp_path):
"""When no bundled script, check repo root."""
repo_root = tmp_path / "repo"
(repo_root / "scripts").mkdir(parents=True)
(repo_root / "scripts" / "install.sh").write_text("# fake")
from hermes_cli.dep_ensure import _find_install_script
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False):
path, shell = _find_install_script(package_dir=tmp_path / "hermes_cli", repo_root=repo_root)
assert path == repo_root / "scripts" / "install.sh"
assert shell == "bash"
def test_find_install_script_returns_none_when_missing(tmp_path):
from hermes_cli.dep_ensure import _find_install_script
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False):
result = _find_install_script(package_dir=tmp_path / "x", repo_root=tmp_path / "y")
assert result == (None, None)
def test_has_system_browser_checks_windows_names():
from hermes_cli.dep_ensure import _has_system_browser
with patch("hermes_cli.dep_ensure._IS_WINDOWS", True), \
patch("hermes_cli.dep_ensure.shutil") as mock_shutil:
mock_shutil.which.side_effect = lambda name: "/fake/msedge.exe" if name == "msedge" else None
assert _has_system_browser() is True
def test_has_system_browser_checks_posix_names():
from hermes_cli.dep_ensure import _has_system_browser
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False), \
patch("hermes_cli.dep_ensure.shutil") as mock_shutil:
mock_shutil.which.return_value = None
assert _has_system_browser() is False
def test_has_hermes_agent_browser_windows_path(tmp_path):
node_dir = tmp_path / "node"
node_dir.mkdir(parents=True)
(node_dir / "agent-browser.cmd").write_text("@echo off")
from hermes_cli.dep_ensure import _has_hermes_agent_browser
with patch("hermes_cli.dep_ensure._IS_WINDOWS", True), \
patch("hermes_constants.get_hermes_home", return_value=tmp_path):
assert _has_hermes_agent_browser() is True
def test_has_hermes_agent_browser_posix_path(tmp_path):
bin_dir = tmp_path / "node" / "bin"
bin_dir.mkdir(parents=True)
(bin_dir / "agent-browser").write_text("#!/bin/sh")
from hermes_cli.dep_ensure import _has_hermes_agent_browser
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False), \
patch("hermes_constants.get_hermes_home", return_value=tmp_path):
assert _has_hermes_agent_browser() is True
def test_has_hermes_agent_browser_legacy_node_modules_path(tmp_path):
"""Legacy git-clone installs put agent-browser in $HERMES_HOME/node_modules/.bin/."""
bin_dir = tmp_path / "node_modules" / ".bin"
bin_dir.mkdir(parents=True)
(bin_dir / "agent-browser").write_text("#!/bin/sh")
from hermes_cli.dep_ensure import _has_hermes_agent_browser
with patch("hermes_cli.dep_ensure._IS_WINDOWS", False), \
patch("hermes_constants.get_hermes_home", return_value=tmp_path):
assert _has_hermes_agent_browser() is True
def test_ensure_dependency_uses_powershell_on_windows(tmp_path):
from hermes_cli.dep_ensure import ensure_dependency
scripts_dir = tmp_path / "scripts"
scripts_dir.mkdir(parents=True)
(scripts_dir / "install.ps1").write_text("# fake")
with patch("hermes_cli.dep_ensure._IS_WINDOWS", True), \
patch("hermes_cli.dep_ensure._DEP_CHECKS", {"node": lambda: False}), \
patch("hermes_cli.dep_ensure._find_install_script", return_value=(scripts_dir / "install.ps1", "powershell")), \
patch("hermes_cli.dep_ensure.shutil") as mock_shutil, \
patch("hermes_constants.get_hermes_home", return_value=tmp_path / "fakehome"), \
patch("subprocess.run") as mock_run, \
patch("sys.stdin") as mock_stdin:
mock_shutil.which.side_effect = lambda name: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" if name == "powershell" else None
mock_stdin.isatty.return_value = False
mock_run.return_value = type("R", (), {"returncode": 0})()
ensure_dependency("node", interactive=False)
cmd = mock_run.call_args[0][0]
assert "powershell" in cmd[0].lower()
assert "-Ensure" in cmd
assert cmd[cmd.index("-Ensure") + 1] == "node"
assert "-HermesHome" in cmd
assert str(tmp_path / "fakehome") in cmd