mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-10-09 16:32:21 +02:00
Merge pull request #6532 from Adamact/fix/windows-pwd-import
fix(platform): resolve the service home through platform_compat so native Windows can start
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import importlib.util
|
||||
import io
|
||||
import sys
|
||||
import types
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -320,3 +321,44 @@ def test_run_ssh_command_uses_built_argv(monkeypatch):
|
||||
assert captured["kwargs"]["timeout"] == 7
|
||||
assert captured["kwargs"]["capture_output"] is True
|
||||
assert captured["kwargs"]["text"] is False
|
||||
|
||||
|
||||
class _PwdBlocker:
|
||||
"""Meta-path finder that makes ``import pwd`` fail, as it does on Windows."""
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if fullname == "pwd":
|
||||
raise ImportError("No module named 'pwd'")
|
||||
return None
|
||||
|
||||
|
||||
def test_service_home_on_windows_never_imports_pwd(monkeypatch):
|
||||
monkeypatch.setattr(platform_compat, "IS_WINDOWS", True)
|
||||
monkeypatch.delitem(sys.modules, "pwd", raising=False)
|
||||
monkeypatch.setattr(sys, "meta_path", [_PwdBlocker(), *sys.meta_path])
|
||||
|
||||
assert platform_compat.service_home() == Path.home()
|
||||
|
||||
|
||||
def test_service_home_reads_the_passwd_entry_on_posix(monkeypatch, tmp_path):
|
||||
account_home = tmp_path / "service-account"
|
||||
fake_pwd = types.SimpleNamespace(
|
||||
getpwuid=lambda uid: types.SimpleNamespace(pw_dir=str(account_home))
|
||||
)
|
||||
monkeypatch.setattr(platform_compat, "IS_WINDOWS", False)
|
||||
monkeypatch.setitem(sys.modules, "pwd", fake_pwd)
|
||||
monkeypatch.setattr(platform_compat.os, "getuid", lambda: 1000, raising=False)
|
||||
monkeypatch.setenv("HOME", str(tmp_path / "task-override"))
|
||||
|
||||
assert platform_compat.service_home() == account_home
|
||||
|
||||
|
||||
def test_service_home_falls_back_when_the_uid_has_no_passwd_entry(monkeypatch):
|
||||
def missing(uid):
|
||||
raise KeyError(uid)
|
||||
|
||||
monkeypatch.setattr(platform_compat, "IS_WINDOWS", False)
|
||||
monkeypatch.setitem(sys.modules, "pwd", types.SimpleNamespace(getpwuid=missing))
|
||||
monkeypatch.setattr(platform_compat.os, "getuid", lambda: 4242, raising=False)
|
||||
|
||||
assert platform_compat.service_home() == Path.home()
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
"""`pwd` is POSIX-only, so importing it at module scope breaks Windows startup.
|
||||
|
||||
`web_tools` is pulled in by `app.py` -> `routes.chat_routes` -> `src.agent_loop`
|
||||
-> `src.agent_tools`, so a `ModuleNotFoundError: pwd` there takes down the whole
|
||||
app at import time rather than degrading. On POSIX the missing module is
|
||||
simulated, so this runs on the hosts CI actually uses.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
from tests.helpers.import_state import clear_module, preserve_import_state
|
||||
|
||||
MODULE = "src.agent_tools.web_tools"
|
||||
|
||||
|
||||
class _PwdBlocker:
|
||||
"""Meta-path finder that makes `import pwd` fail, as it does on Windows."""
|
||||
|
||||
def find_spec(self, fullname, path=None, target=None):
|
||||
if fullname == "pwd":
|
||||
raise ImportError("No module named 'pwd'")
|
||||
return None
|
||||
|
||||
|
||||
def test_web_tools_imports_on_a_host_without_pwd():
|
||||
with preserve_import_state("pwd", MODULE):
|
||||
blocker = _PwdBlocker()
|
||||
sys.meta_path.insert(0, blocker)
|
||||
try:
|
||||
clear_module("pwd")
|
||||
clear_module(MODULE)
|
||||
module = importlib.import_module(MODULE)
|
||||
finally:
|
||||
sys.meta_path.remove(blocker)
|
||||
|
||||
assert module.PrivateBrowserTool.__module__ == MODULE
|
||||
Reference in New Issue
Block a user