|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Checks that the app in the current directory starts: on the Android |
| 3 | +# emulator or device $2 with $1 android, or on the booted iOS simulator |
| 4 | +# with the UDID $2 with $1 ios, in at most $3 seconds, the build of the app |
| 5 | +# included. |
| 6 | +# |
| 7 | +# The app has the start check of smf_flutter_core, |
| 8 | +# integration_test/start_check.dart, which the matrix tool of the CLI adds |
| 9 | +# with --add-app-tests. The script builds a debug app with the check as its |
| 10 | +# entry, installs it anew and launches it with adb or simctl, and waits for |
| 11 | +# the line that the check writes to the file smf_start_check in the |
| 12 | +# temporary directory of the app: `passed`, or `failed: ` and the problems. |
| 13 | +# Nothing connects to the app, unlike flutter test, which must reach the VM |
| 14 | +# service of the app and sometimes never does on the emulators and |
| 15 | +# simulators of CI (on an iOS simulator, a race of flutter_tools: |
| 16 | +# https://github.com/flutter/flutter/issues/181771). So a run that fails |
| 17 | +# is not tried again. The script fails when the check fails, when the app |
| 18 | +# stops before the check writes its result, as when it crashes at start-up, |
| 19 | +# and when the result does not come in time, and then prints the log of |
| 20 | +# the app. |
| 21 | +set -euo pipefail |
| 22 | + |
| 23 | +if [ $# -ne 3 ] || [[ ! "$1" =~ ^(android|ios)$ ]] || [[ ! "$3" =~ ^[0-9]+$ ]]; then |
| 24 | + echo "Usage: $0 <android|ios> <device> <seconds>" >&2 |
| 25 | + exit 64 |
| 26 | +fi |
| 27 | +platform="$1" |
| 28 | +device="$2" |
| 29 | +seconds="$3" |
| 30 | +deadline=$((SECONDS + seconds)) |
| 31 | +check=integration_test/start_check.dart |
| 32 | +result=smf_start_check |
| 33 | + |
| 34 | +if [ ! -f "$check" ]; then |
| 35 | + echo "::error::$PWD has no $check: add it with the --add-app-tests of packages/smf_flutter_cli/tool/matrix.dart." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Runs the command after $1 for at most $1 seconds, with perl rather than |
| 40 | +# timeout, which the macOS runners lack; a command that runs out of time |
| 41 | +# ends with 142, killed by SIGALRM. |
| 42 | +within() { |
| 43 | + local limit="$1" |
| 44 | + shift |
| 45 | + perl -e 'alarm shift; exec @ARGV' "$limit" "$@" |
| 46 | +} |
| 47 | + |
| 48 | +# Runs the command after $1, which names it, in the time that is left, and |
| 49 | +# fails when it fails. |
| 50 | +step() { |
| 51 | + local name="$1" left=$((deadline - SECONDS)) code=0 |
| 52 | + shift |
| 53 | + if [ "$left" -gt 0 ]; then |
| 54 | + within "$left" "$@" || code=$? |
| 55 | + else |
| 56 | + code=142 |
| 57 | + fi |
| 58 | + if [ "$code" -eq 142 ]; then |
| 59 | + echo "::error::$name did not finish in the $seconds s of the start of the app." |
| 60 | + exit 1 |
| 61 | + elif [ "$code" -ne 0 ]; then |
| 62 | + echo "::error::$name failed with the exit code $code." |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# Waits for the result of the check, which the command in the arguments |
| 68 | +# prints once the app wrote it, as long as `alive` finds the app running. |
| 69 | +# Fails when the result is not `passed`, when the app stops before it, and |
| 70 | +# when it does not come in the time that is left, and then prints the log |
| 71 | +# of the app with `show_log`. |
| 72 | +wait_for_result() { |
| 73 | + local launched=$SECONDS line |
| 74 | + while :; do |
| 75 | + if line="$(within 30 "$@" 2> /dev/null)" && [ -n "$line" ]; then |
| 76 | + break |
| 77 | + fi |
| 78 | + if ! alive; then |
| 79 | + # The result may have come just before the app stopped. |
| 80 | + if line="$(within 30 "$@" 2> /dev/null)" && [ -n "$line" ]; then |
| 81 | + break |
| 82 | + fi |
| 83 | + show_log |
| 84 | + echo "::error::The app stopped $((SECONDS - launched)) s after it was launched, before its start check wrote a result." |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + if [ "$SECONDS" -ge "$deadline" ]; then |
| 88 | + show_log |
| 89 | + echo "::error::The start check of the app wrote no result in the $seconds s of the start of the app, $((SECONDS - launched)) s of them after the app was launched." |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + sleep 1 |
| 93 | + done |
| 94 | + line="$(tr -d '\r' <<< "$line")" |
| 95 | + if [ "$line" != passed ]; then |
| 96 | + show_log |
| 97 | + echo "::error::The start check of the app $line" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + echo "The start check of the app passed $((SECONDS - launched)) s after the app was launched, $SECONDS s after the script started." |
| 101 | +} |
| 102 | + |
| 103 | +android() { |
| 104 | + local sdk="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}" |
| 105 | + if [ -z "$sdk" ]; then |
| 106 | + if [ "$(uname)" = Darwin ]; then |
| 107 | + sdk="$HOME/Library/Android/sdk" |
| 108 | + else |
| 109 | + sdk="$HOME/Android/Sdk" |
| 110 | + fi |
| 111 | + fi |
| 112 | + adb=("$sdk/platform-tools/adb" -s "$device") |
| 113 | + step 'flutter build apk' flutter build apk --debug -t "$check" |
| 114 | + # The application id and the activity that starts the app, from the APK, |
| 115 | + # with aapt2 of the newest build tools of the Android SDK. |
| 116 | + local apk=build/app/outputs/flutter-apk/app-debug.apk build_tools badging activity |
| 117 | + build_tools="$(ls "$sdk/build-tools" | sort -t . -k1,1n -k2,2n -k3,3n | tail -n 1)" |
| 118 | + badging="$("$sdk/build-tools/$build_tools/aapt2" dump badging "$apk")" |
| 119 | + id="$(sed -n "s/^package: name='\([^']*\)'.*/\1/p" <<< "$badging")" |
| 120 | + activity="$(sed -n "s/^launchable-activity: name='\([^']*\)'.*/\1/p" <<< "$badging")" |
| 121 | + if [ -z "$id" ] || [ -z "$activity" ]; then |
| 122 | + echo "$badging" |
| 123 | + echo "::error::aapt2 finds no application id or activity to start in $apk." |
| 124 | + exit 1 |
| 125 | + fi |
| 126 | + echo "Starting $id/$activity on $device." |
| 127 | + step 'adb install' "${adb[@]}" install -r "$apk" |
| 128 | + trap 'within 30 "${adb[@]}" shell am force-stop "$id" || true' EXIT |
| 129 | + # pm clear stops the app and deletes its data, with the result of an |
| 130 | + # earlier run. |
| 131 | + step 'adb shell pm clear' "${adb[@]}" shell pm clear "$id" |
| 132 | + within 30 "${adb[@]}" logcat -c || true |
| 133 | + step 'adb shell am start' "${adb[@]}" shell am start -W -n "$id/$activity" |
| 134 | + # The files of a debug app can be read with run-as, which starts in the |
| 135 | + # directory of its data. The temporary directory of Dart is code_cache/ |
| 136 | + # there: Flutter gives the engine the code cache of the app as its cache |
| 137 | + # directory (--cache-dir-path), which the Dart VM takes as systemTemp. |
| 138 | + wait_for_result "${adb[@]}" shell run-as "$id" cat "code_cache/$result" |
| 139 | + android_printed |
| 140 | +} |
| 141 | + |
| 142 | +# What the app printed: the lines of Flutter in the log of the device. |
| 143 | +android_printed() { |
| 144 | + within 60 "${adb[@]}" logcat -d -s flutter || true |
| 145 | +} |
| 146 | + |
| 147 | +android_alive() { |
| 148 | + [ -n "$(within 30 "${adb[@]}" shell pidof "$id" 2> /dev/null | tr -d '\r')" ] |
| 149 | +} |
| 150 | + |
| 151 | +# What the app printed, and the end of the log of the device, with the |
| 152 | +# crash of the app if it crashed. |
| 153 | +android_log() { |
| 154 | + echo "What the app printed:" |
| 155 | + android_printed |
| 156 | + echo "The end of the log of the device:" |
| 157 | + within 60 "${adb[@]}" logcat -d | tail -n 150 || true |
| 158 | +} |
| 159 | + |
| 160 | +ios() { |
| 161 | + # For the architecture of the Mac only, which the simulator runs, as |
| 162 | + # flutter run and flutter test build an app for a simulator: Flutter |
| 163 | + # passes FLUTTER_XCODE_ARCHS to Xcode as ARCHS. (With Xcode 27, Flutter |
| 164 | + # 3.44 fails to build for both: the lipo of Xcode 27 takes only one |
| 165 | + # architecture after -verify_arch.) |
| 166 | + step 'flutter build ios' env FLUTTER_XCODE_ARCHS="$(uname -m)" \ |
| 167 | + flutter build ios --simulator --debug -t "$check" |
| 168 | + local app=build/ios/iphonesimulator/Runner.app data launched |
| 169 | + id="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$app/Info.plist")" |
| 170 | + executable="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$app/Info.plist")" |
| 171 | + echo "Starting $id on $device." |
| 172 | + # A new install, without the data of an earlier run and its result. |
| 173 | + within 60 xcrun simctl terminate "$device" "$id" 2> /dev/null || true |
| 174 | + within 60 xcrun simctl uninstall "$device" "$id" 2> /dev/null || true |
| 175 | + step 'xcrun simctl install' xcrun simctl install "$device" "$app" |
| 176 | + trap 'within 60 xcrun simctl terminate "$device" "$id" 2> /dev/null || true' EXIT |
| 177 | + # The container of the data of the app, whose tmp/ is the temporary |
| 178 | + # directory of the app. |
| 179 | + data="$(within 60 xcrun simctl get_app_container "$device" "$id" data)" |
| 180 | + since="$(date '+%Y-%m-%d %H:%M:%S')" |
| 181 | + launched="$(within 60 xcrun simctl launch "$device" "$id")" || { |
| 182 | + echo "::error::xcrun simctl launch failed: $launched" |
| 183 | + exit 1 |
| 184 | + } |
| 185 | + # The app runs as a process of the Mac: simctl prints `<id>: <pid>`. |
| 186 | + pid="${launched##*: }" |
| 187 | + wait_for_result cat "$data/tmp/$result" |
| 188 | + ios_printed |
| 189 | +} |
| 190 | + |
| 191 | +# What the app printed: the lines of Flutter in the log of the simulator. |
| 192 | +ios_printed() { |
| 193 | + within 60 xcrun simctl spawn "$device" log show --style compact --start "$since" \ |
| 194 | + --predicate "process == \"$executable\" AND eventMessage BEGINSWITH \"flutter: \"" || true |
| 195 | +} |
| 196 | + |
| 197 | +ios_alive() { |
| 198 | + kill -0 "$pid" 2> /dev/null |
| 199 | +} |
| 200 | + |
| 201 | +# What the app printed, the end of its log, and its crash report if it |
| 202 | +# crashed. |
| 203 | +ios_log() { |
| 204 | + echo "What the app printed:" |
| 205 | + ios_printed |
| 206 | + echo "The end of the log of the app:" |
| 207 | + within 60 xcrun simctl spawn "$device" log show --style compact --start "$since" \ |
| 208 | + --predicate "process == \"$executable\"" | tail -n 100 || true |
| 209 | + local report |
| 210 | + report="$(find "$HOME/Library/Logs/DiagnosticReports" -name "$executable-*.ips" -newermt "$since" 2> /dev/null | head -n 1)" |
| 211 | + if [ -n "$report" ]; then |
| 212 | + echo "The crash report $report:" |
| 213 | + head -n 120 "$report" |
| 214 | + fi |
| 215 | +} |
| 216 | + |
| 217 | +alive() { "${platform}_alive"; } |
| 218 | +show_log() { "${platform}_log"; } |
| 219 | + |
| 220 | +"$platform" |
0 commit comments