| 1 | #!/bin/sh |
| 2 | # Docs' screenshot gate (rastrillo/native RastrilloSnapshots): the library, |
| 3 | # Oficina ID onboarding, Other ways to sign in, the team picker and Settings, |
| 4 | # light and dark, and on iPhone at the default size and AX3; and the editor's |
| 5 | # page from fixed fixtures (EditorSnapshots.swift: Mac paged and pageless at |
| 6 | # 100% and 150%, iPhone and iPad width, page breaks, a scripted edit, ar, ru). |
| 7 | # |
| 8 | # native/scripts/snapshots.sh [compare|record] [mac|iphone|all] |
| 9 | # |
| 10 | # compare (the default) fails on any changed, missing or stale picture; |
| 11 | # record redraws the baselines in apple/Tests/DocsSnapshots/__Snapshots__/docs*. |
| 12 | # Review the contact sheets, apple/.build/snapshots/docs*/index.html, before |
| 13 | # committing a recording. Push baselines one commit per push, with |
| 14 | # `git -c http.postBuffer=209715200 push`: without it the hub drops a pack |
| 15 | # over about 1 MB ("unexpected disconnect while reading sideband packet"), |
| 16 | # and pushes back to back trip its per-IP limit. |
| 17 | # |
| 18 | # Both halves run even when the first fails, and each ends by printing the |
| 19 | # environment it drew on beside the one its baselines were drawn on. A |
| 20 | # change there is drift, not a regression: see BUILDING.md, "The |
| 21 | # screenshot gate", before re-recording anything. |
| 22 | # |
| 23 | # A screen that hangs the main thread never reaches the kit's own 3 s |
| 24 | # limit, so each half has a wall clock: SNAPSHOT_MAC_LIMIT seconds (default |
| 25 | # 300) for the Mac test run, after its build; and XCTest's execution time |
| 26 | # allowance, 120 s a test, for the iPhone half. |
| 27 | # |
| 28 | # Not in .amadan/ci.d: baselines are exact only on the OS and simulator |
| 29 | # runtime that drew them (ENVIRONMENT beside them says which). The runner |
| 30 | # scripts are rastrillo/native's own, from the pinned checkout; the iPhone |
| 31 | # half runs on its "Rastrillo Snapshots" simulator under its lock. |
| 32 | set -eu |
| 33 | cd "$(dirname "$0")/.." |
| 34 | mode=${1:-compare} |
| 35 | platforms=${2:-all} |
| 36 | case "$mode" in compare|record) ;; *) echo "usage: $0 [compare|record] [mac|iphone|all]" >&2; exit 2 ;; esac |
| 37 | case "$platforms" in mac|iphone|all) ;; *) echo "usage: $0 [compare|record] [mac|iphone|all]" >&2; exit 2 ;; esac |
| 38 | if [ "${OFICINA_SKIP_PREPARE:-0}" != 1 ]; then scripts/build-crdt.sh; fi |
| 39 | swift package --package-path apple resolve |
| 40 | shared=${OFICINA_NATIVE_PACKAGE_PATH:-apple/.build/checkouts/native} |
| 41 | case "$shared" in /*) ;; *) shared="$(pwd)/$shared" ;; esac |
| 42 | tools="$shared/tools" |
| 43 | |
| 44 | # Stops a process and everything under it. |
| 45 | kill_tree() { |
| 46 | for child in $(pgrep -P "$1" 2>/dev/null); do kill_tree "$child"; done |
| 47 | kill -TERM "$1" 2>/dev/null || true |
| 48 | } |
| 49 | |
| 50 | # Runs the rest of the line, stopping it after $1 seconds. |
| 51 | with_limit() { |
| 52 | limit=$1 |
| 53 | shift |
| 54 | "$@" & |
| 55 | pid=$! |
| 56 | ( |
| 57 | sleep "$limit" |
| 58 | echo "the snapshot run passed its ${limit} s limit: a screen hung the main thread; stopping it" >&2 |
| 59 | kill_tree "$pid" |
| 60 | ) & |
| 61 | watchdog=$! |
| 62 | set +e |
| 63 | wait "$pid" |
| 64 | status=$? |
| 65 | set -e |
| 66 | kill_tree "$watchdog" |
| 67 | return "$status" |
| 68 | } |
| 69 | |
| 70 | # What each catalog's run drew on, and what its baselines were drawn on. |
| 71 | print_environment() { |
| 72 | python3 - "$1" <<'EOF' |
| 73 | import json, pathlib, sys |
| 74 | platform = sys.argv[1] |
| 75 | shown = None |
| 76 | for results in sorted(pathlib.Path("apple/.build/snapshots").glob(f"*/{platform}/results.json")): |
| 77 | run = json.loads(results.read_text()) |
| 78 | here = run.get("environment", "") |
| 79 | if shown is None: |
| 80 | print(f"{platform} half drawn on:") |
| 81 | print(" " + here.replace("\n", "\n ")) |
| 82 | shown = here |
| 83 | recorded = run.get("baselineEnvironment") or "(none recorded)" |
| 84 | if recorded == here: |
| 85 | print(f" {run['app']}: baselines drawn on the same") |
| 86 | else: |
| 87 | print(f" {run['app']}: ENVIRONMENT DIFFERS from its baselines, drawn on:") |
| 88 | print(" " + recorded.replace("\n", "\n ")) |
| 89 | print(" Drift, not a regression: re-record from main in its own commit first (BUILDING.md).") |
| 90 | EOF |
| 91 | } |
| 92 | |
| 93 | failed=0 |
| 94 | case "$platforms" in mac|all) |
| 95 | (cd apple && swift build --build-tests) |
| 96 | if ! (cd apple && with_limit "${SNAPSHOT_MAC_LIMIT:-300}" "$tools/snapshots-mac.sh" "$mode" DocsSnapshots); then failed=1; fi |
| 97 | print_environment mac ;; |
| 98 | esac |
| 99 | case "$platforms" in iphone|all) |
| 100 | xcodegen generate --spec ios/project.yml |
| 101 | # Build products stay under apple/.build, beside the Mac half's. |
| 102 | if ! (cd apple && SNAPSHOT_DERIVED_DATA=.build/xcode "$tools/snapshots-iphone.sh" "$mode" DocsPhoneSnapshots DocsPhoneSnapshots \ |
| 103 | -project ../ios/DocsPhone.xcodeproj -test-timeouts-enabled YES -default-test-execution-time-allowance 120); then |
| 104 | failed=1 |
| 105 | grep -h "exceeded execution time allowance" apple/.build/snapshots-iphone.log || true |
| 106 | fi |
| 107 | print_environment iphone ;; |
| 108 | esac |
| 109 | exit "$failed" |
| 110 | |