From 189214a69db867739e259bf81ca136558e5a6106 Mon Sep 17 00:00:00 2001 From: Teknium Date: Sun, 22 Mar 2026 03:52:39 -0700 Subject: [PATCH] fix(tests): replace FakePath subclass with monkeypatch for Python 3.12 compat Python 3.12 changed PosixPath.__new__ to ignore the redirected path argument, breaking the FakePath subclass pattern. Use monkeypatch on Path.exists instead. Based on PR #2261 by @dieutx, fixed NameError (bare Path not imported). --- tests/hermes_cli/test_gateway_service.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py index adc54615..0bfe1a98 100644 --- a/tests/hermes_cli/test_gateway_service.py +++ b/tests/hermes_cli/test_gateway_service.py @@ -290,21 +290,17 @@ class TestEnsureUserSystemdEnv: monkeypatch.delenv("DBUS_SESSION_BUS_ADDRESS", raising=False) monkeypatch.setattr(os, "getuid", lambda: 42) - # Patch Path so /run/user/42 resolves to our tmp dir (which exists) - from pathlib import Path as RealPath - - class FakePath(type(RealPath())): - def __new__(cls, *args): - p = str(args[0]) if args else "" - if p == "/run/user/42": - return RealPath.__new__(cls, str(tmp_path)) - return RealPath.__new__(cls, *args) - - monkeypatch.setattr(gateway_cli, "Path", FakePath) + # Patch Path.exists so /run/user/42 appears to exist. + # Using a FakePath subclass breaks on Python 3.12+ where + # PosixPath.__new__ ignores the redirected path argument. + _orig_exists = gateway_cli.Path.exists + monkeypatch.setattr( + gateway_cli.Path, "exists", + lambda self: True if str(self) == "/run/user/42" else _orig_exists(self), + ) gateway_cli._ensure_user_systemd_env() - # Function sets the canonical string, not the fake path assert os.environ.get("XDG_RUNTIME_DIR") == "/run/user/42" def test_sets_dbus_address_when_bus_socket_exists(self, tmp_path, monkeypatch):