File size: 2,711 Bytes
8ede856 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from __future__ import annotations
import asyncio
import subprocess
from astrbot.core.computer.booters import local as local_booter
from astrbot.core.computer.booters.local import LocalShellComponent
class _FakeCompletedProcess:
def __init__(self, stdout: bytes, stderr: bytes = b"", returncode: int = 0):
self.stdout = stdout
self.stderr = stderr
self.returncode = returncode
def test_local_shell_component_decodes_utf8_output(monkeypatch):
def fake_run(*args, **kwargs):
_ = args, kwargs
return _FakeCompletedProcess(stdout="技能内容".encode())
monkeypatch.setattr(subprocess, "run", fake_run)
result = asyncio.run(LocalShellComponent().exec("dummy"))
assert result["stdout"] == "技能内容"
assert result["stderr"] == ""
assert result["exit_code"] == 0
def test_local_shell_component_prefers_utf8_before_windows_locale(
monkeypatch,
):
def fake_run(*args, **kwargs):
_ = args, kwargs
return _FakeCompletedProcess(stdout="技能内容".encode())
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(local_booter.os, "name", "nt", raising=False)
monkeypatch.setattr(
local_booter.locale,
"getpreferredencoding",
lambda _do_setlocale=False: "cp936",
)
result = asyncio.run(LocalShellComponent().exec("dummy"))
assert result["stdout"] == "技能内容"
assert result["stderr"] == ""
assert result["exit_code"] == 0
def test_local_shell_component_falls_back_to_gbk_on_windows(monkeypatch):
def fake_run(*args, **kwargs):
_ = args, kwargs
return _FakeCompletedProcess(stdout="微博热搜".encode("gbk"))
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(local_booter.os, "name", "nt", raising=False)
monkeypatch.setattr(
local_booter.locale,
"getpreferredencoding",
lambda _do_setlocale=False: "cp1252",
)
result = asyncio.run(LocalShellComponent().exec("dummy"))
assert result["stdout"] == "微博热搜"
assert result["stderr"] == ""
assert result["exit_code"] == 0
def test_local_shell_component_falls_back_to_utf8_replace(monkeypatch):
def fake_run(*args, **kwargs):
_ = args, kwargs
return _FakeCompletedProcess(stdout=b"\xffabc")
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(local_booter.os, "name", "posix", raising=False)
monkeypatch.setattr(
local_booter.locale,
"getpreferredencoding",
lambda _do_setlocale=False: "utf-8",
)
result = asyncio.run(LocalShellComponent().exec("dummy"))
assert result["stdout"] == "\ufffdabc"
|