| 1 | """현재 사용자 설정을 보존하고 실제 패키지 설치와 창 실행을 검증함.""" |
| 2 | |
| 3 | import argparse |
| 4 | from contextlib import contextmanager |
| 5 | import json |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import select |
| 9 | import subprocess |
| 10 | import time |
| 11 | import zipfile |
| 12 | |
| 13 | ROOT = Path(__file__).resolve().parents[2] |
| 14 | MIMES = ("application/zip", "application/x-7z-compressed", "application/x-compressed-tar") |
| 15 | CONFIGS = (Path.home() / ".config", Path.home() / ".local/share/applications") |
| 16 | |
| 17 | |
| 18 | def configs(): |
| 19 | return {p: p.read_bytes() for directory in CONFIGS for p in directory.glob("*mimeapps.list")} |
| 20 | |
| 21 | |
| 22 | def restore_configs(before, after): |
| 23 | current = configs() |
| 24 | failures = [] |
| 25 | for path in set(before) | set(after): |
| 26 | try: |
| 27 | if current.get(path) != after.get(path): |
| 28 | raise RuntimeError(f"다른 MIME 설정 변경이 있어 복원을 보류함: {path}") |
| 29 | if path in before: |
| 30 | path.write_bytes(before[path]) |
| 31 | elif path.exists(): |
| 32 | path.unlink() |
| 33 | except Exception as error: |
| 34 | failures.append(error) |
| 35 | if failures: |
| 36 | raise ExceptionGroup("MIME 설정 복원 실패", failures) |
| 37 | if configs() != before: |
| 38 | raise RuntimeError("기존 MIME 설정 복원 실패") |
| 39 | |
| 40 | |
| 41 | @contextmanager |
| 42 | def installed_package(package): |
| 43 | before = configs() |
| 44 | after = None |
| 45 | failures = [] |
| 46 | try: |
| 47 | try: |
| 48 | subprocess.run(["sudo", "-n", "dpkg", "-i", str(package)], check=True) |
| 49 | finally: |
| 50 | after = configs() |
| 51 | yield |
| 52 | except BaseException as error: |
| 53 | failures.append(error) |
| 54 | finally: |
| 55 | try: |
| 56 | subprocess.run(["sudo", "-n", "dpkg", "-P", "zipelo"], check=True) |
| 57 | except BaseException as error: |
| 58 | failures.append(error) |
| 59 | try: |
| 60 | if after is not None: |
| 61 | restore_configs(before, after) |
| 62 | except BaseException as error: |
| 63 | failures.append(error) |
| 64 | if failures: |
| 65 | raise BaseExceptionGroup("패키지 시험 또는 정리 실패", failures) |
| 66 | |
| 67 | |
| 68 | def main(): |
| 69 | parser = argparse.ArgumentParser(description=__doc__) |
| 70 | parser.add_argument("package", type=Path) |
| 71 | args = parser.parse_args() |
| 72 | package = args.package.resolve(strict=True) |
| 73 | runs = ROOT / "tests/release-packages/runs/smoke" |
| 74 | runs.mkdir(parents=True, exist_ok=False) |
| 75 | assert subprocess.run(["dpkg-query", "-W", "-f=${db:Status-Status}", "zipelo"], |
| 76 | capture_output=True).stdout != b"installed", "기존 설치를 보존해야 함" |
| 77 | for name in ("/usr/lib/zipelo", "/usr/bin/zipelo", "/usr/share/applications/zipelo.desktop"): |
| 78 | assert not Path(name).exists(), "기존 파일이 있어 설치를 중단함" |
| 79 | app = None |
| 80 | display = None |
| 81 | with installed_package(package): |
| 82 | try: |
| 83 | for mime in MIMES: |
| 84 | result = subprocess.check_output(["xdg-mime", "query", "default", mime], text=True).strip() |
| 85 | assert result == "zipelo.desktop", f"연결 실패: {mime}" |
| 86 | info = json.loads(Path("/usr/lib/zipelo/BUILD-INFO.json").read_text()) |
| 87 | assert info["profile"] == "release" |
| 88 | archive = runs / "sample archive.zip" |
| 89 | with zipfile.ZipFile(archive, "w") as output: |
| 90 | output.writestr("example.txt", "Zipelo package verification") |
| 91 | display = subprocess.Popen( |
| 92 | ["Xvfb", "-displayfd", "1", "-screen", "0", "1280x900x24", "-nolisten", "tcp"], |
| 93 | stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) |
| 94 | assert select.select([display.stdout], [], [], 15)[0], "Xvfb 준비 실패" |
| 95 | number = display.stdout.readline().decode().strip() |
| 96 | assert number.isdecimal() |
| 97 | env = {**os.environ, "DISPLAY": ":" + number, "WINIT_UNIX_BACKEND": "x11", "XDG_SESSION_TYPE": "x11"} |
| 98 | env.pop("WAYLAND_DISPLAY", None) |
| 99 | with (runs / "app.stdout").open("wb") as stdout, (runs / "app.stderr").open("wb") as stderr: |
| 100 | app = subprocess.Popen(["/usr/bin/zipelo", str(archive)], env=env, stdout=stdout, stderr=stderr) |
| 101 | deadline = time.monotonic() + 30 |
| 102 | while True: |
| 103 | assert app.poll() is None, "설치된 앱이 조기에 종료됨" |
| 104 | window = subprocess.run(["xdotool", "search", "--all", "--pid", str(app.pid), "--name", "Zipelo"], |
| 105 | env=env, capture_output=True, text=True) |
| 106 | if window.returncode == 0: |
| 107 | break |
| 108 | assert time.monotonic() < deadline, "앱 창 표시 시간 초과" |
| 109 | time.sleep(0.2) |
| 110 | time.sleep(3) |
| 111 | assert app.poll() is None |
| 112 | subprocess.run(["import", "-window", window.stdout.splitlines()[0], |
| 113 | str(runs / "installed-window.png")], env=env, check=True) |
| 114 | print("PASS installed package, ZIP/7z/TAR.GZ defaults and native X11 window") |
| 115 | finally: |
| 116 | for process in (app, display): |
| 117 | if process is not None and process.poll() is None: |
| 118 | process.terminate() |
| 119 | try: |
| 120 | process.wait(timeout=10) |
| 121 | except subprocess.TimeoutExpired: |
| 122 | process.kill() |
| 123 | process.wait() |
| 124 | assert not Path("/usr/bin/zipelo").exists() |
| 125 | assert not Path("/usr/lib/zipelo").exists() |
| 126 | print("PASS package removed and previous MIME settings restored") |
| 127 | |
| 128 | |
| 129 | if __name__ == "__main__": |
| 130 | main() |