|
| 1 | +#!/bin/bash |
| 2 | +##===----------------------------------------------------------------------===## |
| 3 | +## |
| 4 | +## This source file is part of the Swift.org open source project |
| 5 | +## |
| 6 | +## Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 7 | +## Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +## |
| 9 | +## See https://swift.org/LICENSE.txt for license information |
| 10 | +## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | +## |
| 12 | +##===----------------------------------------------------------------------===## |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +log() { printf -- "** %s\n" "$*" >&2; } |
| 17 | +error() { printf -- "** ERROR: %s\n" "$*" >&2; } |
| 18 | +fatal() { error "$@"; exit 1; } |
| 19 | + |
| 20 | +ANDROID_API=28 |
| 21 | +ANDROID_EMULATOR_ARCH="x86_64" |
| 22 | +EMULATOR_SPEC="system-images;android-${ANDROID_API};default;${ANDROID_EMULATOR_ARCH}" |
| 23 | +EMULATOR_NAME="swiftemu" |
| 24 | +ANDROID_PROFILE="Pixel 6" |
| 25 | + |
| 26 | +# see if the Android SDK is even installed |
| 27 | +echo "CHECKING FOR ANDROID SDK" |
| 28 | +find / -name sdkmanager || true |
| 29 | +echo "DONE CHECKING FOR ANDROID SDK" |
| 30 | + |
| 31 | +# install and start an Android emulator |
| 32 | +sdkmanager --list_installed |
| 33 | +yes | sdkmanager --licenses > /dev/null |
| 34 | +sdkmanager --install "${EMULATOR_SPEC}" "emulator" "platform-tools" "platforms;android-${ANDROID_API}" |
| 35 | +avdmanager create avd -n "${EMULATOR_NAME}" -k "${EMULATOR_SPEC}" --device "${ANDROID_PROFILE}" |
| 36 | +emulator -list-avds |
| 37 | +# launch the emulator in the background; we will cat the logs at the end |
| 38 | +nohup emulator -memory 4096 -avd "${EMULATOR_NAME}" -wipe-data -no-window -no-snapshot -noaudio -no-boot-anim 2>&1 > emulator.log & |
| 39 | +adb logcat 2>&1 > logcat.log & |
| 40 | + |
| 41 | +# create a staging folder where we copy the test executable |
| 42 | +# and all the dependent libraries to copy over to the emulator |
| 43 | +STAGING="android-test-${PACKAGE}" |
| 44 | +rm -rf .build/"${STAGING}" |
| 45 | +mkdir .build/"${STAGING}" |
| 46 | + |
| 47 | +# for the common case of tests referencing their own files as hardwired resource paths |
| 48 | +if [[ -d Tests ]]; then |
| 49 | + cp -a Tests .build/"${STAGING}" |
| 50 | +fi |
| 51 | + |
| 52 | +cd .build/ |
| 53 | +cp -a debug/*.xctest "${STAGING}" |
| 54 | +cp -a debug/*.resources "${STAGING}" || true |
| 55 | +cp -a ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/*/sysroot/usr/lib/${ANDROID_EMULATOR_ARCH_TRIPLE}-linux-android/libc++_shared.so "${STAGING}" |
| 56 | +cp -a ${SWIFT_ANDROID_SDK_HOME}/swift-android/swift-resources/usr/lib/swift-${ANDROID_EMULATOR_ARCH_TRIPLE}/android/*.so "${STAGING}" |
| 57 | + |
| 58 | +adb push ${STAGING} /data/local/tmp/ |
| 59 | + |
| 60 | +cd - |
| 61 | + |
| 62 | +TEST_CMD="./${PACKAGE}PackageTests.xctest" |
| 63 | +TEST_SHELL="cd /data/local/tmp/${STAGING}" |
| 64 | +TEST_SHELL="${TEST_SHELL} && ${TEST_CMD}" |
| 65 | + |
| 66 | +# Run test cases a second time with the Swift Testing library |
| 67 | +# We additionally need to handle the special exit code EXIT_NO_TESTS_FOUND (69 on Android), |
| 68 | +# which can happen when the tests link to Testing, but no tests are executed |
| 69 | +# see: https://github.com/swiftlang/swift-package-manager/blob/1b593469e8ad3daf2cc10e798340bd2de68c402d/Sources/Commands/SwiftTestCommand.swift#L1542 |
| 70 | +TEST_SHELL="${TEST_SHELL} && ${TEST_CMD} --testing-library swift-testing && [ \$? -eq 0 ] || [ \$? -eq 69 ]" |
| 71 | + |
| 72 | +# run the test executable |
| 73 | +adb shell "${TEST_SHELL}" |
| 74 | + |
0 commit comments