AtlatestRepositorysigil-ui

sigil-ui / tree / test / macosqualify.sgl

1#!/usr/bin/env sigil
2;;; Run sealed Linux-built fixtures locally on an arm64 Mac. Default mode
3;;; checks all preconditions and leaves the filesystem unchanged.
4(import (sigil shell prelude))
5(requires (tools uname sw_vers shasum cp mkdir xattr))
6(define packet (realpath (script-dir)))
7(define destination (arg 1))
8(define expected-manifest (arg 2))
9(define mode (arg 3 default: "--plan"))
10(unless (regex-match? "^[0-9a-f]{64}$" expected-manifest)
11 (error "Supply the independently recorded manifest SHA-256"))
12(unless (member mode '("--plan" "--execute")) (error "Expected --plan or --execute"))
13(unless (and (equal? (out uname -s) "Darwin") (equal? (out uname -m) "arm64"))
14 (error "Qualification requires an arm64 Mac"))
15(define os-version (out sw_vers -productVersion))
16(unless (>= (string->number (car (split os-version "."))) 15)
17 (error "Qualification requires macOS 15.0 or newer" os-version))
18(unless (and (starts-with? destination "/")
19 (equal? destination (path-normalize destination))
20 (directory? (dirname destination))
21 (not (file-exists? destination)) (not (symlink? destination)))
22 (error "Supply a fresh absolute run directory with an existing parent" destination))
23(define manifest (path packet "SHA256SUMS"))
24(unless (equal? expected-manifest
25 (car (regex-split "\\s+" (out shasum -a 256 ,manifest))))
26 (error "Manifest differs from its independently supplied SHA-256"))
27(define fixtures '("host-aarch64" "appkit-conformance" "webkit-aarch64" "initialization-aarch64"
28 "font-aarch64" "ui-drawing-aarch64"))
29(define resources
30 '("font/fixtures/JetBrainsMono-Regular.ttf" "font/fixtures/JetBrainsMono-Bold.ttf"
31 "font/fixtures/NotoSansSymbols2-Regular.ttf" "font/fixtures/OFL.txt"
32 "font/fixtures/Noto-Symbols-OFL.txt" "font/fixtures/README.md" "font/fixtures/helpers.sgl"
33 "font/test/test-conformance.sgl" "font/test/test-coretext-lifetime.sgl"
34 "font/test/test-coretext-unicode.sgl"
35 "font/source/coretext.c" "font/source/coretext-runtime.c" "font/source/coretext-runtime.h"
36 "font/source/fixture.c" "font/source/coretext-main.sgl" "font/source/drawing-main.sgl"
37 "font/source/capture.c" "font/source/drawing-package.sgl"
38 "font/source/probe-support.h" "font/source/fixture-module.sgl"
39 "font/source/drawing-native.sgl" "font/source/coretext-package.sgl" "font/source/font-package.sgl"
40 "font/source/collection-support.h"))
41(define names (append fixtures '("Info.plist" "qualify.sgl" "fixture-arguments.sgl") resources))
42(define rows (filter (lambda (row) (not (string=? row "")))
43 (split (read-text manifest trim: #t) "\n")))
44(unless (= (length rows) (length names)) (error "Qualification manifest is incomplete"))
45(define seen '())
46(define expected-hashes '())
47(each row rows
48 (define match (regex-match "^([0-9a-f]{64}) ([A-Za-z0-9._/-]+)$" row))
49 (unless match (error "Invalid manifest row" row))
50 (define name (match-group match 2))
51 (unless (and (member name names) (not (member name seen)) (file? (path packet name)))
52 (error "Unexpected, duplicate or missing qualification input" name))
53 (set! seen (cons name seen))
54 (set! expected-hashes (cons (cons name (match-group match 1)) expected-hashes))
55 (unless (equal? (match-group match 1)
56 (car (regex-split "\\s+" (out shasum -a 256 ,(path packet name)))))
57 (error "Qualification input hash mismatch" name)))
58(each name fixtures
59 (unless (positive? (bitwise-and (stat-mode (file-stat (path packet name))) #o100))
60 (error "Fixture is not executable (owner execute bit required)" name)))
61(println "PASS: arm64 macOS ~a, externally pinned manifest, ~a exact hashes, fresh output directory"
62 os-version (length names))
63(load (path packet "fixture-arguments.sgl"))
64(println "PLAN: directly launch copied host/modal, Sigil, WK, initialization, font and UI bundle executables")
65(println "PLAN: host/Sigil/WK/initialization receive RUN; font/UI receive RUN RESOURCES; retain actual process status")
66(println "PLAN: copy pinned fonts/tests; process-only font registrations are cleaned up; verify exit/PID/hash")
67(println "PLAN: automatically clear com.apple.quarantine only on newly copied scratch bundles before launch")
68(println "PLAN: no compiler, SDK, signing, credentials or network access required")
69(println "PLAN: retain independent fixture failures, continue through drawing, then fail the aggregate if needed")
70(when (equal? mode "--execute")
71 ($ mkdir -m 700 ,destination)
72 (define owned-resources (path destination "resources"))
73 (each name resources
74 (define copy (path owned-resources name))
75 (ensure-directory (dirname copy))
76 ($ cp ,(path packet name) ,copy)
77 (unless (equal? (car (regex-split "\\s+" (out shasum -a 256 ,copy)))
78 (cdr (assoc name expected-hashes)))
79 (error "Scratch resource copy differs" name)))
80 ($ xattr -dr com.apple.quarantine ,owned-resources)
81 (define outcomes '())
82 (each name fixtures
83 (define run (path destination name))
84 ($ mkdir ,run)
85 (define contents (path run "Probe.app/Contents"))
86 (ensure-directory (path contents "MacOS"))
87 (define binary (path contents "MacOS/runtime-probe"))
88 ($ cp ,(path packet name) ,binary)
89 (unless (positive? (bitwise-and (stat-mode (file-stat binary)) #o100))
90 (error "Copied fixture is not executable (owner execute bit required)" name))
91 ($ cp ,(path packet "Info.plist") ,(path contents "Info.plist"))
92 (unless (equal? (car (regex-split "\\s+" (out shasum -a 256 ,(path contents "Info.plist"))))
93 (cdr (assoc "Info.plist" expected-hashes)))
94 (error "Copied bundle metadata differs from the pinned manifest" name))
95 ($ xattr -dr com.apple.quarantine ,(path run "Probe.app"))
96 (define hash (car (regex-split "\\s+" (out shasum -a 256 ,binary))))
97 (unless (equal? hash (cdr (assoc name expected-hashes)))
98 (error "Copied executable differs from the pinned manifest" name))
99 ;; Copy/pin failures above remain fatal. Independent fixture behavior
100 ;; failures below are retained, then the remaining fixtures still run.
101 (guard (failure
102 (else
103 (define message (exception-message failure))
104 (write-text (path run "fixture.failure") (str message "\n"))
105 (set! outcomes (cons (cons name "FAIL") outcomes))
106 (println "FAIL: ~a: ~a" name message)))
107 ;; Direct execution retains the fixture's real exit status. LaunchServices'
108 ;; open -W reports its own success even when a fixture exits before START.
109 (define arguments (fixture-arguments name run owned-resources))
110 (define launched ($? ,binary ,@arguments timeout: 180))
111 (write-text (path run "launch.result") (format-outcome launched))
112 (write-text (path run "launch.stdout") (stdout-of launched))
113 (write-text (path run "launch.stderr") (stderr-of launched))
114 (define result (read-text (path run "runtime.result") missing: "" trim: #t))
115 (define result-match (regex-match "status=(\\d+) pid=(\\d+) sha256=([0-9a-f]{64})" result))
116 (define log-text (read-text (path run "runtime.log") missing: ""))
117 (define start (regex-search "START pid=(\\d+)\n" log-text))
118 (display log-text)
119 (display (stderr-of launched))
120 (display (read-text (path run "runtime.stderr") missing: ""))
121 (unless start
122 (error (format "FAIL: app exited with no START line (exit status ~a; ~a)"
123 (exit-status launched) (format-outcome launched)) name run))
124 (unless (and (ok? launched) start result-match
125 (equal? (match-group result-match 1) "0")
126 (equal? (match-group start 1) (match-group result-match 2))
127 (equal? hash (match-group result-match 3)))
128 (error "FAIL: fixture exit/PID/hash evidence did not match" name run))
129 (set! outcomes (cons (cons name "PASS") outcomes))
130 (println "PASS: ~a exact executable ~a exited with matching evidence" name hash)))
131 (write-text (path destination "qualification.result")
132 (str (string-join (map (lambda (entry) (str (car entry) " " (cdr entry)))
133 (reverse outcomes)) "\n") "\n"))
134 (unless (every (lambda (entry) (equal? (cdr entry) "PASS")) outcomes)
135 (error "FAIL: qualification fixtures failed; all fixtures attempted"
136 (reverse outcomes) destination))
137 (println "PASS: AppKit, CoreText and native UI drawing; evidence ~a" destination))