Scripts/test.sh

126 lines · 4096 bytes · executable

  1#!/usr/bin/env bash
  2#
  3# Run the deterministic unit suite against real simulator runtimes.
  4#
  5#   ./Scripts/test.sh            # floor + current
  6#   ./Scripts/test.sh floor      # oldest supported runtime only
  7#   ./Scripts/test.sh current    # newest installed runtime only
  8#
  9# Why both rather than one simulator: the suite's result is not runtime
 10# independent. Releasing a main-actor-isolated object aborts the test runner
 11# under iOS 18.6 with "pointer being freed was not allocated" — a Swift runtime
 12# bug in MarkerItem::create, fixed by iOS 26.5 — so a test that constructs and
 13# releases a view model passes on current and dies on floor. Pinning one
 14# destination hides whichever direction it is pinned away from.
 15#
 16# This covers DomainDigTests only. Scripts/audit-a11y.sh covers the
 17# accessibility suite, which has its own reason for running both tiers.
 18
 19set -euo pipefail
 20
 21cd "$(dirname "$0")/.."
 22
 23PROJECT="DomainDig.xcodeproj"
 24SCHEME="DomainDig"
 25TIER="${1:-both}"
 26MAX_LAUNCH_ATTEMPTS=3
 27
 28# shellcheck source=lib/simulators.sh
 29source "$(dirname "$0")/lib/simulators.sh"
 30
 31case "$TIER" in
 32  floor | current | both) ;;
 33  *)
 34    echo "usage: $0 [floor|current|both]" >&2
 35    exit 2
 36    ;;
 37esac
 38
 39echo "==> Reading deployment target from $PROJECT"
 40read_deployment_target
 41echo "    deployment target: $DEPLOYMENT_TARGET (rank $FLOOR_RANK)"
 42
 43run_tier() {
 44  local which="$1"
 45  local sim udid label
 46  local attempt=1
 47
 48  sim=$(select_sim "$which")
 49  if [[ -z "$sim" ]]; then
 50    echo "error: no iPhone simulator at or above iOS $DEPLOYMENT_TARGET installed" >&2
 51    echo "hint: install one with 'xcodebuild -downloadPlatform iOS'" >&2
 52    return 1
 53  fi
 54
 55  udid=$(echo "$sim" | jq -r .udid)
 56  label=$(echo "$sim" | jq -r '"\(.name) (iOS \(.os))"')
 57
 58  echo
 59  echo "==> $which: $label"
 60
 61  if [[ "$which" = "floor" ]] && floor_is_approximate "$sim"; then
 62    echo "    NOTE: nearest installed runtime is a major version above the $DEPLOYMENT_TARGET"
 63    echo "          deployment target, so this is not true floor coverage."
 64  fi
 65
 66  local log
 67  log=$(mktemp -t "domaindig-test-$which")
 68
 69  while :; do
 70    # Boot and wait rather than letting xcodebuild race its own boot.
 71    xcrun simctl bootstatus "$udid" -b > /dev/null 2>&1 || true
 72
 73    set +e
 74    xcodebuild test \
 75      -project "$PROJECT" \
 76      -scheme "$SCHEME" \
 77      -destination "id=$udid" \
 78      -only-testing:DomainDigTests > "$log" 2>&1
 79    local code=$?
 80    set -e
 81
 82    grep -E 'Executed [0-9]+ test|\*\* TEST (SUCCEEDED|FAILED)|error:|malloc: \*\*\*|^Test Case .* failed|failed to launch|did not return a process handle' "$log" || true
 83
 84    xcrun simctl shutdown "$udid" > /dev/null 2>&1 || true
 85
 86    if [[ "$code" -eq 0 ]] || ! launch_failed "$log" || [[ "$attempt" -ge "$MAX_LAUNCH_ATTEMPTS" ]]; then
 87      rm -f "$log"
 88      return "$code"
 89    fi
 90
 91    echo "    launch failed before any test ran; retrying ($attempt/$((MAX_LAUNCH_ATTEMPTS - 1)))"
 92    attempt=$((attempt + 1))
 93  done
 94}
 95
 96# A CoreSimulator flake, not a test result. The device accepts the install and
 97# then the test host never starts: sometimes with "Application launch ... did
 98# not return a process handle nor launch error", sometimes with no diagnostic
 99# at all beyond "** TEST FAILED **", and left alone xcodebuild can sit ~380s
100# before saying so. Seen on roughly a third of invocations here, on several
101# devices and both runtimes, and not fixed by booting and waiting first.
102#
103# Retrying is a workaround for that, so the test is what makes it safe rather
104# than the error text, which varies: a run that executed no tests at all did
105# not get far enough to have a test result, and a compile failure is excluded
106# so a broken build is not retried three times.
107launch_failed() {
108  local log="$1"
109  ! grep -qE 'Executed [0-9]+ test' "$log" && ! grep -qE ': error:' "$log"
110}
111
112status=0
113if [[ "$TIER" = "both" ]]; then
114  run_tier floor || status=1
115  run_tier current || status=1
116else
117  run_tier "$TIER" || status=1
118fi
119
120echo
121if [[ "$status" -eq 0 ]]; then
122  echo "==> Done. Green on every tier run."
123else
124  echo "==> At least one tier failed."
125fi
126exit "$status"