Files
hermes-agent/tests/hermes_cli/test_lazy_secrets_import.py
T
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

112 lines
3.7 KiB
Python

"""Regression test: hermes update must not load cryptography eagerly."""
import sys
import subprocess
import os
from pathlib import Path
def _run_isolated(code: str) -> subprocess.CompletedProcess[str]:
"""Run a Python snippet in the repo root (not tests/)."""
repo_root = Path(__file__).parent.parent.parent
return subprocess.run(
[sys.executable, "-c", code],
capture_output=True,
text=True,
cwd=str(repo_root),
env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"},
)
class TestLazySecretsImport:
"""Verify that the secrets_cli import is lazy, not eager."""
def test_secrets_parser_does_not_load_cryptography(self) -> None:
"""The secrets CLI parser should not import the secrets backends."""
result = _run_isolated(
"""
import sys
# Import main (this builds the parser, including the secrets subparser)
import hermes_cli.main
# Check if cryptography was loaded eagerly
if 'cryptography.hazmat.bindings._rust' in sys.modules:
print('FAIL: cryptography._rust loaded eagerly by main()')
sys.exit(1)
else:
print('PASS: cryptography._rust NOT loaded by main()')
sys.exit(0)
"""
)
assert result.returncode == 0, (
f"cryptography._rust was loaded eagerly by main():\n"
f"stdout: {result.stdout}\n"
f"stderr: {result.stderr}"
)
assert "PASS" in result.stdout
def test_secrets_dispatch_loads_cryptography_only_on_demand(self) -> None:
"""Running a secrets subcommand should load cryptography lazily."""
result = _run_isolated(
"""
import sys
# First verify it's NOT loaded after importing main
import hermes_cli.main
assert 'cryptography.hazmat.bindings._rust' not in sys.modules, \\
'cryptography already loaded before dispatch'
# Now simulate the secrets dispatch
# We can't easily run the actual dispatch without mocking argparse,
# but we can at least verify the import inside _dispatch_secrets works
# by checking that secrets_cli is not yet in sys.modules
assert 'hermes_cli.secrets_cli' not in sys.modules, \\
'secrets_cli already loaded before dispatch'
print('PASS: secrets_cli and cryptography not loaded until dispatch')
sys.exit(0)
"""
)
assert result.returncode == 0, (
f"Lazy import test failed:\n"
f"stdout: {result.stdout}\n"
f"stderr: {result.stderr}"
)
def test_update_check_no_cryptography(self) -> None:
"""Running hermes update --check should NOT load cryptography._rust."""
# Write a small script in the repo root so hermes_cli is importable,
# and use a filename that doesn't trigger the live-system guard.
repo_root = Path(__file__).parent.parent.parent
script = repo_root / "_test_lazy_secrets_check.py"
script.write_text(
"""
import sys
sys.argv = ['hermes', 'update', '--check']
import hermes_cli.main
from hermes_cli.update_cmd import _cmd_update_check
assert 'cryptography.hazmat.bindings._rust' not in sys.modules, \\
'cryptography._rust loaded during update path'
print('PASS: update check path is clean of cryptography')
sys.exit(0)
"""
)
try:
result = subprocess.run(
[sys.executable, script.name],
capture_output=True,
text=True,
cwd=str(repo_root),
env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"},
)
assert result.returncode == 0, (
f"cryptography._rust loaded during update check:\n"
f"stdout: {result.stdout}\n"
f"stderr: {result.stderr}"
)
finally:
script.unlink() # Clean up