@@ -588,10 +588,13 @@ async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
588588
589589
590590@pytest .mark .asyncio
591+ @pytest .mark .parametrize ("stderr_text" , ["bad" , "找不到命令" ])
591592async def test_codex_exec_run_raises_on_non_zero_exit (
592- monkeypatch : pytest .MonkeyPatch ,
593+ monkeypatch : pytest .MonkeyPatch , stderr_text : str
593594) -> None :
594- process = FakeProcess (stdout_lines = [], stderr_chunks = [b"bad" ], returncode = 2 )
595+ process = FakeProcess (
596+ stdout_lines = [], stderr_chunks = [stderr_text .encode ("utf-8" )], returncode = 2
597+ )
595598
596599 async def fake_create_subprocess_exec (* args : Any , ** kwargs : Any ) -> FakeProcess :
597600 return process
@@ -601,10 +604,44 @@ async def fake_create_subprocess_exec(*args: Any, **kwargs: Any) -> FakeProcess:
601604 exec_client = exec_module .CodexExec (executable_path = "/bin/codex" )
602605 args = exec_module .CodexExecArgs (input = "hello" )
603606
604- with pytest .raises (RuntimeError , match = "exited with code 2" ) :
607+ with pytest .raises (RuntimeError ) as exc_info :
605608 async for _ in exec_client .run (args ):
606609 pass
607610
611+ assert str (exc_info .value ) == f"Codex exec exited with code 2: { stderr_text } "
612+
613+
614+ @pytest .mark .asyncio
615+ @pytest .mark .parametrize ("streamed" , [False , True ])
616+ async def test_codex_thread_preserves_exit_error_with_non_utf8_stderr (
617+ monkeypatch : pytest .MonkeyPatch , streamed : bool
618+ ) -> None :
619+ # Captured Windows CP950 diagnostic from the npm Codex shim in issue #5185.
620+ stderr = (
621+ b"'\" node\" ' \xa4 \xa3 \xac O\xa4 \xba \xb3 \xa1 \xa9 \xce \xa5 ~\xb3 \xa1 \xa9 R\xa5 O"
622+ b"\xa1 B\xa5 i\xb0 \xf5 \xa6 \xe6 \xaa \xba \xb5 {\xa6 \xa1 \xa9 \xce \xa7 \xe5 \xa6 \xb8 \xc0 \xc9 \xa1 C\r \n "
623+ )
624+ process = FakeProcess (stdout_lines = [], stderr_chunks = [stderr ], returncode = 1 )
625+
626+ async def fake_create_subprocess_exec (* args : Any , ** kwargs : Any ) -> FakeProcess :
627+ return process
628+
629+ monkeypatch .setattr (exec_module .asyncio , "create_subprocess_exec" , fake_create_subprocess_exec )
630+ thread = Codex (codex_path_override = "/bin/codex" ).start_thread ()
631+
632+ with pytest .raises (RuntimeError ) as exc_info :
633+ if streamed :
634+ result = await thread .run_streamed ("hello" )
635+ async for _ in result .events :
636+ pass
637+ else :
638+ await thread .run ("hello" )
639+
640+ message = str (exc_info .value )
641+ assert message .startswith ("Codex exec exited with code 1: '\" node\" ' " )
642+ assert "\ufffd " in message
643+ assert message .endswith ("\r \n " )
644+
608645
609646@pytest .mark .asyncio
610647async def test_codex_exec_run_raises_without_stdin (monkeypatch : pytest .MonkeyPatch ) -> None :
0 commit comments