|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Wait until an iOS Simulator is fully ready for integration tests (including first-boot |
| 4 | +# data migration). Intended to run after futureware-tech/simulator-action (or any step |
| 5 | +# that has issued simctl boot) and before flutter test / flutter drive. |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# SIMULATOR=<udid-or-device-name> ./ensure-simulator-ready.sh |
| 9 | +# ./ensure-simulator-ready.sh <udid-or-device-name> |
| 10 | +# |
| 11 | +# Environment: |
| 12 | +# SIMULATOR UDID or device name (required if not passed as arg) |
| 13 | +# BOOT_POLL_INTERVAL_SECONDS Poll interval (default: 20) |
| 14 | +# BOOT_PROBE_TIMEOUT_SECONDS Per-probe timeout (default: 12) |
| 15 | +# BOOT_MAX_WAIT_SECONDS Max wait for full boot (default: 660) |
| 16 | +# ENSURE_OPEN_SIMULATOR_APP Open Simulator.app when booting (default: 1) |
| 17 | +# ENSURE_BOOT_IF_NEEDED simctl boot when not Booted yet (default: 1) |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +BOOT_POLL_INTERVAL_SECONDS="${BOOT_POLL_INTERVAL_SECONDS:-20}" |
| 21 | +BOOT_PROBE_TIMEOUT_SECONDS="${BOOT_PROBE_TIMEOUT_SECONDS:-12}" |
| 22 | +BOOT_MAX_WAIT_SECONDS="${BOOT_MAX_WAIT_SECONDS:-660}" |
| 23 | +ENSURE_OPEN_SIMULATOR_APP="${ENSURE_OPEN_SIMULATOR_APP:-1}" |
| 24 | +ENSURE_BOOT_IF_NEEDED="${ENSURE_BOOT_IF_NEEDED:-1}" |
| 25 | + |
| 26 | +DEVICE="${SIMULATOR:-${1:-}}" |
| 27 | +if [[ -z "$DEVICE" ]]; then |
| 28 | + echo "[boot-status] ERROR: set SIMULATOR or pass device UDID/name as first argument" >&2 |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +run_with_timeout() { |
| 33 | + local max="$1" |
| 34 | + shift |
| 35 | + "$@" & |
| 36 | + local cmd_pid=$! |
| 37 | + local waited=0 |
| 38 | + while kill -0 "$cmd_pid" 2>/dev/null && (( waited < max )); do |
| 39 | + sleep 1 |
| 40 | + waited=$((waited + 1)) |
| 41 | + done |
| 42 | + if kill -0 "$cmd_pid" 2>/dev/null; then |
| 43 | + kill "$cmd_pid" 2>/dev/null |
| 44 | + wait "$cmd_pid" 2>/dev/null || true |
| 45 | + return 124 |
| 46 | + fi |
| 47 | + wait "$cmd_pid" |
| 48 | +} |
| 49 | + |
| 50 | +log_boot_status() { |
| 51 | + echo "[boot-status] $*" |
| 52 | +} |
| 53 | + |
| 54 | +is_udid() { |
| 55 | + [[ "$1" =~ ^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$ ]] |
| 56 | +} |
| 57 | + |
| 58 | +describe_booted_device() { |
| 59 | + local device="$1" |
| 60 | + if is_udid "$device"; then |
| 61 | + xcrun simctl list devices booted 2>/dev/null \ |
| 62 | + | grep -F "$device" \ |
| 63 | + | grep -v 'unavailable' \ |
| 64 | + | head -1 \ |
| 65 | + || true |
| 66 | + else |
| 67 | + xcrun simctl list devices booted 2>/dev/null \ |
| 68 | + | grep -i "${device} (" \ |
| 69 | + | grep -v 'Phone:' \ |
| 70 | + | grep -v 'unavailable' \ |
| 71 | + | grep -v CoreSimulator \ |
| 72 | + | head -1 \ |
| 73 | + || true |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +is_device_booted() { |
| 78 | + [[ -n "$(describe_booted_device "$1")" ]] |
| 79 | +} |
| 80 | + |
| 81 | +log_migration_status() { |
| 82 | + local device="$1" |
| 83 | + local migration_output probe_rc |
| 84 | + |
| 85 | + log_boot_status "probing data migration (bootstatus -d, up to ${BOOT_PROBE_TIMEOUT_SECONDS}s)..." |
| 86 | + set +e |
| 87 | + migration_output="$(run_with_timeout "$BOOT_PROBE_TIMEOUT_SECONDS" xcrun simctl bootstatus "$device" -d 2>&1)" |
| 88 | + probe_rc=$? |
| 89 | + set -e |
| 90 | + |
| 91 | + if [[ "$probe_rc" -eq 124 ]]; then |
| 92 | + log_boot_status " data migration / system bring-up still in progress" |
| 93 | + return 1 |
| 94 | + fi |
| 95 | + |
| 96 | + if [[ -n "$migration_output" ]]; then |
| 97 | + while IFS= read -r line; do |
| 98 | + [[ -z "$line" ]] && continue |
| 99 | + log_boot_status " ${line}" |
| 100 | + done <<<"$migration_output" |
| 101 | + else |
| 102 | + log_boot_status " no migration details reported" |
| 103 | + fi |
| 104 | + return 0 |
| 105 | +} |
| 106 | + |
| 107 | +wait_for_simulator_ready() { |
| 108 | + local device="$1" |
| 109 | + local start=$SECONDS |
| 110 | + |
| 111 | + while (( SECONDS - start < BOOT_MAX_WAIT_SECONDS )); do |
| 112 | + local elapsed=$(( SECONDS - start )) |
| 113 | + local booted_line ready_rc |
| 114 | + |
| 115 | + log_boot_status "elapsed=${elapsed}s phase=wait_for_full_boot device=\"${device}\"" |
| 116 | + |
| 117 | + booted_line="$(describe_booted_device "$device")" |
| 118 | + if [[ -z "$booted_line" ]]; then |
| 119 | + log_boot_status " simctl list: not in Booted state yet" |
| 120 | + else |
| 121 | + log_boot_status " simctl list: ${booted_line}" |
| 122 | + log_migration_status "$device" || true |
| 123 | + fi |
| 124 | + |
| 125 | + set +e |
| 126 | + run_with_timeout "$BOOT_PROBE_TIMEOUT_SECONDS" xcrun simctl bootstatus "$device" >/dev/null 2>&1 |
| 127 | + ready_rc=$? |
| 128 | + set -e |
| 129 | + |
| 130 | + if [[ "$ready_rc" -eq 0 ]]; then |
| 131 | + log_boot_status "bootstatus: simulator ready after ${elapsed}s" |
| 132 | + log_migration_status "$device" || true |
| 133 | + return 0 |
| 134 | + fi |
| 135 | + |
| 136 | + if [[ "$ready_rc" -eq 124 ]]; then |
| 137 | + log_boot_status "bootstatus: still booting (probe timed out after ${BOOT_PROBE_TIMEOUT_SECONDS}s)" |
| 138 | + else |
| 139 | + log_boot_status "bootstatus: probe exited with status ${ready_rc}" |
| 140 | + fi |
| 141 | + |
| 142 | + sleep "$BOOT_POLL_INTERVAL_SECONDS" |
| 143 | + done |
| 144 | + |
| 145 | + log_boot_status "ERROR: timed out after ${BOOT_MAX_WAIT_SECONDS}s waiting for simulator to become ready" |
| 146 | + return 1 |
| 147 | +} |
| 148 | + |
| 149 | +if is_udid "$DEVICE"; then |
| 150 | + log_boot_status "phase=resolve_device udid=\"${DEVICE}\"" |
| 151 | +else |
| 152 | + log_boot_status "phase=resolve_device name=\"${DEVICE}\"" |
| 153 | +fi |
| 154 | + |
| 155 | +if ! is_device_booted "$DEVICE"; then |
| 156 | + if [[ "$ENSURE_BOOT_IF_NEEDED" == "1" ]]; then |
| 157 | + log_boot_status "phase=boot_command device not Booted; starting simctl boot..." |
| 158 | + set +e |
| 159 | + boot_output="$(xcrun simctl boot "$DEVICE" 2>&1)" |
| 160 | + boot_rc=$? |
| 161 | + set -e |
| 162 | + if [[ "$boot_rc" -ne 0 ]]; then |
| 163 | + log_boot_status "simctl boot exited ${boot_rc}: ${boot_output}" |
| 164 | + else |
| 165 | + log_boot_status "simctl boot command returned (device may still be migrating data)" |
| 166 | + fi |
| 167 | + if [[ "$ENSURE_OPEN_SIMULATOR_APP" == "1" ]]; then |
| 168 | + log_boot_status "phase=foreground_simulator opening Simulator.app..." |
| 169 | + open -a Simulator.app || true |
| 170 | + fi |
| 171 | + else |
| 172 | + log_boot_status "phase=boot_command skipped (ENSURE_BOOT_IF_NEEDED=0); waiting for existing boot..." |
| 173 | + fi |
| 174 | +else |
| 175 | + log_boot_status "phase=boot_command device already Booted; waiting for full readiness..." |
| 176 | +fi |
| 177 | + |
| 178 | +if ! wait_for_simulator_ready "$DEVICE"; then |
| 179 | + exit 1 |
| 180 | +fi |
| 181 | + |
| 182 | +log_boot_status "phase=complete device=\"${DEVICE}\" ready for flutter test" |
0 commit comments