mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
Improve Android testing scripts
Summary: The goal of this pull request is to make it easier for contributors to run Android tests locally, specifically the unit tests and integration tests. I added a bunch of checks to the local testing scripts that will warn you if your environment is misconfigured, and tell you how to fix it. I also updated the testing docs, so that the regular "Testing" page should be a decent resource to point people to when you are telling them "hey this pull request needs a test." Just Android, though, I haven't gotten to the iOS parts yet. I also disabled a couple tests that seemed quite flaky while running on a local machine, and don't seem to be providing much value. In particular, the `TestId` test just hangs on my emulator a lot and has been flaky on CI in the past, so I removed about half of its test cases to make the sample app smaller. The testMetions test appears to be dependent on screen size so I commented it out. Closes https://github.com/facebook/react-native/pull/11442 Differential Revision: D4323569 Pulled By: bestander fbshipit-source-id: 9c869f3915d5c7cee438615f37986b07ab251f8c
This commit is contained in:
committed by
Facebook Github Bot
parent
3e6d762ab7
commit
affd5ac681
19
scripts/run-android-emulator.sh
Executable file
19
scripts/run-android-emulator.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Runs an Android emulator locally.
|
||||
# If there already is a running emulator, this just uses that.
|
||||
# The only reason to use this config is that it represents a known-good
|
||||
# virtual device configuration.
|
||||
# This is useful for running integration tests on a local machine.
|
||||
# TODO: make continuous integration use the precise same setup
|
||||
|
||||
STATE=`adb get-state`
|
||||
|
||||
if [ -n "$STATE" ]; then
|
||||
echo "An emulator is already running."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Creating virtual device..."
|
||||
echo no | android create avd -n testAVD -f -t android-23 --abi default/x86
|
||||
emulator -avd testAVD
|
||||
@@ -3,19 +3,21 @@
|
||||
# Runs all Android integration tests locally.
|
||||
# See http://facebook.github.io/react-native/docs/testing.html
|
||||
|
||||
set -e
|
||||
source $(dirname $0)/validate-android-sdk.sh
|
||||
source $(dirname $0)/validate-android-test-env.sh
|
||||
source $(dirname $0)/validate-android-device-env.sh
|
||||
|
||||
which buck > /dev/null || {
|
||||
echo "React Native uses the Buck build tool to run tests. Please install Buck: https://buckbuild.com/setup/install.html";
|
||||
exit 1;
|
||||
}
|
||||
set -e
|
||||
|
||||
echo "Compiling native code..."
|
||||
./gradlew :ReactAndroid:packageReactNdkLibsForBuck
|
||||
|
||||
echo "Building JS bundle..."
|
||||
node local-cli/cli.js bundle --platform android --dev true --entry-file ReactAndroid/src/androidTest/js/TestBundle.js --bundle-output ReactAndroid/src/androidTest/assets/AndroidTestBundle.js
|
||||
|
||||
echo "Installing test app on the device..."
|
||||
buck fetch ReactAndroid/src/androidTest/buck-runner:instrumentation-tests
|
||||
buck install ReactAndroid/src/androidTest/buck-runner:instrumentation-tests
|
||||
|
||||
echo "Running integration tests..."
|
||||
adb shell am instrument -w com.facebook.react.tests/android.support.test.runner.AndroidJUnitRunner
|
||||
|
||||
@@ -3,14 +3,13 @@
|
||||
# Runs all Android unit tests locally.
|
||||
# See http://facebook.github.io/react-native/docs/testing.html
|
||||
|
||||
set -e
|
||||
source $(dirname $0)/validate-android-sdk.sh
|
||||
source $(dirname $0)/validate-android-test-env.sh
|
||||
|
||||
which buck > /dev/null || {
|
||||
echo "React Native uses the Buck build tool to run tests. Please install Buck: https://buckbuild.com/setup/install.html";
|
||||
exit 1;
|
||||
}
|
||||
set -e
|
||||
|
||||
echo "Fetching dependencies..."
|
||||
buck fetch ReactAndroid/src/test/...
|
||||
|
||||
echo "Running unit tests..."
|
||||
buck test ReactAndroid/src/test/...
|
||||
|
||||
33
scripts/validate-android-device-env.sh
Normal file
33
scripts/validate-android-device-env.sh
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script validates that the Android environment is set up to run
|
||||
# tests on a device or emulator (as opposed to a plain Java environment).
|
||||
|
||||
# This requires that the Android NDK is set up correctly and it also
|
||||
# requires that you are currently either running an emulator or have
|
||||
# an Android device plugged in.
|
||||
|
||||
if [ -z "$ANDROID_NDK" ]; then
|
||||
echo "Error: \$ANDROID_NDK is not configured."
|
||||
echo "You must first install the Android NDK and then set \$ANDROID_NDK."
|
||||
echo "If you already installed the Android SDK, well, the NDK is a different thing that you also need to install."
|
||||
echo "See https://facebook.github.io/react-native/docs/android-building-from-source.html for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$(adb get-state)" ]; then
|
||||
echo "Error: you must either run an emulator or connect a device."
|
||||
echo "You can check what devices are running with 'adb get-state'."
|
||||
echo "You can run scripts/run-android-emulator.sh to get a known-good emulator config."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while :
|
||||
do
|
||||
BOOTANIM=`adb -e shell getprop init.svc.bootanim`
|
||||
if [ -n `echo $BOOTANIM | grep stopped` ]; then
|
||||
break
|
||||
fi
|
||||
echo "Waiting for the emulator to finish booting..."
|
||||
sleep 3
|
||||
done
|
||||
51
scripts/validate-android-sdk.sh
Executable file
51
scripts/validate-android-sdk.sh
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script validates that the Android SDK is installed correctly.
|
||||
# This means setting ANDROID_HOME and adding its subdirectories to PATH.
|
||||
# If the Android SDK is not installed correctly, this script exits
|
||||
# with an error and a helpful message is printed.
|
||||
|
||||
if [ -z "$ANDROID_HOME" ]; then
|
||||
echo "Error: \$ANDROID_HOME is not configured."
|
||||
echo "You must first install the Android SDK and then set \$ANDROID_HOME."
|
||||
echo "If you already installed the Android SDK, the problem is that you need to export ANDROID_HOME from your .bashrc or equivalent."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -d "$ANDROID_HOME" ]; then
|
||||
echo "Error: \$ANDROID_HOME = $ANDROID_HOME but that directory does not exist."
|
||||
echo "It is possible that you installed then uninstalled the Android SDK."
|
||||
echo "In that case, you should reinstall it."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -e "$ANDROID_HOME/tools/emulator" ]; then
|
||||
echo "Error: could not find an emulator at \$ANDROID_HOME/tools/emulator."
|
||||
echo "Specifically, $ANDROID_HOME/tools/emulator does not exist."
|
||||
echo "This indicates something is borked with your Android SDK install."
|
||||
echo "One possibility is that you have \$ANDROID_HOME set to the wrong value."
|
||||
echo "If that seems correct, you might want to try reinstalling the Android SDK."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z `which emulator` ]; then
|
||||
echo "Error: could not find 'emulator'. Specifically, 'which emulator' was empty."
|
||||
echo "However, the emulator seems to be installed at \$ANDROID_HOME/tools/emulator already."
|
||||
echo "This means that the problem is that you are not adding \$ANDROID_HOME/tools to your \$PATH."
|
||||
echo "You should do that, and then rerun this command."
|
||||
echo "Sorry for not fixing this automatically - we just didn't want to mess with your \$PATH automatically because that can break things."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z `which adb` ]; then
|
||||
echo "Error: could not find 'adb'. Specifically, 'which adb' was empty."
|
||||
echo "This indicates something is borked with your Android SDK install."
|
||||
echo "The most likely problem is that you are not adding \$ANDROID_HOME/platform-tools to your \$PATH."
|
||||
echo "If all else fails, try reinstalling the Android SDK."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
97
scripts/validate-android-test-env.sh
Executable file
97
scripts/validate-android-test-env.sh
Executable file
@@ -0,0 +1,97 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script validates that Android is set up correctly for the
|
||||
# testing environment.
|
||||
#
|
||||
# In particular, the config in ReactAndroid/build.gradle must match
|
||||
# the android sdk that is actually installed. Also, we must have the
|
||||
# right version of Java.
|
||||
|
||||
# Check that Buck is working.
|
||||
if [ -z "$(which buck)" ]; then
|
||||
echo "You need to install Buck."
|
||||
echo "See https://buckbuild.com/setup/install.htm for instructions."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$(buck --version)" ]; then
|
||||
echo "Your Buck install is broken."
|
||||
|
||||
if [ -d "/opt/facebook" ]; then
|
||||
SUGGESTED="ff27d5270ecaa92727cd5a19954e62298fa78f09"
|
||||
echo "FB laptops ship with a Buck config that is not compatible with open "
|
||||
echo "source. FB Buck requires the environment to set a buck version, but "
|
||||
echo "the open source version of Buck forbids that."
|
||||
echo
|
||||
echo "You can try setting:"
|
||||
echo
|
||||
echo "export BUCKVERSION=${SUGGESTED}"
|
||||
echo
|
||||
echo "in your .bashrc or .bash_profile to fix this."
|
||||
echo
|
||||
echo "If you don't want to alter BUCKVERSION for other things running on"
|
||||
echo "your machine, you can just scope it to a single script, for example"
|
||||
echo "by running something like:"
|
||||
echo
|
||||
echo "BUCKVERSION=${SUGGESTED} $0"
|
||||
echo
|
||||
else
|
||||
echo "I don't know what's wrong, but calling 'buck --version' should work."
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# BUILD_TOOLS_VERSION is in a format like "23.0.1"
|
||||
BUILD_TOOLS_VERSION=`grep buildToolsVersion $(dirname $0)/../ReactAndroid/build.gradle | sed 's/^[^"]*\"//' | sed 's/"//'`
|
||||
|
||||
# MAJOR is something like "23"
|
||||
MAJOR=`echo $BUILD_TOOLS_VERSION | sed 's/\..*//'`
|
||||
|
||||
# Check that we have the right major version of the Android SDK.
|
||||
PLATFORM_DIR="$ANDROID_HOME/platforms/android-$MAJOR"
|
||||
if [ ! -e "$PLATFORM_DIR" ]; then
|
||||
echo "Error: could not find version $ANDROID_VERSION of the Android SDK."
|
||||
echo "Specifically, the directory $PLATFORM_DIR does not exist."
|
||||
echo "You probably need to specify the right version using the SDK Manager from within Android Studio."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for details."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check that we have the right version of the build tools.
|
||||
BT_DIR="$ANDROID_HOME/build-tools/$BUILD_TOOLS_VERSION"
|
||||
if [ ! -e "$BT_DIR" ]; then
|
||||
echo "Error: could not find version $BUILD_TOOLS_VERSION of the Android build tools."
|
||||
echo "Specifically, the directory $BT_DIR does not exist."
|
||||
echo "You probably need to explicitly install the correct version of the Android SDK Build Tools from within Android Studio."
|
||||
echo "See https://facebook.github.io/react-native/docs/getting-started.html for details."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -n "$(which csrutil)" ]; then
|
||||
# This is a SIP-protected machine (recent OSX).
|
||||
# Check that we are not using SIP-protected Java.
|
||||
JAVA=`which java`
|
||||
if [ "$JAVA" = "/usr/bin/java" ]; then
|
||||
echo "Error: we can't use this Java version."
|
||||
echo "Currently, Java runs from $JAVA."
|
||||
echo "The operating-system-provided Java doesn't work with React Native because of SIP protection."
|
||||
echo "Please install the Oracle Java Development Kit 8."
|
||||
if [ -d "/opt/facebook" ]; then
|
||||
echo "See https://our.intern.facebook.com/intern/dex/installing-java-8/ for instructions on installing Java 8 on FB laptops."
|
||||
else
|
||||
echo "Check out http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ."
|
||||
echo "Be sure that you set JAVA_HOME and PATH correctly."
|
||||
fi
|
||||
echo "After installing Java, run 'buck kill' and 'buck clean'."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
echo "Error: \$JAVA_HOME is not configured."
|
||||
echo "Try adding export JAVA_HOME=\$(/usr/libexec/java_home) to your .bashrc or equivalent."
|
||||
echo "You will also want to add \$JAVA_HOME/bin to your path."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user