Skip to content

Commit 5f227e3

Browse files
committed
fix(ci): start the apps on devices with adb and simctl instead of flutter test
flutter test must reach the VM service of the app it installs, and on the emulators and simulators of CI it sometimes never did: the first nightly run hung in 4 of 38 steps that start an app, on iOS simulators through a race of flutter_tools (flutter/flutter#181771). The retry of .github/scripts/start_app_test.sh only made that less likely. .github/scripts/start_app.sh replaces it. It builds a debug app with the start check of flutter_core as its entry, installs it anew, launches it with adb or xcrun simctl and reads the result that the check writes to its temporary directory: code_cache/ of the app on Android, read with run-as, and tmp/ of the data container on an iOS simulator. It finds the application id and the activity to start with aapt2 in the APK, and the bundle id in the Info.plist of the app. It makes one try and fails with the log of the app, and the crash report on iOS, when the check fails, when the app stops before the check writes its result, and when no result comes in time, so the steps that start the apps get the timeouts of one try.
1 parent 8920ec8 commit 5f227e3

5 files changed

Lines changed: 248 additions & 62 deletions

File tree

‎.github/scripts/start_app.sh‎

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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"

‎.github/scripts/start_app_test.sh‎

Lines changed: 0 additions & 40 deletions
This file was deleted.

‎.github/workflows/apps.yml‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,23 @@ jobs:
229229
cat "$RUNNER_TEMP/emulator.log"
230230
exit 1
231231
232-
# The test that the app starts is a test of flutter_core in
233-
# app_tests/start, which the matrix tool adds to the app as it adds it
234-
# to the apps of the matrix, where it is only analyzed.
235-
# .github/scripts/start_app_test.sh runs it, giving a try that never
236-
# reaches the app on the emulator or simulator one more try.
232+
# The check that the app starts is an entry of the app that
233+
# flutter_core keeps in app_tests/start, which the matrix tool adds to
234+
# the app as it adds it to the apps of the matrix, where it is only
235+
# analyzed. .github/scripts/start_app.sh builds the app with it,
236+
# starts the app with adb and reads the result that the check writes,
237+
# in one try: unlike flutter test, it does not connect to the app.
237238
- name: Start an app without Firebase on the Android emulator
238239
if: ${{ !cancelled() && matrix.registry == 'real' }}
239-
timeout-minutes: 25
240+
timeout-minutes: 12
240241
run: |
241242
dart run packages/smf_flutter_cli/bin/smf_flutter.dart create plain_app \
242243
-m go_router,home,bottom_tabs,riverpod,get_it,event_bus \
243244
-o "$RUNNER_TEMP/SMF apps" --no-input --skip-external-setup --no-dart-fix --strict
244245
dart run packages/smf_flutter_cli/tool/matrix.dart --add-app-tests \
245246
"$RUNNER_TEMP/SMF apps/plain_app" packages/smf_modules/smf_flutter_core/app_tests/start
246247
cd "$RUNNER_TEMP/SMF apps/plain_app"
247-
"$GITHUB_WORKSPACE/.github/scripts/start_app_test.sh" emulator-5554 480
248+
"$GITHUB_WORKSPACE/.github/scripts/start_app.sh" android emulator-5554 480
248249
249250
# An app with Firebase starts only once flutterfire configured it: the
250251
# placeholder of its options throws at start-up. flutterfire writes
@@ -300,9 +301,9 @@ jobs:
300301

301302
- name: Start the app with every module on the Android emulator
302303
if: ${{ !cancelled() && steps.firebase_android.outcome == 'success' }}
303-
timeout-minutes: 20
304+
timeout-minutes: 10
304305
working-directory: ${{ runner.temp }}/SMF apps/firebase_start_app
305-
run: '"$GITHUB_WORKSPACE/.github/scripts/start_app_test.sh" emulator-5554 480'
306+
run: '"$GITHUB_WORKSPACE/.github/scripts/start_app.sh" android emulator-5554 480'
306307

307308
- name: Leave out the app with Firebase without the key
308309
if: ${{ !cancelled() && matrix.registry == 'real' && env.HAS_FIREBASE_KEY != 'true' }}
@@ -484,18 +485,22 @@ jobs:
484485
xcrun simctl bootstatus "$udid" -b
485486
echo "SIMULATOR=$udid" >> "$GITHUB_ENV"
486487
487-
# The test that the app starts is a test of flutter_core in
488-
# app_tests/start, which the matrix tool adds to the app as it adds it
489-
# to the apps of the matrix, where it is only analyzed.
488+
# The check that the app starts is an entry of the app that
489+
# flutter_core keeps in app_tests/start, which the matrix tool adds to
490+
# the app as it adds it to the apps of the matrix, where it is only
491+
# analyzed. .github/scripts/start_app.sh builds the app with it,
492+
# starts the app with simctl and reads the result that the check
493+
# writes, in one try: unlike flutter test, it does not connect to the
494+
# app.
490495
- name: Start the app without Firebase on the iOS simulator
491496
if: ${{ !cancelled() && env.SIMULATOR != '' }}
492-
timeout-minutes: 25
497+
timeout-minutes: 15
493498
run: |
494499
app="$RUNNER_TEMP/SMF apps застосунки é/plain_app"
495500
dart run packages/smf_flutter_cli/tool/matrix.dart --add-app-tests \
496501
"$app" packages/smf_modules/smf_flutter_core/app_tests/start
497502
cd "$app"
498-
"$GITHUB_WORKSPACE/.github/scripts/start_app_test.sh" "$SIMULATOR" 600
503+
"$GITHUB_WORKSPACE/.github/scripts/start_app.sh" ios "$SIMULATOR" 600
499504
500505
# An app of its own rather than firebase_app, whose Xcode project has
501506
# the build phase for Crashlytics of the test of the archive:
@@ -546,9 +551,9 @@ jobs:
546551

547552
- name: Start the app with every module on the iOS simulator
548553
if: ${{ !cancelled() && env.SIMULATOR != '' && steps.firebase_ios.outcome == 'success' }}
549-
timeout-minutes: 25
554+
timeout-minutes: 13
550555
working-directory: ${{ runner.temp }}/SMF apps застосунки é/firebase_start_app
551-
run: '"$GITHUB_WORKSPACE/.github/scripts/start_app_test.sh" "$SIMULATOR" 600'
556+
run: '"$GITHUB_WORKSPACE/.github/scripts/start_app.sh" ios "$SIMULATOR" 600'
552557

553558
- name: Leave out the app with Firebase without the key
554559
if: ${{ !cancelled() && env.HAS_FIREBASE_KEY != 'true' }}

0 commit comments

Comments
 (0)