| 1 | import os |
| 2 | import re |
| 3 | from pathlib import Path |
| 4 | import select |
| 5 | import signal |
| 6 | import subprocess |
| 7 | import time |
| 8 | |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[2] |
| 11 | |
| 12 | |
| 13 | def work_directory(): |
| 14 | common = Path(subprocess.check_output( |
| 15 | ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"], |
| 16 | cwd=ROOT, text=True, |
| 17 | ).strip()).resolve() |
| 18 | if common.name != ".git": |
| 19 | raise RuntimeError("프로젝트 Git 작업 폴더를 찾을 수 없음") |
| 20 | job = os.environ.get("ZIPELO_CI_JOB", "rust-local") |
| 21 | if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}", job): |
| 22 | raise ValueError("안전한 작업명이 필요함") |
| 23 | work = common.parent / "tests/work" / job |
| 24 | if work.is_symlink() or not work.resolve().is_relative_to(common.parent / "tests/work"): |
| 25 | raise ValueError("작업 폴더 밖의 경로는 허용하지 않음") |
| 26 | return work |
| 27 | |
| 28 | |
| 29 | def binary_path(work): |
| 30 | target = Path(os.environ.get("CARGO_TARGET_DIR", work / "target")) |
| 31 | return target / "debug/zipelo-ui-memory-app" |
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | work = work_directory() |
| 36 | output = work / "screens" / str(time.time_ns()) |
| 37 | output.mkdir(mode=0o700, parents=True) |
| 38 | display = subprocess.Popen( |
| 39 | ["Xvfb", "-displayfd", "1", "-screen", "0", "1280x900x24", "-nolisten", "tcp"], |
| 40 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True, |
| 41 | ) |
| 42 | app = None |
| 43 | try: |
| 44 | if not select.select([display.stdout], [], [], 15)[0]: |
| 45 | raise RuntimeError("Xvfb 준비 시간 초과") |
| 46 | number = display.stdout.readline().decode().strip() |
| 47 | if not number.isdecimal(): |
| 48 | raise RuntimeError("Xvfb display 응답 오류") |
| 49 | env = {**os.environ, "DISPLAY": ":" + number, "WINIT_UNIX_BACKEND": "x11"} |
| 50 | env.pop("WAYLAND_DISPLAY", None) |
| 51 | with (output / "app.stdout").open("wb") as stdout, (output / "app.stderr").open("wb") as stderr: |
| 52 | app = subprocess.Popen( |
| 53 | [str(binary_path(work)), "--ui-fixture", "/synthetic/example.zip"], |
| 54 | env=env, stdout=stdout, stderr=stderr, start_new_session=True, |
| 55 | ) |
| 56 | deadline = time.monotonic() + 30 |
| 57 | while True: |
| 58 | if app.poll() is not None: |
| 59 | raise RuntimeError(f"화면 조기 종료: {app.returncode}") |
| 60 | window = subprocess.run(["xdotool", "search", "--all", "--pid", str(app.pid), "--name", "Zipelo"], env=env, timeout=5, capture_output=True) |
| 61 | if window.returncode == 0: |
| 62 | break |
| 63 | if time.monotonic() >= deadline: |
| 64 | raise RuntimeError("소유 창 표시 시간 초과") |
| 65 | time.sleep(0.1) |
| 66 | time.sleep(1) |
| 67 | subprocess.run(["import", "-window", "root", str(output / "list.png")], env=env, timeout=10, check=True) |
| 68 | subprocess.run(["xdotool", "search", "--pid", str(app.pid), "--name", "Zipelo"], env=env, timeout=10, check=True) |
| 69 | print(str(output / "list.png")) |
| 70 | finally: |
| 71 | for process in [app, display]: |
| 72 | if process is not None: |
| 73 | if process.poll() is None: |
| 74 | os.killpg(process.pid, signal.SIGTERM) |
| 75 | try: |
| 76 | process.wait(timeout=5) |
| 77 | except subprocess.TimeoutExpired: |
| 78 | os.killpg(process.pid, signal.SIGKILL) |
| 79 | process.wait(timeout=5) |
| 80 | if display.stdout: |
| 81 | display.stdout.close() |
| 82 | if display.stderr: |
| 83 | display.stderr.close() |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | main() |