Files
hermes-agent/tests/scripts/install/test_install_sh_termux_python_bounds.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

221 lines
7.3 KiB
Python

"""Behavioral regression tests for Termux Python selection."""
from __future__ import annotations
import os
import shutil
import stat
import subprocess
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent.parent.parent
INSTALL_SH = REPO_ROOT / "scripts" / "install.sh"
SETUP_HERMES_SH = REPO_ROOT / "setup-hermes.sh"
def _write_executable(path: Path, content: str) -> Path:
path.write_text(content)
path.chmod(path.stat().st_mode | stat.S_IXUSR)
return path
def _write_fake_python(bin_dir: Path, name: str, version: str) -> Path:
return _write_executable(
bin_dir / name,
f"""#!{sys.executable}
import os
import sys
VERSION = {version!r}
VERSION_INFO = tuple(int(part) for part in VERSION.split('.')[:3]) + ('final', 0)
if len(sys.argv) >= 2 and sys.argv[1] == '--version':
print(f'Python {{VERSION}}')
raise SystemExit(0)
if len(sys.argv) >= 3 and sys.argv[1] == '-c':
sys.version = f'{{VERSION}} (fake)'
sys.version_info = VERSION_INFO
exec(sys.argv[2], {{'__name__': '__main__'}})
raise SystemExit(0)
if len(sys.argv) >= 3 and sys.argv[1:3] == ['-m', 'venv']:
target = sys.argv[3] if len(sys.argv) >= 4 else 'venv'
bin_path = os.path.join(target, 'bin')
os.makedirs(bin_path, exist_ok=True)
python_path = os.path.join(bin_path, 'python')
with open(python_path, 'w', encoding='utf-8') as handle:
handle.write('''#!/bin/sh\nif [ "${{1:-}}" = '-m' ] && [ "${{2:-}}" = 'pip' ]; then\n exit 0\nfi\nexit 0\n''')
os.chmod(python_path, 0o755)
raise SystemExit(0)
raise SystemExit(0)
""",
)
def _write_unsupported_explicit_pythons(bin_dir: Path, *except_names: str) -> None:
for name in ("python3.11", "python3.12", "python3.13"):
if name not in except_names and not (bin_dir / name).exists():
_write_fake_python(bin_dir, name, "3.14.6")
def _write_termux_command_stubs(bin_dir: Path) -> None:
_write_executable(
bin_dir / "uname",
"#!/bin/sh\n[ \"${1:-}\" = '-s' ] && echo Linux || echo Linux\n",
)
if not (bin_dir / "pkg").exists():
_write_executable(bin_dir / "pkg", "#!/bin/sh\nexit 0\n")
_write_executable(bin_dir / "git", "#!/bin/sh\necho 'git version 2.50.0'\n")
_write_executable(bin_dir / "node", "#!/bin/sh\necho 'v22.12.0'\n")
_write_executable(bin_dir / "npm", "#!/bin/sh\nexit 0\n")
_write_executable(bin_dir / "curl", "#!/bin/sh\nexit 0\n")
_write_executable(bin_dir / "rg", "#!/bin/sh\nexit 0\n")
def _termux_env(tmp_path: Path, bin_dir: Path) -> dict[str, str]:
prefix = tmp_path / "com.termux" / "files" / "usr"
(prefix / "bin").mkdir(parents=True)
env = os.environ.copy()
env.update({
"ANDROID_API_LEVEL": "35",
"HOME": str(tmp_path / "home"),
"HERMES_HOME": str(tmp_path / "home" / ".hermes"),
"PATH": f"{bin_dir}{os.pathsep}{env.get('PATH', os.defpath)}",
"PREFIX": str(prefix),
"TERMUX_VERSION": "0.118.0",
})
return env
def _run_install_prerequisites(tmp_path: Path) -> subprocess.CompletedProcess[str]:
bin_dir = tmp_path / "bin"
bin_dir.mkdir(exist_ok=True)
_write_termux_command_stubs(bin_dir)
env = _termux_env(tmp_path, bin_dir)
bash = shutil.which("bash") or "/bin/bash"
return subprocess.run(
[bash, str(INSTALL_SH), "--stage", "prerequisites", "--non-interactive"],
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
def _copy_setup_checkout(tmp_path: Path) -> Path:
checkout = tmp_path / "checkout"
checkout.mkdir()
shutil.copy2(SETUP_HERMES_SH, checkout / "setup-hermes.sh")
return checkout
def _run_setup(tmp_path: Path) -> subprocess.CompletedProcess[str]:
bin_dir = tmp_path / "bin"
bin_dir.mkdir(exist_ok=True)
_write_termux_command_stubs(bin_dir)
env = _termux_env(tmp_path, bin_dir)
checkout = _copy_setup_checkout(tmp_path)
bash = shutil.which("bash") or "/bin/bash"
return subprocess.run(
[bash, str(checkout / "setup-hermes.sh")],
env=env,
input="n\n",
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=False,
)
def test_install_stage_prefers_compatible_minor_over_unsupported_default(
tmp_path: Path,
) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_write_fake_python(bin_dir, "python3.11", "3.11.15")
_write_fake_python(bin_dir, "python", "3.14.6")
result = _run_install_prerequisites(tmp_path)
assert result.returncode == 0, result.stdout
assert "Python found: Python 3.11.15" in result.stdout
def test_install_stage_rejects_post_install_unsupported_default(tmp_path: Path) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_write_fake_python(bin_dir, "python", "3.14.6")
_write_unsupported_explicit_pythons(bin_dir)
result = _run_install_prerequisites(tmp_path)
assert result.returncode == 1
assert "Termux Python Python 3.14.6 is not supported" in result.stdout
assert "Hermes requires Python >=3.11,<3.14" in result.stdout
assert "pkg install tur-repo && pkg install python3.13" in result.stdout
def test_install_stage_provisions_supported_python_from_tur(tmp_path: Path) -> None:
"""When the default Termux python is too new, the installer falls back to
the Termux User Repository (TUR) and picks up a supported interpreter that
`pkg install python3.13` provides."""
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_write_fake_python(bin_dir, "python", "3.14.6")
# Shadow any host python3.11/3.12/3.13 so the candidate scan can't find a
# supported interpreter before the TUR fallback runs.
_write_unsupported_explicit_pythons(bin_dir)
# Stateful pkg stub: `pkg install -y python3.13` drops a supported fake
# interpreter into PATH, mimicking a successful TUR package install.
staged = tmp_path / "staged"
staged.mkdir()
_write_fake_python(staged, "python3.13", "3.13.7")
_write_executable(
bin_dir / "pkg",
"#!/bin/sh\n"
"for arg in \"$@\"; do\n"
f" if [ \"$arg\" = 'python3.13' ]; then cp {staged}/python3.13 {bin_dir}/python3.13; fi\n"
"done\n"
"exit 0\n",
)
result = _run_install_prerequisites(tmp_path)
assert result.returncode == 0, result.stdout
assert "Python installed from TUR: Python 3.13.7" in result.stdout
def test_setup_script_prefers_compatible_minor_over_unsupported_default(
tmp_path: Path,
) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_write_fake_python(bin_dir, "python3.11", "3.14.6")
_write_fake_python(bin_dir, "python3.12", "3.12.11")
_write_fake_python(bin_dir, "python", "3.14.6")
result = _run_setup(tmp_path)
assert result.returncode == 0, result.stdout
assert "Python 3.12.11 found" in result.stdout
assert (tmp_path / "checkout" / "venv" / "bin" / "python").exists()
def test_setup_script_rejects_unsupported_default(tmp_path: Path) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
_write_fake_python(bin_dir, "python", "3.14.6")
_write_unsupported_explicit_pythons(bin_dir)
result = _run_setup(tmp_path)
assert result.returncode == 1
assert "Termux Python Python 3.14.6 is not supported" in result.stdout
assert "Hermes requires Python >=3.11,<3.14" in result.stdout