Files
hermes-agent/tests/pm/test_workspace_output_encoding.py
T
ethernet 8bf20a3bc8 test(pm): align engine fixtures with public builds
Isolate profile discovery and supply explicit engine inputs for output diagnostics. Keep destination safety checks independent of invalid-source error ordering, and verify sealed pruning with real offline wheels.

The requested canonical group passes: 98 passed, 0 failed, 4 Windows-only skips across 15 files. The full suite was not run.
2026-09-11 18:06:20 -04:00

101 lines
3.8 KiB
Python

"""Captured package-tool output must not depend on the host's ANSI code page."""
import importlib
import locale
import os
from pathlib import Path
import subprocess
import sys
import pytest
import pm.workspace as ws
from pm.package import InstallError
@pytest.fixture
def legacy_locale_child(monkeypatch):
# The canonical runner enables UTF-8 mode. Pin only subprocess's default
# encoding seam to a legacy locale; keep the real host and pipe I/O.
monkeypatch.setattr(locale, "getencoding", lambda: "cp1252")
monkeypatch.setattr(subprocess, "_text_encoding", locale.getencoding)
real_run = subprocess.run
def run(*, stdout=b"", stderr=b"", returncode=0, **kwargs):
script = (
"import sys; "
f"sys.stdout.buffer.write({stdout!r}); "
f"sys.stderr.buffer.write({stderr!r}); "
f"sys.exit({returncode})"
)
return real_run([sys.executable, "-c", script], **kwargs)
return run
@pytest.mark.parametrize("stage", ["lock", "sync"])
@pytest.mark.parametrize("stream", ["stdout", "stderr"])
@pytest.mark.parametrize("suffix", [b"", b"\xff"], ids=["utf8", "invalid-byte"])
def test_uv_failure_retains_utf8_build_diagnostic(
tmp_path, monkeypatch, legacy_locale_child, stage, stream, suffix
):
diagnostic = "🔍 cryptography: OpenSSL headers not found"
raw = diagnostic.encode("utf-8") + suffix + b"\n"
expected = diagnostic + ("�" if suffix else "")
monkeypatch.setattr(ws, "_generate_pyproject", lambda *a, **k: (tmp_path, False))
from pm.environment import PythonEnvironment
environment = PythonEnvironment(
uv=Path(sys.executable), python=Path(sys.executable),
destination=tmp_path / "venv", cache=tmp_path / "cache", env=dict(os.environ),
)
completed = []
def run_uv(cmd, **kwargs):
# Successful lock output must also be decoded before sync can run.
output = {stream: raw} if cmd[1] == stage else {"stdout": raw, "stderr": raw}
result = legacy_locale_child(
**output, returncode=17 if cmd[1] == stage else 0, **kwargs
)
completed.append(result)
return result
monkeypatch.setattr("pm.environment.subprocess.run", run_uv)
with pytest.raises(InstallError) as excinfo:
ws.lock_and_sync([], venv_dir=environment.destination, root=tmp_path,
source=tmp_path, environment=environment)
assert type(excinfo.value) is InstallError # A build error is not a resolver conflict.
assert excinfo.value.cause == f"uv {stage} exited 17: {expected}"
assert getattr(completed[-1], stream) == expected + "\n"
@pytest.mark.parametrize("install_cmd", ["ci", "install"])
@pytest.mark.parametrize("stream", ["stdout", "stderr"])
@pytest.mark.parametrize("returncode", [0, 17])
def test_node_sidecar_retains_output_and_exit_status(
tmp_path, monkeypatch, legacy_locale_child, install_cmd, stream, returncode
):
(tmp_path / "package.json").write_text("{}", encoding="utf-8")
if install_cmd == "ci":
(tmp_path / "package-lock.json").write_text("{}", encoding="utf-8")
monkeypatch.setattr(
importlib.import_module("pm.ensure"), "lazy_installs_allowed", lambda: True
)
diagnostic = "🔍 node-gyp: build toolchain unavailable"
raw = diagnostic.encode("utf-8") + b"\xff\n"
completed = []
def run_npm(cmd, **kwargs):
result = legacy_locale_child(**{stream: raw}, returncode=returncode, **kwargs)
completed.append(result)
return result
error = ws.install_node_sidecar(tmp_path, npm_bin=sys.executable, runner=run_npm)
expected = diagnostic + "�"
if returncode:
assert error == f"npm {install_cmd} exited {returncode}: {expected}"
else:
assert error is None
assert getattr(completed[-1], stream) == expected + "\n"