Skip to content

The agent that grows with you

test(gateway): linger tests use a real pathlib.Path redirected at tmp_path

teknium139643567f9d5
diff --git a/tests/hermes_cli/test_gateway_linger.py b/tests/hermes_cli/test_gateway_linger.py
index 3a92894bc7c..1335d9f7a4a 100644
--- a/tests/hermes_cli/test_gateway_linger.py
+++ b/tests/hermes_cli/test_gateway_linger.py
@@ -8,34 +8,40 @@ import pytest
import hermes_cli.gateway as gateway
-def _stub_linger_file(monkeypatch, *, exists: bool) -> None:
- """Stub only the linger-file probe, leaving service-name Paths functional."""
+def _stub_linger_file(monkeypatch, tmp_path, *, exists: bool) -> None:
+ """Point the ``/var/lib/systemd/linger/<user>`` probe at a real file under tmp_path.
+
+ ``gateway.Path`` stays ``pathlib.Path`` — every other ``Path(...)`` the code under test makes
+ (``.expanduser()``, ``.resolve()`` in service-name resolution) keeps working. A ``SimpleNamespace``
+ stand-in broke as soon as the code grew a call the stub did not implement (#119786).
+ """
linger_dir = Path("/var/lib/systemd/linger")
+ fake_dir = tmp_path / "linger"
+ fake_dir.mkdir()
+ if exists:
+ (fake_dir / "testuser").write_text("")
- def fake_path(path):
- candidate = Path(path)
- if candidate.parent == linger_dir:
- return SimpleNamespace(exists=lambda: exists)
- return candidate
+ def redirected_path(path, *more):
+ candidate = Path(path, *more)
+ return fake_dir / candidate.name if candidate.parent == linger_dir else candidate
- monkeypatch.setattr(gateway, "Path", fake_path)
+ monkeypatch.setattr(gateway, "Path", redirected_path)
-def _force_service_name_path_rendering(monkeypatch) -> None:
- """Exercise the root/system-unit Path branch used by warning rendering."""
- monkeypatch.setenv("HERMES_HOME", "/tmp/hermes-linger-test-home")
+def _root_with_system_unit_pinned_home(monkeypatch, tmp_path) -> None:
+ """Take the root/system-unit branch of service-name resolution (``_bare_unit_pinned_home``), the
+ path that calls ``Path(...).expanduser()`` while rendering the restart hint."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes-home"))
monkeypatch.setattr(gateway.os, "geteuid", lambda: 0)
- monkeypatch.setattr(
- gateway, "_hermes_home_pinned_by_unit", lambda _unit_path: "/tmp/hermes-linger-system-home"
- )
+ monkeypatch.setattr(gateway, "_hermes_home_pinned_by_unit", lambda _unit_path: str(tmp_path / "system-home"))
class TestEnsureLingerEnabled:
- def test_linger_already_enabled_via_file(self, monkeypatch, capsys):
+ def test_linger_already_enabled_via_file(self, monkeypatch, capsys, tmp_path):
monkeypatch.setattr(gateway, "is_linux", lambda: True)
monkeypatch.setattr(gateway, "is_termux", lambda: False)
monkeypatch.setattr("getpass.getuser", lambda: "testuser")
- _stub_linger_file(monkeypatch, exists=True)
+ _stub_linger_file(monkeypatch, tmp_path, exists=True)
calls = []
monkeypatch.setattr(gateway.subprocess, "run", lambda *args, **kwargs: calls.append((args, kwargs)))
@@ -45,11 +51,11 @@ class TestEnsureLingerEnabled:
assert calls == []
- def test_loginctl_success_enables_linger(self, monkeypatch, capsys):
+ def test_loginctl_success_enables_linger(self, monkeypatch, capsys, tmp_path):
monkeypatch.setattr(gateway, "is_linux", lambda: True)
monkeypatch.setattr(gateway, "is_termux", lambda: False)
monkeypatch.setattr("getpass.getuser", lambda: "testuser")
- _stub_linger_file(monkeypatch, exists=False)
+ _stub_linger_file(monkeypatch, tmp_path, exists=False)
monkeypatch.setattr(gateway, "get_systemd_linger_status", lambda username=None: (False, ""))
monkeypatch.setattr("shutil.which", lambda name: "/usr/bin/loginctl")
@@ -66,12 +72,12 @@ class TestEnsureLingerEnabled:
assert run_calls == [(["loginctl", "enable-linger", "testuser"], True, True, False)]
- def test_loginctl_failure_shows_manual_guidance(self, monkeypatch, capsys):
+ def test_loginctl_failure_shows_manual_guidance(self, monkeypatch, capsys, tmp_path):
monkeypatch.setattr(gateway, "is_linux", lambda: True)
monkeypatch.setattr(gateway, "is_termux", lambda: False)
monkeypatch.setattr("getpass.getuser", lambda: "testuser")
- _stub_linger_file(monkeypatch, exists=False)
- _force_service_name_path_rendering(monkeypatch)
+ _stub_linger_file(monkeypatch, tmp_path, exists=False)
+ _root_with_system_unit_pinned_home(monkeypatch, tmp_path)
monkeypatch.setattr(gateway, "get_systemd_linger_status", lambda username=None: (False, ""))
monkeypatch.setattr("shutil.which", lambda name: "/usr/bin/loginctl")
monkeypatch.setattr(
@@ -86,10 +92,10 @@ class TestEnsureLingerEnabled:
assert "sudo loginctl enable-linger testuser" in out
assert "Permission denied" in out
- def test_system_scope_enables_target_user_without_logout_messaging(self, monkeypatch, capsys):
+ def test_system_scope_enables_target_user_without_logout_messaging(self, monkeypatch, capsys, tmp_path):
monkeypatch.setattr(gateway, "is_linux", lambda: True)
monkeypatch.setattr(gateway, "is_termux", lambda: False)
- _stub_linger_file(monkeypatch, exists=False)
+ _stub_linger_file(monkeypatch, tmp_path, exists=False)
monkeypatch.setattr(gateway, "get_systemd_linger_status", lambda username=None: (False, ""))
monkeypatch.setattr("shutil.which", lambda name: "/usr/bin/loginctl")
run_calls = []
@@ -106,11 +112,11 @@ class TestEnsureLingerEnabled:
out = capsys.readouterr().out
assert "alice" in out and "logout" not in out
- def test_system_scope_warning_uses_system_restart(self, monkeypatch, capsys):
+ def test_system_scope_warning_uses_system_restart(self, monkeypatch, capsys, tmp_path):
monkeypatch.setattr(gateway, "is_linux", lambda: True)
monkeypatch.setattr(gateway, "is_termux", lambda: False)
- _stub_linger_file(monkeypatch, exists=False)
- _force_service_name_path_rendering(monkeypatch)
+ _stub_linger_file(monkeypatch, tmp_path, exists=False)
+ _root_with_system_unit_pinned_home(monkeypatch, tmp_path)
monkeypatch.setattr(gateway, "get_systemd_linger_status", lambda username=None: (False, ""))
monkeypatch.setattr("shutil.which", lambda name: "/usr/bin/loginctl")
monkeypatch.setattr(