realm-js/scripts/test.sh

425 lines
12 KiB
Bash
Raw Normal View History

2015-12-17 20:42:02 +00:00
#!/bin/bash
set -o pipefail
set -e
2016-08-19 17:06:45 +00:00
export TEST_SCRIPT=1
export NPM_CONFIG_PROGRESS=false
TARGET=$1
CONFIGURATION=${2:-Release}
2017-03-16 11:15:20 +00:00
if echo $CONFIGURATION | grep -i "^Debug$" > /dev/null ; then
CONFIGURATION="Debug"
fi
IOS_SIM_DEVICE=${IOS_SIM_DEVICE:-} # use preferentially, otherwise will be set and re-exported
ios_sim_default_device_type=${IOS_SIM_DEVICE_TYPE:-iPhone 5s}
ios_sim_default_ios_version=${IOS_SIM_OS:-iOS 10.1}
ACCEPTED_LICENSES='MIT, ISC, BSD, Apache-2.0, BSD-2-Clause, BSD-3-Clause, WTFPL, Unlicense, (MIT AND CC-BY-3.0)'
PATH="/opt/android-sdk-linux/platform-tools:$PATH"
SRCROOT=$(cd "$(dirname "$0")/.." && pwd)
2017-01-02 12:35:45 +00:00
XCPRETTY=$(which xcpretty || true)
CI_RUN=false
if [ -n "${JENKINS_HOME}" ]; then
CI_RUN=true
fi
2016-01-22 21:32:56 +00:00
# Start current working directory at the root of the project.
cd "$SRCROOT"
# Add node_modules to PATH just in case we weren't called from `npm test`
PATH="$PWD/node_modules/.bin:$PATH"
if [[ $TARGET = *-android ]]; then
# Inform the prepublish script to build Android modules.
export REALM_BUILD_ANDROID=1
2016-01-22 21:30:16 +00:00
fi
SERVER_PID=0
PACKAGER_OUT="$SRCROOT/packager_out.txt"
LOGCAT_OUT="$SRCROOT/logcat_out.txt"
download_server() {
sh ./scripts/download-object-server.sh
}
start_server() {
sh ./object-server-for-testing/start-object-server.command &
SERVER_PID=$!
}
stop_server() {
2017-01-02 12:35:45 +00:00
if [[ ${SERVER_PID} -gt 0 ]] ; then
kill -9 ${SERVER_PID}
fi
}
startedSimulator=false
log_temp=
test_temp_dir=
cleanup() {
# Kill started object server
stop_server || true
2017-01-02 12:35:45 +00:00
# Quit Simulator.app to give it a chance to go down gracefully
if $startedSimulator; then
osascript -e 'tell app "Simulator" to quit without saving' || true
sleep 0.25 # otherwise the pkill following will get it too early
fi
2017-01-02 12:35:45 +00:00
# Kill all child processes.
pkill -9 -P $$ || true
# Kill react native packager
pkill -x node || true
rm -f "$PACKAGER_OUT" "$LOGCAT_OUT"
2017-01-02 12:35:45 +00:00
# Cleanup temp files
if [ -n "$log_temp" ] && [ -e "$log_temp" ]; then
rm "$log_temp" || true
fi
if [ -n "$test_temp_dir" ] && [ -e "$test_temp_dir" ]; then
rm -rf "$test_temp_dir" || true
fi
}
open_chrome() {
local dir
for dir in "$HOME/Applications" "/Applications"; do
if [ -d "$dir/Google Chrome.app" ]; then
open "$dir/Google Chrome.app"
break
fi
2015-12-17 18:56:08 +00:00
done
}
start_packager() {
2016-03-16 20:30:06 +00:00
watchman watch-del-all || true
./node_modules/react-native/packager/packager.sh | tee "$PACKAGER_OUT" &
while :; do
if grep -Fxq "React packager ready." "$PACKAGER_OUT"; then
break
else
echo "Waiting for packager."
sleep 2
fi
done
2016-01-26 20:23:26 +00:00
}
2016-05-13 23:30:57 +00:00
xctest() {
setup_ios_simulator
2017-01-02 12:35:45 +00:00
# - Wait until the simulator is fully booted by waiting for it to launch SpringBoard
printf "Waiting for springboard to ensure device is ready..."
xcrun simctl launch "$IOS_SIM_DEVICE" com.apple.springboard 1>/dev/null 2>/dev/null || true
echo " done"
2017-01-02 12:35:45 +00:00
# - Run the build and test
2016-08-19 17:06:45 +00:00
if [ -n "$XCPRETTY" ]; then
2017-01-02 12:35:45 +00:00
log_temp=$(mktemp build.log.XXXXXX)
if [ -e "$log_temp" ]; then
rm "$log_temp"
fi
xcrun xcodebuild -scheme "$1" -configuration "$CONFIGURATION" -sdk iphonesimulator -destination name="iPhone 5s" build test 2>&1 | tee "$log_temp" | "$XCPRETTY" -c --no-utf --report junit --output build/reports/junit.xml || {
EXITCODE=$?
2017-01-02 12:35:45 +00:00
printf "*** Xcode Failure (exit code %s). The full xcode log follows: ***\n\n" "$EXITCODE"
cat "$log_temp"
printf "\n\n*** End Xcode Failure ***\n"
exit $EXITCODE
2016-11-10 18:06:48 +00:00
}
rm "$log_temp"
2016-08-19 17:06:45 +00:00
else
xcrun xcodebuild -scheme "$1" -configuration "$CONFIGURATION" -sdk iphonesimulator -destination id="$IOS_SIM_DEVICE" build test || {
EXITCODE=$?
echo "*** Failure (exit code $EXITCODE). ***"
exit $EXITCODE
}
fi
}
setup_ios_simulator() {
# - Ensure one version of xcode is chosen by all tools
if [[ -z "$DEVELOPER_DIR" ]]; then
2017-01-02 12:35:45 +00:00
DEV_DIR="$(xcode-select -p)"
export DEVELOPER_DIR=$DEV_DIR
fi
2017-01-02 12:35:45 +00:00
# -- Ensure that the simulator is ready
2017-01-02 12:35:45 +00:00
if [ $CI_RUN == true ]; then
# - Kill the Simulator to ensure we are running the correct one, only when running in CI
echo "Resetting simulator using toolchain from: $DEVELOPER_DIR"
2017-01-02 12:35:45 +00:00
# Quit Simulator.app to give it a chance to go down gracefully
local deadline=$((SECONDS+5))
while pgrep -qx Simulator && [ $SECONDS -lt $deadline ]; do
osascript -e 'tell app "Simulator" to quit without saving' || true
2017-01-02 12:35:45 +00:00
sleep 0.25 # otherwise the pkill following will get it too early
done
2017-01-02 12:35:45 +00:00
# stop CoreSimulatorService
launchctl remove com.apple.CoreSimulator.CoreSimulatorService 2>/dev/null || true
sleep 0.25 # launchtl can take a small moment to kill services
2017-01-02 12:35:45 +00:00
# kill them with fire
while pgrep -qx Simulator com.apple.CoreSimulator.CoreSimulatorService; do
pkill -9 -x Simulator com.apple.CoreSimulator.CoreSimulatorService || true
sleep 0.05
done
2017-01-02 12:35:45 +00:00
# - Prod `simctl` a few times as sometimes it fails the first couple of times after switching XCode vesions
local deadline=$((SECONDS+5))
2017-01-02 12:35:45 +00:00
while [ -z "$(xcrun simctl list devices 2>/dev/null)" ] && [ $SECONDS -lt $deadline ]; do
: # nothing to see here, will stop cycling on the first successful run
done
2017-01-02 12:35:45 +00:00
# - Choose a device, if it has not already been chosen
local deadline=$((SECONDS+5))
while [ -z "$IOS_SIM_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
2017-01-02 12:35:45 +00:00
IOS_DEVICE=$(ruby -rjson -e "puts JSON.parse(%x{xcrun simctl list devices --json})['devices'].each{|os,group| group.each{|dev| dev['os'] = os}}.flat_map{|x| x[1]}.select{|x| x['availability'] == '(available)'}.each{|x| x['score'] = (x['name'] == '$ios_sim_default_device_type' ? 1 : 0) + (x['os'] == '$ios_sim_default_ios_version' ? 1 : 0)}.sort_by!{|x| [x['score'], x['name']]}.reverse![0]['udid']")
export IOS_SIM_DEVICE=$IOS_DEVICE
done
if [ -z "$IOS_SIM_DEVICE" ]; then
echo "*** Failed to determine the iOS Simulator device to use ***"
exit 1
fi
2017-01-02 12:35:45 +00:00
# - Reset the device we will be using if running in CI
xcrun simctl shutdown "$IOS_SIM_DEVICE" 1>/dev/null 2>/dev/null || true # sometimes simctl gets confused
xcrun simctl erase "$IOS_SIM_DEVICE"
2017-01-02 12:35:45 +00:00
# - Start the target in Simulator.app
# Note: as of Xcode 7.3.1 `simctl` can not completely boot a simulator, specifically it can not bring up backboard, so GUI apps can not run.
# This is fixed in version 8 of Xcode, but we still need the compatibility
2017-01-02 12:35:45 +00:00
"$DEVELOPER_DIR/Applications/Simulator.app/Contents/MacOS/Simulator" -CurrentDeviceUDID "$IOS_SIM_DEVICE" & # will get killed with all other children at exit
startedSimulator=true
2017-01-02 12:35:45 +00:00
else
# - ensure that the simulator is running on a developer's workstation
open "$DEVELOPER_DIR/Applications/Simulator.app"
2017-01-02 12:35:45 +00:00
# - Select the first device booted in the simulator, since it will boot something for us
local deadline=$((SECONDS+10))
while [ -z "$IOS_SIM_DEVICE" ] && [ $SECONDS -lt $deadline ]; do
2017-01-02 12:35:45 +00:00
IOS_DEVICE=$(ruby -rjson -e "puts JSON.parse(%x{xcrun simctl list devices --json})['devices'].each{|os,group| group.each{|dev| dev['os'] = os}}.flat_map{|x| x[1]}.select{|x| x['state'] == 'Booted'}[0]['udid']")
export IOS_SIM_DEVICE=$IOS_DEVICE
done
if [ -z "$IOS_SIM_DEVICE" ]; then
echo "*** Failed to determine the iOS Simulator device in use ***"
exit 1
fi
2016-08-19 17:06:45 +00:00
fi
2017-01-02 12:35:45 +00:00
# Wait until the boot completes
2017-01-02 12:35:45 +00:00
printf " waiting for simulator (%s) to boot..." "$IOS_SIM_DEVICE"
2017-01-03 09:16:19 +00:00
until ruby -rjson -e "exit JSON.parse(%x{xcrun simctl list devices --json})['devices'].flat_map { |d| d[1] }.any? { |d| d['availability'] == '(available)' && d['state'] == 'Booted' }"; do
sleep 0.25
done
echo " done"
echo "It will take some time before the simulator is fully ready, continuing on to other work"
}
# Cleanup now and also cleanup when this script exits.
cleanup
trap cleanup EXIT
# Use a consistent version of Node if possible.
if [ -f "$NVM_DIR/nvm.sh" ]; then
. "$NVM_DIR/nvm.sh"
elif [ -x "$(command -v brew)" ] && [ -f "$(brew --prefix nvm)/nvm.sh" ]; then
# we must be on mac and nvm was installed with brew
# TODO: change the mac slaves to use manual nvm installation
. "$(brew --prefix nvm)/nvm.sh"
fi
[[ "$(command -v nvm)" ]] && nvm use 6.5.0 || true
# Remove cached packages
rm -rf ~/.yarn-cache/npm-realm-*
case "$TARGET" in
"eslint")
[[ $CONFIGURATION == 'Debug' ]] && exit 0
npm install
npm run lint .
;;
2016-11-17 15:55:02 +00:00
"eslint-ci")
[[ $CONFIGURATION == 'Debug' ]] && exit 0
npm install
./node_modules/.bin/eslint -f checkstyle . > eslint.xml || true
;;
"license-check")
[[ $CONFIGURATION == 'Debug' ]] && exit 0
licenses=$(node_modules/.bin/license-checker --exclude "$ACCEPTED_LICENSES");
if [[ $licenses ]]; then
echo "Unknown license detected in dependency:"
node_modules/.bin/license-checker --exclude "$ACCEPTED_LICENSES"
exit 1
else
echo "All licenses are accepted"
fi
;;
"jsdoc")
[[ $CONFIGURATION == 'Debug' ]] && exit 0
npm install
npm run jsdoc
;;
"realmjs")
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
download_server
start_server
pushd src
2016-05-13 23:30:57 +00:00
xctest RealmJS
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
stop_server
;;
"react-tests")
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
download_server
start_server
pushd tests/react-test-app
2016-11-11 07:55:26 +00:00
npm install
open_chrome
2015-12-17 18:56:08 +00:00
start_packager
2016-01-22 23:23:27 +00:00
2016-03-23 20:20:05 +00:00
pushd ios
xctest ReactTestApp
Add missing chrome debugging apis for Sync.User (#801) * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Add user tests * Add Sync member to Realm * Add a simple test for addListener * Introduce browser User class * Introduce Sync.User proxy * Fix User.all * Add User.logout * Remove some console.log statements * Update rpc.cpp from tests-folder * Use user.current from user-methods, not cpp * remove user.current from two missing places * Start server in realmjs and react tests * Replace needle with node-fetch * Make AuthError work in the browser * Allow for persistent callbacks * Expose _authenticateRealm to native * Use clear_test_state in roc * Streamline test inclusion * Fix login tests * Promisify testLogin() * Disable AsyncTests for now * Mode jasmine timeout to spec/unit_tests.js * Avoid use of global for fetch module * Only include user-tests is sync is enabled * Skip ROS for android tests * Add some comments about persistent callbacks * Fix results-test * Run async-tests in node * Add a comment about (user object) * Cache all users in User.current and return promises in fetch * node_require async-tests so RN doesn't try and package them * Add missing parenthesis
2017-01-31 13:07:29 +00:00
stop_server
;;
"react-example")
pushd examples/ReactExample
npm install
open_chrome
2015-12-17 18:56:08 +00:00
start_packager
2016-01-22 23:27:17 +00:00
pushd ios
xctest ReactExample
;;
"react-tests-android")
if [ "$(uname)" = 'Darwin' ]; then
download_server
start_server
fi
[[ $CONFIGURATION == 'Debug' ]] && exit 0
XCPRETTY=''
2016-01-22 20:15:38 +00:00
pushd tests/react-test-app
npm install
2016-01-22 23:23:27 +00:00
echo "Resetting logcat"
# Despite the docs claiming -c to work, it doesn't, so `-T 1` alleviates that.
adb logcat -c
adb logcat -T 1 | tee "$LOGCAT_OUT" &
./run-android.sh
echo "Start listening for Test completion"
while :; do
if grep -q "__REALM_REACT_ANDROID_TESTS_COMPLETED__" "$LOGCAT_OUT"; then
break
else
echo "Waiting for tests."
sleep 2
fi
2016-01-22 23:23:27 +00:00
done
2016-01-22 20:15:38 +00:00
rm -f tests.xml
adb pull /sdcard/tests.xml .
# Stop running child processes before printing results.
cleanup
2016-01-26 21:10:23 +00:00
echo "********* TESTS COMPLETED *********";
echo "********* File location: $(pwd)/tests.xml *********";
cat tests.xml
if [ "$(uname)" = 'Darwin' ]; then
stop_server
fi
;;
2016-04-19 21:10:10 +00:00
"node")
2016-11-10 13:42:58 +00:00
if [ "$(uname)" = 'Darwin' ]; then
download_server
start_server
npm_tests_cmd="npm run test"
2016-11-15 12:49:41 +00:00
npm install --build-from-source --realm_enable_sync
2016-11-10 13:42:58 +00:00
else
npm_tests_cmd="npm run test-nosync"
2016-11-15 12:49:41 +00:00
npm install --build-from-source
2016-11-10 13:42:58 +00:00
fi
2016-10-11 20:30:39 +00:00
2016-05-13 23:30:57 +00:00
# Change to a temp directory.
cd "$(mktemp -q -d -t realm.node.XXXXXX)"
test_temp_dir=$PWD # set it to be cleaned at exit
2016-04-19 21:10:10 +00:00
2016-10-04 22:02:51 +00:00
pushd "$SRCROOT/tests"
npm install
2017-01-02 12:35:45 +00:00
eval "$npm_tests_cmd"
2016-10-04 22:02:51 +00:00
popd
stop_server
2016-04-19 21:10:10 +00:00
;;
"node-nosync")
npm install --build-from-source
# Change to a temp directory.
cd "$(mktemp -q -d -t realm.node.XXXXXX)"
test_temp_dir=$PWD # set it to be cleaned at exit
pushd "$SRCROOT/tests"
npm install
npm run test-nosync
popd
;;
"test-runners")
# Create a fake realm module that points to the source root so that test-runner tests can require('realm')
2017-01-04 10:55:54 +00:00
rm -rf "$SRCROOT/tests/test-runners/node_modules/realm"
2017-01-03 11:55:36 +00:00
mkdir -p "$SRCROOT/tests/test-runners/node_modules"
ln -s "../../.." "$SRCROOT/tests/test-runners/node_modules/realm"
2016-10-11 20:30:39 +00:00
npm install --build-from-source
for runner in ava mocha jest; do
pushd "$SRCROOT/tests/test-runners/$runner"
npm install
npm test
popd
done
;;
2016-03-03 22:58:05 +00:00
"object-store")
pushd src/object-store
2017-01-02 12:35:45 +00:00
cmake -DCMAKE_BUILD_TYPE="$CONFIGURATION" .
2016-03-03 22:58:05 +00:00
make run-tests
2016-04-19 21:10:10 +00:00
;;
2016-10-04 22:02:51 +00:00
"download-object-server")
2017-01-02 12:35:45 +00:00
# shellcheck disable=SC1091
2016-10-04 22:02:51 +00:00
. dependencies.list
object_server_bundle="realm-object-server-bundled_node_darwin-$REALM_OBJECT_SERVER_VERSION.tar.gz"
curl -f -L "https://static.realm.io/downloads/object-server/$object_server_bundle" -o "$object_server_bundle"
rm -rf tests/sync-bundle
mkdir -p tests/sync-bundle
tar -C tests/sync-bundle -xf "$object_server_bundle"
rm "$object_server_bundle"
echo -e "enterprise:\n skip_setup: true\n" >> "tests/sync-bundle/object-server/configuration.yml"
touch "tests/sync-bundle/object-server/do_not_open_browser"
;;
"object-server-integration")
echo -e "yes\n" | ./tests/sync-bundle/reset-server-realms.command
2016-10-04 22:02:51 +00:00
pushd "$SRCROOT/tests"
npm install
npm run test-sync-integration
2016-10-04 22:02:51 +00:00
popd
;;
*)
2015-12-17 02:14:14 +00:00
echo "Invalid target '${TARGET}'"
exit 1
esac