fix(cli-apps): decode subprocess output as UTF-8

This commit is contained in:
Pei Futong
2026-07-19 16:05:26 +08:00
committed by chengyongru
parent b1232fdaf4
commit 39a952ecce
2 changed files with 27 additions and 0 deletions
+4
View File
@@ -961,6 +961,8 @@ class CliAppManager:
argv, argv,
capture_output=True, capture_output=True,
text=True, text=True,
encoding="utf-8",
errors="replace",
timeout=timeout, timeout=timeout,
) )
logger.info("CLI Apps: command exited with code {}: {}", result.returncode, command) logger.info("CLI Apps: command exited with code {}: {}", result.returncode, command)
@@ -1364,6 +1366,8 @@ Use the `run_cli_app` tool with `name="{name}"` for command execution. Do not in
cwd=str(cwd), cwd=str(cwd),
capture_output=True, capture_output=True,
text=True, text=True,
encoding="utf-8",
errors="replace",
timeout=effective_timeout, timeout=effective_timeout,
env=os.environ.copy(), env=os.environ.copy(),
) )
+23
View File
@@ -423,10 +423,14 @@ def test_run_argv_logs_command_exit_and_output(
*, *,
capture_output: bool, capture_output: bool,
text: bool, text: bool,
encoding: str,
errors: str,
timeout: int, timeout: int,
) -> subprocess.CompletedProcess[str]: ) -> subprocess.CompletedProcess[str]:
assert capture_output is True assert capture_output is True
assert text is True assert text is True
assert encoding == "utf-8"
assert errors == "replace"
assert timeout == 5 assert timeout == 5
return subprocess.CompletedProcess(argv, 0, stdout="installed ok", stderr="") return subprocess.CompletedProcess(argv, 0, stdout="installed ok", stderr="")
@@ -441,6 +445,22 @@ def test_run_argv_logs_command_exit_and_output(
assert any("installed ok" in record for record in records) assert any("installed ok" in record for record in records)
def test_run_argv_decodes_utf8_output(tmp_path: Path) -> None:
manager = _manager(tmp_path)
result = manager._run_argv(
[
sys.executable,
"-c",
"import sys; sys.stdout.buffer.write(chr(0x2713).encode('utf-8'))",
],
timeout=5,
)
assert result.returncode == 0
assert result.stdout == "\u2713"
def test_install_records_available_cli_without_reinstalling( def test_install_records_available_cli_without_reinstalling(
tmp_path: Path, tmp_path: Path,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
@@ -863,6 +883,9 @@ def test_run_installed_cli_uses_argv_without_shell(
def fake_run(argv: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]: def fake_run(argv: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]:
assert "shell" not in kwargs or kwargs["shell"] is False assert "shell" not in kwargs or kwargs["shell"] is False
assert kwargs["text"] is True
assert kwargs["encoding"] == "utf-8"
assert kwargs["errors"] == "replace"
return subprocess.CompletedProcess( return subprocess.CompletedProcess(
argv, argv,
0, 0,