Fix shell routes on Windows without PTY support

This commit is contained in:
pewdiepie-archdaemon
2026-06-01 13:15:40 +09:00
parent 3aa3f0fbc0
commit 24f73bf539
2 changed files with 72 additions and 6 deletions
+59 -2
View File
@@ -1,9 +1,66 @@
"""Tests for shell_routes.py — _find_line_break helper.
Imports the function directly since it has no app dependencies."""
"""Tests for shell_routes.py helpers."""
import builtins
import importlib.util
import json
import sys
from pathlib import Path
from types import SimpleNamespace
from routes.shell_routes import _find_line_break
def test_shell_routes_import_without_posix_pty_modules(monkeypatch):
"""Native Windows has no fcntl/termios; importing routes must still work."""
real_import = builtins.__import__
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
if name in {"fcntl", "pty"}:
raise ImportError(f"No module named {name!r}")
return real_import(name, globals, locals, fromlist, level)
monkeypatch.setattr(builtins, "__import__", fake_import)
cached_modules = {name: sys.modules.pop(name, None) for name in ("fcntl", "pty")}
module_path = Path(__file__).resolve().parents[1] / "routes" / "shell_routes.py"
spec = importlib.util.spec_from_file_location("_shell_routes_without_pty", module_path)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
try:
spec.loader.exec_module(module)
finally:
sys.modules.pop(spec.name, None)
for name, cached_module in cached_modules.items():
if cached_module is not None:
sys.modules[name] = cached_module
assert module.PTY_SUPPORTED is False
assert module._find_line_break(b"ok\n") == (2, 1)
async def test_generate_pty_reports_explicit_unsupported_error(monkeypatch):
"""Clients can distinguish unsupported PTY mode from process failures."""
import routes.shell_routes as shell_routes
monkeypatch.setattr(shell_routes, "PTY_SUPPORTED", False)
monkeypatch.setattr(shell_routes, "_PTY_IMPORT_ERROR", ImportError("No module named 'termios'"))
request = SimpleNamespace(is_disconnected=lambda: False)
events = [
json.loads(chunk.removeprefix("data: ").strip())
async for chunk in shell_routes._generate_pty("echo hi", 5, request)
]
assert events == [
{
"stream": "stderr",
"data": "PTY streaming is not supported on this platform: No module named 'termios'",
"error": shell_routes.PTY_UNSUPPORTED_ERROR,
},
{"exit_code": -1, "error": shell_routes.PTY_UNSUPPORTED_ERROR},
]
class TestFindLineBreak:
"""Test line-break detection in byte buffers."""