`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.
116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
"""Tests for hermes_cli.gitlock — stale git lock recovery + ancestry probe.
|
|
|
|
These cover the two failure modes that produced the false "update available"
|
|
notification and the hard ``update --check`` failure after a crashed fetch on
|
|
a shallow clone:
|
|
|
|
1. A stale ``.git/shallow.lock`` makes every later ``git fetch`` fail with
|
|
"File exists" unless cleared.
|
|
2. On a shallow clone the update check compares tip SHAs, so local
|
|
cherry-picks on top of the remote tip look like "update available" even
|
|
though HEAD already contains the remote tip.
|
|
|
|
The module's safety rules are also pinned: a *young* lock or a *running git
|
|
process* must never be cleared.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import subprocess
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from hermes_cli.gitlock import (
|
|
LOCK_NAMES,
|
|
STALE_LOCK_MIN_AGE_SECONDS,
|
|
clear_stale_git_locks,
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def repo(tmp_path: Path) -> Path:
|
|
"""A real, tiny git repo with two commits (no network)."""
|
|
root = tmp_path / "repo"
|
|
root.mkdir()
|
|
subprocess.run(["git", "init", "-q"], cwd=root, check=True)
|
|
subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=root, check=True)
|
|
subprocess.run(["git", "config", "user.name", "Test"], cwd=root, check=True)
|
|
(root / "a.txt").write_text("one\n")
|
|
subprocess.run(["git", "add", "a.txt"], cwd=root, check=True)
|
|
subprocess.run(["git", "commit", "-q", "-m", "first"], cwd=root, check=True)
|
|
(root / "b.txt").write_text("two\n")
|
|
subprocess.run(["git", "add", "b.txt"], cwd=root, check=True)
|
|
subprocess.run(["git", "commit", "-q", "-m", "second"], cwd=root, check=True)
|
|
return root
|
|
|
|
|
|
def _touch(path: Path, age_seconds: float) -> None:
|
|
"""Create (or truncate) a file and backdate its mtime."""
|
|
path.touch()
|
|
old = time.time() - age_seconds
|
|
os.utime(path, (old, old))
|
|
|
|
|
|
@pytest.fixture
|
|
def no_git_running(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Pin the process guard: pretend no git process is running.
|
|
|
|
The removal tests exercise the sweep itself, not the guard. Without
|
|
this pin they are flaky on CI: the parallel per-file test runner is
|
|
almost always running a real ``git`` subprocess somewhere, the
|
|
``pgrep -x git`` probe hits, and the sweep (correctly) refuses to
|
|
remove anything — failing the assertion for reasons unrelated to the
|
|
code under test. The guard's own behavior is pinned separately in
|
|
:func:`test_clear_skips_sweep_while_git_running`.
|
|
"""
|
|
import hermes_cli.gitlock as gitlock
|
|
|
|
monkeypatch.setattr(gitlock, "_git_proc_running", lambda: False)
|
|
|
|
|
|
def test_clear_removes_stale_shallow_lock(repo: Path, no_git_running: None) -> None:
|
|
_touch(repo / ".git" / "shallow.lock", STALE_LOCK_MIN_AGE_SECONDS + 60)
|
|
removed = clear_stale_git_locks(repo)
|
|
assert str(repo / ".git" / "shallow.lock") in removed
|
|
assert not (repo / ".git" / "shallow.lock").exists()
|
|
|
|
|
|
def test_clear_removes_all_stale_lock_kinds(repo: Path, no_git_running: None) -> None:
|
|
for name in LOCK_NAMES:
|
|
_touch(repo / ".git" / name, STALE_LOCK_MIN_AGE_SECONDS + 60)
|
|
removed = clear_stale_git_locks(repo)
|
|
assert len(removed) == len(LOCK_NAMES)
|
|
for name in LOCK_NAMES:
|
|
assert not (repo / ".git" / name).exists()
|
|
|
|
|
|
def test_clear_skips_sweep_while_git_running(repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A running git process must block the sweep even for stale locks."""
|
|
import hermes_cli.gitlock as gitlock
|
|
|
|
monkeypatch.setattr(gitlock, "_git_proc_running", lambda: True)
|
|
_touch(repo / ".git" / "shallow.lock", STALE_LOCK_MIN_AGE_SECONDS + 60)
|
|
removed = clear_stale_git_locks(repo)
|
|
assert removed == []
|
|
assert (repo / ".git" / "shallow.lock").exists()
|
|
|
|
|
|
def test_clear_keeps_young_lock(repo: Path, no_git_running: None) -> None:
|
|
_touch(repo / ".git" / "shallow.lock", 1) # 1 second old — presumably live
|
|
removed = clear_stale_git_locks(repo)
|
|
assert removed == []
|
|
assert (repo / ".git" / "shallow.lock").exists()
|
|
|
|
|
|
def test_clear_noop_on_non_repo(tmp_path: Path) -> None:
|
|
bare = tmp_path / "not-a-repo"
|
|
bare.mkdir()
|
|
assert clear_stale_git_locks(bare) == []
|
|
|
|
|
|
def test_clear_noop_with_no_locks(repo: Path) -> None:
|
|
assert clear_stale_git_locks(repo) == []
|