#!/usr/bin/env bash # Run the SwiftUI phone's library tests on a simulator (design P17/P18). # # pnpm ios:test # this Mac's walk simulator (scripts/ios-walk-sim.sh) # COTTON_SIM= pnpm ios:test # a specific one # # COTTON_ONLY_TESTING=CottonUITests/AlarmSyncModelTests pnpm ios:test # One class, or one case (`…/AlarmSyncModelTests/testFoo`), or several # separated by commas. For DIAGNOSING a known failure only — a filtered # run is not a verification, and the count line says so. # # `swift test` would build for macOS. The targets are iOS, so the tests run # under `xcodebuild test` against a simulator destination and nothing else. # # It generates the project first, and tests through it rather than through the # package's own `Cotton-Package` scheme: a `.xcodeproj` SHADOWS the package in # its own directory, so once anyone has run `pnpm ios:project` the package # scheme is unreachable from there. Generating is cheap (it compiles nothing) # and it makes this one command work from a clean checkout too. # # It takes the machine-wide build lock: this Mac is 16 GB, two heavy builds # swap-crash it, and the CI lane on the same machine takes the same lock. A # caller may wrap this whole script in `scripts/with-build-lock.sh` as well — # that helper is re-entrant, so the inner acquire below runs straight through # instead of waiting on a lock its own parent holds. set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT="$(cd "$HERE/.." && pwd)" OWN_SIM=0 RESULTS_DIR="" cleanup() { local status=$? trap - EXIT if [ "$OWN_SIM" = 1 ] && [ -n "$SIM" ]; then if ! bash "$ROOT/scripts/ios-walk-sim.sh" --cleanup-device "$SIM"; then echo "ios-test: simulator cleanup failed" >&2 [ "$status" -ne 0 ] || status=1 fi fi [ -z "$RESULTS_DIR" ] || rm -rf "$RESULTS_DIR" exit "$status" } trap cleanup EXIT trap 'exit 1' INT TERM SIM="${COTTON_SIM:-}" if [ -z "$SIM" ]; then OWN_SIM=1 # This Mac's walk simulator, never the first iPhone listed: the launch smoke # below ERASES the simulator it is handed, and the first one listed can be a # booted simulator another session is using (scripts/ios-walk-sim.sh). SIM="$(bash "$ROOT/scripts/ios-walk-sim.sh")" SIM="${SIM%%|*}" fi bash "$ROOT/scripts/gen-ios-project.sh" >/dev/null # The result bundle is where the COUNTS are. `-quiet` prints nothing but # failures, so a run that tested nothing at all looks exactly like a green one # — which is the single most expensive thing a test command can do. # # ONE PATH, EMPTIED AT EVERY START OF THE COMMAND. The build lock below YIELDS a # local run to a check job and then runs the command again from the top; the # first attempt has already created the bundle, and xcodebuild refuses a second # write to it (`Existing file at -resultBundlePath`, exit 64) — so the retry # tested nothing and the summary read the killed attempt's bundle as "NO TESTS # RAN". The `rm -rf` sits INSIDE the command the lock re-runs, so each attempt # writes a fresh bundle and the one read below is the run that finished. # The bundle's folder goes when this script exits: the summary below is read first. RESULTS_DIR="$(mktemp -d)" RESULTS="$RESULTS_DIR/ios-test.xcresult" # `-only-testing` narrows the run. Built as a string of flags rather than one # flag with a comma-joined value, which xcodebuild does not accept. ONLY="" if [ -n "${COTTON_ONLY_TESTING:-}" ]; then IFS=',' read -ra WANTED <<< "$COTTON_ONLY_TESTING" for one in "${WANTED[@]}"; do ONLY="$ONLY -only-testing:$one"; done echo "ios-test: FILTERED to $COTTON_ONLY_TESTING — a diagnosis, not a verification" fi # THE BUILD DIRECTORY IS ALWAYS NAMED, never left to Xcode. `COTTON_DERIVED_DATA` # wins when the caller set one (CI names it per head ref and sweeps that root); # otherwise `scripts/lib/derived-data.sh` answers with this branch's directory # under `~/.cache/cottondb/derived`. Unset and unnamed, xcodebuild would write to # ~/Library/Developer/Xcode/DerivedData/Cotton- — a # SECOND tree of the same intermediates, which nothing sweeps: 22 GB beside the # 22 GB of the first on one Mac, 2026-09-22. # shellcheck source=lib/derived-data.sh . "$ROOT/scripts/lib/derived-data.sh" COTTON_DERIVED_DATA="$(cotton_derived_data)" export COTTON_DERIVED_DATA DERIVED="-derivedDataPath '$COTTON_DERIVED_DATA'" echo "ios-test: derived data $COTTON_DERIVED_DATA" echo "ios-test: simulator $SIM" # Inside a lane's seatbelt Xcode's own sandboxes cannot start; the lane # policy's flags turn them off (scripts/lib/mac_disk_policy.py, lane_args). NO_SANDBOX="" [ "${COTTON_LANE_SANDBOXED:-}" = 1 ] && NO_SANDBOX="-IDEPackageSupportDisableManifestSandbox=YES \ -IDEPackageSupportDisablePluginExecutionSandbox=YES ENABLE_USER_SCRIPT_SANDBOXING=NO \ 'OTHER_SWIFT_FLAGS=\$(inherited) -disable-sandbox'" set +e BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-ios-test}" bash "$ROOT/scripts/with-build-lock.sh" \ bash -c "rm -rf '$RESULTS' && cd '$ROOT/apps/cotton' && xcodebuild test \ -project Cotton.xcodeproj \ -scheme tinmen-cotton \ -destination 'platform=iOS Simulator,id=$SIM' \ -resultBundlePath '$RESULTS' \ CODE_SIGNING_ALLOWED=NO \ COTTON_FLAVOR=dev \ $DERIVED \ $ONLY \ ${NO_SANDBOX:-} \ -quiet" status=$? set -e if [ -d "$RESULTS" ]; then xcrun xcresulttool get test-results summary --path "$RESULTS" --format json \ | /usr/bin/python3 -c 'import json,sys found = json.load(sys.stdin) print("ios-test:{} {} — {} passed, {} failed, {} skipped of {} tests".format( " FILTERED" if len(sys.argv) > 1 and sys.argv[1] else "", found.get("result", "?"), found.get("passedTests", 0), found.get("failedTests", 0), found.get("skippedTests", 0), found.get("totalTestCount", 0))) # A -quiet xcodebuild never prints the failing case, and the result bundle # lives in a mktemp dir CI throws away — so the ONE line CI keeps has to name # what broke, or every red run costs a local reproduction to even read. for failure in found.get("testFailures", []) or []: print("ios-test: FAILED {} — {}".format( failure.get("testName") or failure.get("name") or "?", (failure.get("failureText") or "").strip().splitlines()[0] if failure.get("failureText") else "")) if not found.get("totalTestCount"): raise SystemExit("ios-test: NO TESTS RAN — a green run that tested nothing is a red one")' \ "${COTTON_ONLY_TESTING:-}" fi # Launch walks belong to verify-main and the release workflow. A PR's XCTest # result never depends on simulator automation outside the test runner. exit "$status"