Handle mixed command output encodings

This commit is contained in:
王鹏
2026-10-09 12:54:03 +08:00
parent 9981ad1368
commit a94193d684
3 changed files with 44 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ AutoDeploy 是一个面向 Windows 的 Spring Boot / SSM + Vue 本地自动化
- 可按顺序导入多个 SQL 文件,并启动后端、用户端和管理端。
- 服务启动前检查端口占用并终止旧进程,等待新服务端口就绪后再继续。
- 后端在独立控制台中显示实时输出,各服务同时保留运行日志。
- Maven、npm 等命令输出会自动识别 UTF-8 和 Windows 中文编码,避免日志解码导致部署任务提前中断。
## 运行环境

View File

@@ -837,13 +837,23 @@ def _run_process(
stdin=input_file,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
shell=False,
)
assert process.stdout is not None
for line in process.stdout:
on_output(line.rstrip())
for raw_line in process.stdout:
on_output(_decode_process_output(raw_line).rstrip())
return process.wait()
finally:
if input_file:
input_file.close()
def _decode_process_output(value: bytes | str) -> str:
if isinstance(value, str):
return value
for encoding in ("utf-8", "gb18030", "mbcs"):
try:
return value.decode(encoding)
except (LookupError, UnicodeDecodeError):
continue
return value.decode("utf-8", errors="replace")

View File

@@ -11,6 +11,36 @@ from deploy_tool.scanner import ProjectComponent, ScanResult
class DeployerTests(unittest.TestCase):
def test_process_output_accepts_utf8_and_gbk_without_decode_failure(self):
output = []
popen_options = {}
class FakeProcess:
stdout = iter(
[
"npm ERR! ⚠ dependency failed\n".encode("utf-8"),
"安装失败\n".encode("gb18030"),
]
)
def wait(self):
return 1
def fake_popen(*args, **kwargs):
popen_options.update(kwargs)
return FakeProcess()
with patch.object(deployer_module.subprocess, "Popen", side_effect=fake_popen):
exit_code = deployer_module._run_process(
["npm.cmd", "install"],
Path("C:/project/web"),
output.append,
)
self.assertEqual(exit_code, 1)
self.assertEqual(output, ["npm ERR! ⚠ dependency failed", "安装失败"])
self.assertNotIn("text", popen_options)
def test_frozen_app_reuses_executable_for_service_console(self):
with patch.object(deployer_module.sys, "frozen", True, create=True):
launcher = deployer_module._service_console_launcher()