Repository Public
1#!/usr/bin/env python32"""Control channel for the UI tests.34The XCUITest runner is sandboxed: it cannot stop or start the local backend nor write screenshots outside its5container. This helper, started by Scripts/test.sh OUTSIDE the sandbox, listens on 127.0.0.1 and executes exactly6three commands sent by the runner over a plain TCP connection:78 stop\\n stop the local control-api (the one started by local-backend.sh, by PID)9 start\\n start it again on the same database10 shot <name> <bytes>\\n… save a PNG screenshot (only when a screenshot directory was given)1112Usage: ui-test-control.py <port> <local-backend.sh> <database> [screenshot-dir]13"""14import os, re, socketserver, subprocess, sys1516PORT, BACKEND, DB = int(sys.argv[1]), sys.argv[2], sys.argv[3]17SHOTS = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else None1819class Handler(socketserver.StreamRequestHandler):20 def handle(self):21 line = self.rfile.readline().decode("utf-8", "replace").strip()22 if line == "stop":23 ok = subprocess.run(["/bin/bash", BACKEND, "stop"], capture_output=True).returncode == 024 elif line == "start":25 ok = subprocess.run(["/bin/bash", BACKEND, "start", DB], capture_output=True).returncode == 026 elif line.startswith("shot "):27 m = re.fullmatch(r"shot ([A-Za-z0-9._-]{1,80}) (\d{1,9})", line)28 ok = False29 if m and SHOTS:30 data = self.rfile.read(int(m.group(2)))31 os.makedirs(SHOTS, exist_ok=True)32 with open(os.path.join(SHOTS, m.group(1) + ".png"), "wb") as f:33 f.write(data)34 ok = True35 else:36 ok = False37 self.wfile.write(b"ok\n" if ok else b"error\n")3839class Server(socketserver.TCPServer):40 allow_reuse_address = True4142with Server(("127.0.0.1", PORT), Handler) as server:43 server.serve_forever()44