| 1 | #!/usr/bin/env python3 |
| 2 | """Launch a relocated release archive without access to SwiftPM build fallbacks. |
| 3 | |
| 4 | Run on a logged-in Mac. No files in the source/build directories are modified. |
| 5 | The test terminates only the app process it launches. |
| 6 | """ |
| 7 | import argparse |
| 8 | import json |
| 9 | from pathlib import Path |
| 10 | import plistlib |
| 11 | import subprocess |
| 12 | import tempfile |
| 13 | import time |
| 14 | |
| 15 | parser = argparse.ArgumentParser(description=__doc__) |
| 16 | parser.add_argument("archive", type=Path) |
| 17 | args = parser.parse_args() |
| 18 | with tempfile.TemporaryDirectory(prefix="oficina-sheets-launch-") as directory: |
| 19 | root = Path(directory) |
| 20 | subprocess.run(["ditto", "-x", "-k", str(args.archive.resolve()), str(root)], check=True) |
| 21 | apps = list(root.glob("*.app")) |
| 22 | assert len(apps) == 1, "Expected one application in release archive" |
| 23 | app = apps[0] |
| 24 | info = plistlib.loads((app / "Contents/Info.plist").read_bytes()) |
| 25 | # Paul, 2026-11-14: works.oficina.* on every build, "Oficina Sheets" where |
| 26 | # Finder looks, CFBundleName the plain short name "Sheets" (the menu bar, |
| 27 | # the About window, PlainAppNameCommands' About/Hide/Quit and, through |
| 28 | # SUBundleName, Sparkle's windows), and the sign-in scheme registered so |
| 29 | # the browser's callback reaches the app. |
| 30 | assert info["CFBundleIdentifier"] == "works.oficina.sheets", info["CFBundleIdentifier"] |
| 31 | assert app.name == "Oficina Sheets.app" and info["CFBundleDisplayName"] == "Oficina Sheets", app.name |
| 32 | assert info["CFBundleName"] == "Sheets", info["CFBundleName"] |
| 33 | assert info.get("SUBundleName") == "Sheets", info.get("SUBundleName") |
| 34 | # Without CFBundleLocalizations every bundle in the app, RastrilloApple's |
| 35 | # menu titles and SheetsKit's own strings included, reads English. |
| 36 | # bundle-mac.py writes it from mac-updates.json, so only a release |
| 37 | # (--updates-config) build carries it. |
| 38 | if "SUFeedURL" in info: |
| 39 | shipped = sorted(p.stem for p in (Path(__file__).resolve().parents[1] / |
| 40 | "apple/Sources/SheetsKit/Resources").glob("*.lproj")) |
| 41 | assert sorted(info.get("CFBundleLocalizations", [])) == shipped, (info.get("CFBundleLocalizations"), shipped) |
| 42 | assert info.get("CFBundleDevelopmentRegion") == "en", info.get("CFBundleDevelopmentRegion") |
| 43 | # "install": "automatic" in mac-updates.json: Sparkle checks daily and |
| 44 | # installs on quit without first asking whether it may check. |
| 45 | for key in ("SUAllowsAutomaticUpdates", "SUEnableAutomaticChecks", "SUAutomaticallyUpdate"): |
| 46 | assert info.get(key) is True, (key, info.get(key)) |
| 47 | assert info.get("SUScheduledCheckInterval") == 86400, info.get("SUScheduledCheckInterval") |
| 48 | assert any("works.oficina.sheets" in entry.get("CFBundleURLSchemes", []) |
| 49 | for entry in info.get("CFBundleURLTypes", [])), "works.oficina.sheets URL scheme not registered" |
| 50 | executable = app / "Contents/MacOS" / info["CFBundleExecutable"] |
| 51 | strings = subprocess.check_output(["strings", str(executable)], text=True).splitlines() |
| 52 | # Before Swift 6.4 the resource accessor fell back to the absolute .build |
| 53 | # path it was compiled in; deny every such path the binary names. Swift |
| 54 | # 6.4.2's accessor names none (Bundle.main, Bundle(for:), the bundle URL), |
| 55 | # so the scan finds nothing there. This checkout's own build directory is |
| 56 | # denied either way, which keeps the test denying something real. |
| 57 | fallbacks = {line.split("/.build/", 1)[0] + "/.build" for line in strings |
| 58 | if line.startswith("/") and "/.build/" in line and line.endswith(".bundle")} |
| 59 | fallbacks.add(str(Path(__file__).resolve().parents[1] / "apple/.build")) |
| 60 | profile = '(version 1)(allow default)' + ''.join( |
| 61 | f'(deny file-read* (subpath {json.dumps(path)}))' for path in sorted(fallbacks)) |
| 62 | probe = root / "windows.swift" |
| 63 | probe.write_text('''import CoreGraphics |
| 64 | import Foundation |
| 65 | let pid = Int(CommandLine.arguments[1])! |
| 66 | let windows = CGWindowListCopyWindowInfo(.optionAll, kCGNullWindowID) as? [[String: Any]] ?? [] |
| 67 | let found = windows.contains { window in |
| 68 | guard window[kCGWindowOwnerPID as String] as? Int == pid, |
| 69 | window[kCGWindowLayer as String] as? Int == 0, |
| 70 | let bounds = window[kCGWindowBounds as String] as? [String: Any], |
| 71 | let width = bounds["Width"] as? Double, |
| 72 | let height = bounds["Height"] as? Double else { return false } |
| 73 | return width >= 300 && height >= 100 // Signed-out account chooser is compact. |
| 74 | } |
| 75 | exit(found ? 0 : 1) |
| 76 | ''') |
| 77 | subprocess.run(["swiftc", str(probe), "-o", str(root / "windows")], check=True) |
| 78 | with (root / "launch.log").open("w+") as log: |
| 79 | process = subprocess.Popen(["sandbox-exec", "-p", profile, str(executable), |
| 80 | "-ApplePersistenceIgnoreState", "YES"], stdout=log, stderr=log) |
| 81 | try: |
| 82 | found = False |
| 83 | deadline = time.monotonic() + 12 |
| 84 | while time.monotonic() < deadline: |
| 85 | if process.poll() is not None: |
| 86 | log.seek(0) |
| 87 | raise RuntimeError(f"App exited with {process.returncode}: {log.read()}") |
| 88 | if subprocess.run([str(root / "windows"), str(process.pid)]).returncode == 0: |
| 89 | found = True |
| 90 | time.sleep(0.25) |
| 91 | assert found, "App survived but did not create a normal window" |
| 92 | print(f'PASS: {info["CFBundleName"]} {info["CFBundleShortVersionString"]} ' |
| 93 | f'({info["CFBundleVersion"]}) opens a window with build-directory access denied') |
| 94 | finally: |
| 95 | if process.poll() is None: |
| 96 | process.terminate() |
| 97 | try: |
| 98 | process.wait(timeout=5) |
| 99 | except subprocess.TimeoutExpired: |
| 100 | process.kill() |
| 101 | process.wait() |
| 102 | |