2015-12-17 12:42:02 -08:00
#!/bin/bash
2015-12-15 18:02:20 -08:00
set -o pipefail
set -e
2016-08-19 10:06:45 -07:00
export TEST_SCRIPT = 1
export NPM_CONFIG_PROGRESS = false
2016-12-22 10:02:15 -08:00
TARGET = $1
CONFIGURATION = ${ 2 :- Release }
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 }
2016-01-26 16:12:26 -08:00
PATH = " /opt/android-sdk-linux/platform-tools: $PATH "
SRCROOT = $( cd " $( dirname " $0 " ) /.. " && pwd )
2016-11-18 14:00:30 -08:00
XCPRETTY = ` which xcpretty || true `
2016-12-22 10:02:15 -08:00
CI_RUN = false
if [ -n " ${ JENKINS_HOME } " ] ; then
CI_RUN = true
fi
2016-01-22 13:32:56 -08:00
2016-01-26 16:12:26 -08:00
# Start current working directory at the root of the project.
cd " $SRCROOT "
2016-04-19 16:57:52 -07:00
# Add node_modules to PATH just in case we weren't called from `npm test`
PATH = " $PWD /node_modules/.bin: $PATH "
2016-03-02 15:47:51 -08:00
if [ [ $TARGET = *-android ] ] ; then
# Inform the prepublish script to build Android modules.
export REALM_BUILD_ANDROID = 1
2016-01-22 13:30:16 -08:00
fi
2015-12-15 18:02:20 -08:00
2016-11-08 16:47:20 -08:00
SERVER_PID = 0
2016-01-26 16:12:26 -08:00
PACKAGER_OUT = " $SRCROOT /packager_out.txt "
LOGCAT_OUT = " $SRCROOT /logcat_out.txt "
2016-11-08 16:47:20 -08:00
download_server( ) {
sh ./scripts/download-object-server.sh
}
start_server( ) {
sh ./object-server-for-testing/start-object-server.command &
SERVER_PID = $!
}
stop_server( ) {
if [ [ ${ SERVER_PID } > 0 ] ] ; then
2016-12-07 13:28:05 +01:00
kill -9 ${ SERVER_PID }
2016-11-08 16:47:20 -08:00
fi
}
2016-12-22 10:02:15 -08:00
startedSimulator = false
log_temp =
test_temp_dir =
2016-01-26 16:12:26 -08:00
cleanup( ) {
2016-11-08 16:47:20 -08:00
# Kill started object server
2016-12-22 10:02:15 -08:00
stop_server || true
# 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
# Kill all child processes.
pkill -9 -P $$ || true
2016-11-08 16:47:20 -08:00
# Kill react native packager
2016-01-26 16:12:26 -08:00
pkill node || true
rm -f " $PACKAGER_OUT " " $LOGCAT_OUT "
2016-12-22 10:02:15 -08: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
2016-01-26 16:12:26 -08:00
}
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 10:56:08 -08:00
done
}
2016-01-26 16:12:26 -08:00
start_packager( ) {
2016-03-16 13:30:06 -07:00
watchman watch-del-all || true
2016-02-02 14:52:15 -08:00
./node_modules/react-native/packager/packager.sh | tee " $PACKAGER_OUT " &
2016-01-26 16:12:26 -08:00
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 16:30:57 -07:00
xctest( ) {
2016-12-22 10:02:15 -08:00
setup_ios_simulator
# - 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"
# - Run the build and test
2016-08-19 10:06:45 -07:00
if [ -n " $XCPRETTY " ] ; then
2016-12-22 10:02:15 -08: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 || {
2016-11-21 16:48:03 -08:00
EXITCODE = $?
printf " *** Xcode Failure (exit code $EXITCODE ). The full xcode log follows: ***\n\n "
2016-12-22 10:02:15 -08:00
cat " $log_temp "
2016-11-21 16:48:03 -08:00
printf "\n\n*** End Xcode Failure ***\n"
exit $EXITCODE
2016-11-10 10:06:48 -08:00
}
2016-12-22 10:02:15 -08:00
rm " $log_temp "
2016-08-19 10:06:45 -07:00
else
2016-12-22 10:02:15 -08:00
xcrun xcodebuild -scheme " $1 " -configuration " $CONFIGURATION " -sdk iphonesimulator -destination id = " $IOS_SIM_DEVICE " build test || {
2016-11-21 16:48:03 -08:00
EXITCODE = $?
echo " *** Failure (exit code $EXITCODE ). *** "
exit $EXITCODE
2016-12-22 10:02:15 -08:00
}
fi
}
setup_ios_simulator( ) {
# - Ensure one version of xcode is chosen by all tools
if [ [ -z " $DEVELOPER_DIR " ] ] ; then
export DEVELOPER_DIR = " $( xcode-select -p) "
fi
# -- Ensure that the simulator is ready
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 "
# 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
sleep 0.25 # otherwise the pkill following will get it too early
done
# stop CoreSimulatorService
launchctl remove com.apple.CoreSimulator.CoreSimulatorService 2>/dev/null || true
sleep 0.25 # launchtl can take a small moment to kill services
# 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
# - Prod `simctl` a few times as sometimes it fails the first couple of times after switching XCode vesions
local deadline = $(( SECONDS+5))
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
# - Choose a device, if it has not already been chosen
local deadline = $(( SECONDS+5))
while [ -z " $IOS_SIM_DEVICE " ] && [ $SECONDS -lt $deadline ] ; do
export IOS_SIM_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'] " `
done
if [ -z " $IOS_SIM_DEVICE " ] ; then
echo "*** Failed to determine the iOS Simulator device to use ***"
exit 1
fi
# - 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 "
# - 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
" $DEVELOPER_DIR /Applications/Simulator.app/Contents/MacOS/Simulator " -CurrentDeviceUDID " $IOS_SIM_DEVICE " & # will get killed with all other children at exit
startedSimulator = true
else
# - ensure that the simulator is running on a developer's workstation
open " $DEVELOPER_DIR /Applications/Simulator.app "
# - 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
export IOS_SIM_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']" `
done
if [ -z " $IOS_SIM_DEVICE " ] ; then
echo "*** Failed to determine the iOS Simulator device in use ***"
exit 1
fi
2016-08-19 10:06:45 -07:00
fi
2016-12-22 10:02:15 -08:00
# Wait until the boot completes
printf " waiting for simulator ( $IOS_SIM_DEVICE ) to boot... "
until ruby -rjson -e 'exit JSON.parse(`xcrun simctl list devices -j `)["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"
2016-01-26 16:12:26 -08:00
}
# Cleanup now and also cleanup when this script exits.
cleanup
trap cleanup EXIT
2015-12-15 18:02:20 -08:00
2016-02-19 02:53:10 -08:00
# Use a consistent version of Node if possible.
if [ -s " ${ HOME } /.nvm/nvm.sh " ] ; then
. " ${ HOME } /.nvm/nvm.sh "
2016-11-22 07:07:52 -08:00
nvm use 5.12 || true
2016-02-19 02:53:10 -08:00
fi
2016-11-10 23:11:31 -08:00
# Remove cached packages
rm -rf ~/.yarn-cache/npm-realm-*
2016-01-26 16:12:26 -08:00
case " $TARGET " in
2016-03-02 15:16:22 -08:00
"eslint" )
2016-03-16 17:49:20 -07:00
[ [ $CONFIGURATION = = 'Debug' ] ] && exit 0
npm install
2016-03-02 15:16:22 -08:00
npm run lint .
; ;
2016-11-17 16:55:02 +01:00
"eslint-ci" )
[ [ $CONFIGURATION = = 'Debug' ] ] && exit 0
npm install
./node_modules/.bin/eslint -f checkstyle . > eslint.xml || true
; ;
2016-02-12 02:47:08 -08:00
"jsdoc" )
2016-03-16 17:49:20 -07:00
[ [ $CONFIGURATION = = 'Debug' ] ] && exit 0
npm install
2016-02-12 02:47:08 -08:00
npm run jsdoc
; ;
2016-01-26 16:12:26 -08:00
"realmjs" )
2016-05-16 16:26:37 -07:00
pushd src
2016-05-13 16:30:57 -07:00
xctest RealmJS
2016-01-26 16:12:26 -08:00
; ;
"react-tests" )
2015-12-15 18:02:20 -08:00
pushd tests/react-test-app
2016-11-10 23:55:26 -08:00
npm install
2016-01-26 16:12:26 -08:00
open_chrome
2015-12-17 10:56:08 -08:00
start_packager
2016-01-22 23:23:27 +00:00
2016-03-23 13:20:05 -07:00
pushd ios
2016-12-22 10:02:15 -08:00
xctest ReactTestApp
2016-01-26 16:12:26 -08:00
; ;
"react-example" )
2015-12-15 18:02:20 -08:00
pushd examples/ReactExample
2016-01-26 16:12:26 -08:00
2016-02-19 02:28:59 -08:00
npm install
2016-02-16 00:37:10 -08:00
open_chrome
2015-12-17 10:56:08 -08:00
start_packager
2016-01-22 15:27:17 -08:00
pushd ios
2016-12-22 10:02:15 -08:00
xctest ReactExample
2016-01-26 16:12:26 -08:00
; ;
"react-tests-android" )
2016-03-16 17:49:20 -07:00
[ [ $CONFIGURATION = = 'Debug' ] ] && exit 0
2016-11-18 14:00:30 -08:00
XCPRETTY = ''
2016-02-08 11:16:18 -08:00
2016-01-22 12:15:38 -08:00
pushd tests/react-test-app
npm install
2016-01-22 23:23:27 +00:00
2016-12-20 19:01:45 +00:00
echo "Resetting logcat"
2016-02-02 14:52:15 -08:00
# Despite the docs claiming -c to work, it doesn't, so `-T 1` alleviates that.
2016-01-26 16:12:26 -08:00
adb logcat -c
2016-02-02 14:52:15 -08:00
adb logcat -T 1 | tee " $LOGCAT_OUT " &
2016-01-26 16:12:26 -08:00
2016-12-20 19:01:45 +00:00
./run-android.sh
echo "Start listening for Test completion"
2016-01-26 16:12:26 -08:00
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 12:15:38 -08:00
2016-02-02 14:52:15 -08:00
rm -f tests.xml
adb pull /sdcard/tests.xml .
2016-01-26 16:12:26 -08:00
2016-02-02 14:52:15 -08:00
# Stop running child processes before printing results.
cleanup
2016-01-26 21:10:23 +00:00
echo "********* TESTS COMPLETED *********" ;
2016-01-26 16:12:26 -08:00
echo " ********* File location: $( pwd ) /tests.xml ********* " ;
2016-02-02 14:52:15 -08:00
cat tests.xml
2016-01-26 16:12:26 -08:00
; ;
2016-04-19 14:10:10 -07:00
"node" )
2016-11-10 14:42:58 +01:00
if [ " $( uname) " = 'Darwin' ] ; then
download_server
start_server
npm_tests_cmd = "npm run test"
2016-11-15 13:49:41 +01:00
npm install --build-from-source --realm_enable_sync
2016-11-10 14:42:58 +01:00
else
npm_tests_cmd = "npm run test-nosync"
2016-11-15 13:49:41 +01:00
npm install --build-from-source
2016-11-10 14:42:58 +01:00
fi
2016-10-11 22:30:39 +02:00
2016-05-13 16:30:57 -07:00
# Change to a temp directory.
cd " $( mktemp -q -d -t realm.node.XXXXXX) "
2016-12-22 10:02:15 -08:00
test_temp_dir = $PWD # set it to be cleaned at exit
2016-04-19 14:10:10 -07:00
2016-10-04 15:02:51 -07:00
pushd " $SRCROOT /tests "
npm install
2016-11-10 14:42:58 +01:00
eval $npm_tests_cmd
2016-10-04 15:02:51 -07:00
popd
2016-11-08 16:47:20 -08:00
stop_server
2016-04-19 14:10:10 -07:00
; ;
2016-11-09 12:50:19 -08:00
"node-nosync" )
npm install --build-from-source
# Change to a temp directory.
cd " $( mktemp -q -d -t realm.node.XXXXXX) "
2016-12-22 10:02:15 -08:00
test_temp_dir = $PWD # set it to be cleaned at exit
2016-11-09 12:50:19 -08:00
pushd " $SRCROOT /tests "
npm install
npm run test-nosync
popd
; ;
2016-08-10 15:47:55 +02:00
"test-runners" )
2016-10-11 22:30:39 +02:00
npm install --build-from-source
2016-08-10 15:50:37 +02:00
for runner in ava mocha jest; do
2016-08-10 15:47:55 +02:00
pushd " $SRCROOT /tests/test-runners/ $runner "
npm install
npm test
popd
done
; ;
2016-03-03 14:58:05 -08:00
"object-store" )
pushd src/object-store
2016-03-16 17:30:29 -07:00
cmake -DCMAKE_BUILD_TYPE= $CONFIGURATION .
2016-03-03 14:58:05 -08:00
make run-tests
2016-04-19 14:10:10 -07:00
; ;
2016-10-04 15:02:51 -07:00
"download-object-server" )
. 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" )
2016-11-09 17:28:47 +01:00
echo -e "yes\n" | ./tests/sync-bundle/reset-server-realms.command
2016-10-04 15:02:51 -07:00
pushd " $SRCROOT /tests "
npm install
2016-11-09 12:50:19 -08:00
npm run test-sync-integration
2016-10-04 15:02:51 -07:00
popd
; ;
2016-01-26 16:12:26 -08:00
*)
2015-12-16 18:14:14 -08:00
echo " Invalid target ' ${ TARGET } ' "
2016-01-26 16:12:26 -08:00
exit 1
esac