Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

test(cua): run the Python process tests on Windows without a POSIX shell (#5861)

cua-agents[bot]3b15e988c942
diff --git a/libs/cua/python/tests/test_process_scrollback.py b/libs/cua/python/tests/test_process_scrollback.py
index 5f6307d91f..b194a63053 100644
--- a/libs/cua/python/tests/test_process_scrollback.py
+++ b/libs/cua/python/tests/test_process_scrollback.py
@@ -12,6 +12,7 @@ the gap.
from __future__ import annotations
import asyncio
+import sys
import cua
@@ -19,11 +20,28 @@ MIB = 1024 * 1024
SIZE = 16 * MIB
LINE = b"0123456789abcdefghijklmnopqrstuvwxyz\n"
+# Writes SIZE bytes of LINE, over and over, to stdout as raw bytes (the same
+# stream `yes | head -c SIZE` makes, without needing a POSIX shell).
+PRODUCER = (
+ "import sys\n"
+ f"line, size = {LINE!r}, {SIZE}\n"
+ "out, chunk, n = sys.stdout.buffer, line * 1024, 0\n"
+ "while n < size:\n"
+ " part = chunk[: size - n]\n"
+ " out.write(part)\n"
+ " n += len(part)\n"
+ "out.flush()\n"
+)
+
+
+def why(exit_info):
+ return (exit_info.code, exit_info.signal, exit_info.timed_out, exit_info.error)
+
def producer(scrollback_bytes=None):
return cua.SpacesdCommand(
- program="sh",
- args=["-c", f"yes 0123456789abcdefghijklmnopqrstuvwxyz | head -c {SIZE}"],
+ program=sys.executable,
+ args=["-c", PRODUCER],
scrollback_bytes=scrollback_bytes,
)
@@ -60,7 +78,7 @@ def test_large_output_read_slowly_arrives_whole(fixtures, tmp_path):
proc = await env.spawn(producer(64 * MIB))
data, gap, exit_info = await slow_read(proc)
- assert exit_info.success
+ assert exit_info.success, why(exit_info)
assert gap is None, f"offset gap {gap}"
assert len(data) == SIZE
assert data == (LINE * (SIZE // len(LINE) + 1))[:SIZE]
@@ -68,7 +86,7 @@ def test_large_output_read_slowly_arrives_whole(fixtures, tmp_path):
# Control: a 1 MiB ring loses what the slow reader did not take in time.
proc = await env.spawn(producer(MIB))
data, gap, exit_info = await slow_read(proc)
- assert exit_info.success
+ assert exit_info.success, why(exit_info)
assert gap is not None and len(data) < SIZE
# Over the server maximum is clamped, not refused.
diff --git a/libs/cua/python/tests/test_process_signal.py b/libs/cua/python/tests/test_process_signal.py
index f5657ed8c6..3527e8769c 100644
--- a/libs/cua/python/tests/test_process_signal.py
+++ b/libs/cua/python/tests/test_process_signal.py
@@ -9,24 +9,34 @@ fixture's in-process cua-spacesd core (real child processes).
from __future__ import annotations
import asyncio
+import os
import signal as _signal
+import sys
import cua
+import pytest
-async def run_sh(env, script):
- proc = await env.spawn(cua.SpacesdCommand(program="sh", args=["-c", script]))
+async def run_program(env, program, *args):
+ proc = await env.spawn(cua.SpacesdCommand(program=program, args=list(args)))
while (ev := await proc.next_event()) is not None:
if ev.kind == cua.ProcessEventKind.EXIT:
return ev.exit
raise AssertionError("no exit event")
+async def run_sh(env, script):
+ return await run_program(env, "sh", "-c", script)
+
+
def test_signal_fields_default_to_none():
info = cua.ExitInfo(code=0, signal=None, timed_out=False, error=None, success=True)
assert info.signal_number is None and info.signal_name is None
+ os.name == "nt", reason="POSIX signals: a Windows process ends with an exit code"
+)
def test_signal_deaths_carry_number_and_name(fixtures, tmp_path):
async def body():
c = cua.embedded(
@@ -54,3 +64,64 @@ def test_signal_deaths_carry_number_and_name(fixtures, tmp_path):
assert exit_info.signal_number is None and exit_info.signal_name is None
asyncio.run(asyncio.wait_for(body(), timeout=60))
+
+
+# A Windows process never dies of a POSIX signal: even when it ends abnormally
+# the native exit code says how, and every signal field stays unset. The second
+# case ends through TerminateProcess, the call behind `Process.kill()` there.
+_WINDOWS_TERMINATE_SELF = (
+ "import ctypes, ctypes.wintypes as w;"
+ "k = ctypes.windll.kernel32;"
+ "k.GetCurrentProcess.restype = w.HANDLE;"
+ "k.TerminateProcess.argtypes = [w.HANDLE, w.UINT];"
+ "k.TerminateProcess(k.GetCurrentProcess(), 7)"
+)
+
+
[email protected](os.name != "nt", reason="Windows exit codes; POSIX is covered above")
+ ("snippet", "native_code"),
+ [
+ ("import os; os._exit(3)", 3),
+ (_WINDOWS_TERMINATE_SELF, 7),
+ ],
+ ids=["os-exit", "terminate-process"],
+)
+def test_windows_abnormal_termination_reports_native_code_and_no_signal(
+ fixtures, tmp_path, snippet, native_code
+):
+ async def body():
+ c = cua.embedded(
+ state_dir=str(tmp_path / "sbx"),
+ spaces_home=str(tmp_path / "cua"),
+ )
+ env = await c.spacesd(fixtures["spaces_url"], fixtures["spaces_token"])
+
+ exit_info = await run_program(env, sys.executable, "-c", snippet)
+ assert exit_info.code == native_code, exit_info
+ assert not exit_info.success, exit_info
+ assert not exit_info.timed_out, exit_info
+ assert exit_info.signal is None, exit_info
+ assert exit_info.signal_number is None and exit_info.signal_name is None
+
+ asyncio.run(asyncio.wait_for(body(), timeout=60))
+
+
+def test_a_normal_exit_carries_its_code_and_no_signal_on_every_os(fixtures, tmp_path):
+ async def body():
+ c = cua.embedded(
+ state_dir=str(tmp_path / "sbx"),
+ spaces_home=str(tmp_path / "cua"),
+ )
+ env = await c.spacesd(fixtures["spaces_url"], fixtures["spaces_token"])
+
+ exit_info = await run_program(env, sys.executable, "-c", "raise SystemExit(3)")
+ assert exit_info.code == 3 and not exit_info.success
+ assert exit_info.signal is None
+ assert exit_info.signal_number is None and exit_info.signal_name is None
+
+ exit_info = await run_program(env, sys.executable, "-c", "")
+ assert exit_info.code == 0 and exit_info.success
+ assert exit_info.signal_number is None and exit_info.signal_name is None
+
+ asyncio.run(asyncio.wait_for(body(), timeout=60))