Files
Maciej Lisiewski 1a55ec3398 fix(update): order pm-store PortableGit trees by version, not by name
A name sort puts git-2.56.9 ahead of git-2.56.10, so the self-heal would
prefer the older git-core when both trees are in the store.

Refs #148835

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-14 17:07:29 +01:00

168 lines
7.0 KiB
Python

"""Git trampoline recovery; branch updates use the real target-identity suite."""
import subprocess
from unittest.mock import patch
import pytest
from hermes_cli import update_cmd
@pytest.fixture(autouse=True)
def _isolate_venv_holders(monkeypatch):
"""The update flow's venv-holder guard sees the live gateway processes on
a dev machine and aborts with SystemExit 2 before reaching the branch
logic under test. Isolate it so the test exercises the intended path."""
monkeypatch.setattr("hermes_cli.update_cmd_windows._detect_venv_python_processes", list)
class TestGitTrampolineSelfHeal:
"""Proactive Git-for-Windows trampoline self-heal (#87876).
A broken bin\\git.exe / cmd\\git.exe shim (~46KB) refuses every git call
with a "BUG (fork bomb)" guard instead of re-execing the real git-core
binary. _ensure_non_trampoline_git detects this up front and swaps in a
real git binary when one can be located, so the normal git update path
survives instead of degrading to the ZIP fallback.
"""
@staticmethod
def _fake_run_healthy(command, **_kwargs):
return subprocess.CompletedProcess(
command, 0, stdout="git version 2.50.0.windows.1\n", stderr=""
)
@staticmethod
def _fake_run_trampoline(command, **_kwargs):
return subprocess.CompletedProcess(
command,
1,
stdout="",
stderr="BUG (fork bomb): tried to spawn itself, check your PATH\n",
)
@pytest.mark.platforms("windows")
def test_healthy_git_command_unchanged(self):
from hermes_cli import update_cmd
git_cmd = ["git", "-c", "windows.appendAtomically=false"]
with (
patch(
"hermes_cli.update_cmd.subprocess.run",
side_effect=self._fake_run_healthy,
),
patch("hermes_cli.update_cmd._locate_real_git") as locate,
):
result = update_cmd._ensure_non_trampoline_git(git_cmd)
assert result == git_cmd
locate.assert_not_called()
@pytest.mark.platforms("windows")
def test_trampoline_swaps_to_real_git(self, capsys):
from pathlib import Path
from hermes_cli import update_cmd
git_cmd = ["git", "-c", "windows.appendAtomically=false"]
real = Path(r"C:\Program Files\Git\mingw64\libexec\git-core\git.exe")
with (
patch(
"hermes_cli.update_cmd.subprocess.run",
side_effect=self._fake_run_trampoline,
),
patch(
"hermes_cli.update_cmd._locate_real_git", return_value=real
),
):
result = update_cmd._ensure_non_trampoline_git(git_cmd)
assert result == [str(real), "-c", "windows.appendAtomically=false"]
out = capsys.readouterr().out
assert "switching to real git" in out
@pytest.mark.platforms("windows")
def test_trampoline_no_real_git_keeps_command(self, capsys):
from hermes_cli import update_cmd
git_cmd = ["git", "-c", "windows.appendAtomically=false"]
with (
patch(
"hermes_cli.update_cmd.subprocess.run",
side_effect=self._fake_run_trampoline,
),
patch("hermes_cli.update_cmd._locate_real_git", return_value=None),
):
result = update_cmd._ensure_non_trampoline_git(git_cmd)
assert result == git_cmd
out = capsys.readouterr().out
assert "ZIP path" in out
@pytest.mark.platforms("not windows")
def test_off_windows_noop(self):
from hermes_cli import update_cmd
git_cmd = ["git"]
with patch("hermes_cli.update_cmd.subprocess.run") as run:
result = update_cmd._ensure_non_trampoline_git(git_cmd)
assert result == git_cmd
run.assert_not_called()
def test_portable_git_candidates_check_shared_root_first(self, tmp_path, monkeypatch):
# Profile-scoped layout: HERMES_HOME = <root>/profiles/foo, but the
# PortableGit tree lives under the SHARED root (monerostar review on
# #88136). The candidate list must check get_default_hermes_root()
# before the profile home.
import hermes_constants
from hermes_cli.update_cmd_git import _portable_git_candidates
root = tmp_path / "root"
profile_home = root / "profiles" / "foo"
monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: root)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: profile_home)
monkeypatch.setattr("pm.paths.store_root", lambda: root / "tools")
candidates = _portable_git_candidates()
# 2.56+ ships git-core under ucrt64/ (x64) or clangarm64/ (arm64); older trees use mingw64/.
assert candidates == [
tree / "git" / prefix / "libexec" / "git-core" / "git.exe"
for tree in (root, profile_home)
for prefix in ("ucrt64", "clangarm64", "mingw64")
]
def test_portable_git_candidates_include_the_pm_store_tree(self, tmp_path, monkeypatch):
# install.ps1 and pm stage PortableGit at <store>/git-<version>-<target>/, not <root>/git.
import hermes_constants
from hermes_cli.update_cmd_git import _portable_git_candidates
store = tmp_path / "tools"
for entry in ("git-2.53.0+3-win32-x64", "git-2.56.1+1-win32-arm64", "python-3.14-win32-x64"):
(store / entry).mkdir(parents=True)
monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: tmp_path)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
monkeypatch.setattr("pm.paths.store_root", lambda: store)
candidates = _portable_git_candidates()
assert store / "git-2.56.1+1-win32-arm64" / "clangarm64" / "libexec" / "git-core" / "git.exe" in candidates
assert store / "git-2.53.0+3-win32-x64" / "mingw64" / "libexec" / "git-core" / "git.exe" in candidates
assert not any("python-" in str(c) for c in candidates)
def test_portable_git_candidates_order_store_entries_by_version(self, tmp_path, monkeypatch):
# Newest first by number, not by name: a name sort puts 2.56.9 ahead of 2.56.10.
from types import SimpleNamespace
import hermes_constants
from hermes_cli.update_cmd_git import _portable_git_candidates
store = tmp_path / "tools"
# Listed oldest first, so a sort that ties every entry keeps the wrong order.
listed = [store / f"git-{v}-win32-x64" for v in ("2.56.9+1", "2.56.10+1", "2.56.10+2")]
monkeypatch.setattr(hermes_constants, "get_default_hermes_root", lambda: tmp_path)
monkeypatch.setattr(hermes_constants, "get_hermes_home", lambda: tmp_path)
monkeypatch.setattr("pm.paths.store_root", lambda: SimpleNamespace(glob=lambda pattern: list(listed)))
trees = [c.parents[3].name for c in _portable_git_candidates() if c.parents[3].parent == store]
assert list(dict.fromkeys(trees)) == [
"git-2.56.10+2-win32-x64", "git-2.56.10+1-win32-x64", "git-2.56.9+1-win32-x64",
]