d3ro-voice/scripts/ci/run-mobile-csprng-instrumentation.sh
2026-08-29 18:33:45 +09:00

75 lines
2.2 KiB
Bash

#!/usr/bin/env bash
set -Eeuo pipefail
APP_PACKAGE='com.d3ro.voice'
TEST_PACKAGE='com.d3ro.voice.test'
TEST_RUNNER='androidx.test.runner.AndroidJUnitRunner'
TEST_CLASS='com.d3ro.voice.CsprngInstrumentedTest'
APP_APK="${1:-}"
TEST_APK="${2:-}"
OUTPUT_PATH="${3:-apps/mobile-rn/.maestro-output/csprng-instrumentation.txt}"
if [[ -z "$APP_APK" || ! -f "$APP_APK" ]]; then
echo 'A debug app APK path is required.' >&2
exit 2
fi
if [[ -z "$TEST_APK" || ! -f "$TEST_APK" ]]; then
echo 'A debug AndroidTest APK path is required.' >&2
exit 2
fi
if ! command -v adb >/dev/null 2>&1; then
echo 'adb is required.' >&2
exit 2
fi
connected_devices="$(adb devices | awk '$2 == "device" { count += 1 } END { print count + 0 }')"
if [[ "$connected_devices" -ne 1 && -z "${ANDROID_SERIAL:-}" ]]; then
echo "Expected exactly one Android device, found $connected_devices. Set ANDROID_SERIAL explicitly." >&2
exit 2
fi
adb get-state | grep -Fx 'device' >/dev/null
mkdir -p "$(dirname "$OUTPUT_PATH")"
remove_if_installed() {
local package_name="$1"
if adb shell pm path "$package_name" | grep -F 'package:' >/dev/null 2>&1; then
adb uninstall "$package_name" >/dev/null
fi
}
cleanup() {
set +e
remove_if_installed "$TEST_PACKAGE"
remove_if_installed "$APP_PACKAGE"
}
trap cleanup EXIT
remove_if_installed "$TEST_PACKAGE"
remove_if_installed "$APP_PACKAGE"
adb install --no-streaming "$APP_APK"
adb install --no-streaming "$TEST_APK"
set +e
instrumentation_output="$(
adb shell am instrument -w -r \
-e class "$TEST_CLASS" \
"$TEST_PACKAGE/$TEST_RUNNER" 2>&1
)"
instrumentation_exit="$?"
set -e
instrumentation_output="${instrumentation_output//$'\r'/}"
printf '%s\n' "$instrumentation_output" | tee "$OUTPUT_PATH"
if [[ "$instrumentation_exit" -ne 0 ]]; then
echo "CSPRNG instrumentation exited with $instrumentation_exit." >&2
exit "$instrumentation_exit"
fi
if ! grep -Eq '^OK \(1 test\)$' <<<"$instrumentation_output"; then
echo 'CSPRNG instrumentation did not report exactly one passing test.' >&2
exit 1
fi
if ! grep -Eq '^INSTRUMENTATION_CODE: -1$' <<<"$instrumentation_output"; then
echo 'CSPRNG instrumentation did not report a successful runner exit.' >&2
exit 1
fi