2016-04-13 18:18:49 +03:00
|
|
|
/**
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-04-13 18:18:49 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-05-11 23:32:37 +03:00
|
|
|
*
|
|
|
|
* @format
|
2016-04-13 18:18:49 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This script tests that React Native end to end installation/bootstrap works for different platforms
|
|
|
|
* Available arguments:
|
2017-03-15 17:43:20 +03:00
|
|
|
* --ios - 'react-native init' and check iOS app doesn't redbox
|
|
|
|
* --tvos - 'react-native init' and check tvOS app doesn't redbox
|
|
|
|
* --android - 'react-native init' and check Android app doesn't redbox
|
|
|
|
* --js - 'react-native init' and only check the packager returns a bundle
|
2016-04-13 18:18:49 +03:00
|
|
|
* --skip-cli-install - to skip react-native-cli global installation (for local debugging)
|
2018-02-06 23:36:07 +03:00
|
|
|
* --retries [num] - how many times to retry possible flaky commands: yarn add and running tests, default 1
|
2016-04-13 18:18:49 +03:00
|
|
|
*/
|
|
|
|
/*eslint-disable no-undef */
|
|
|
|
require('shelljs/global');
|
2016-04-25 17:15:25 +03:00
|
|
|
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const argv = require('yargs').argv;
|
2016-04-13 18:18:49 +03:00
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const SCRIPTS = __dirname;
|
|
|
|
const ROOT = path.normalize(path.join(__dirname, '..'));
|
2016-05-04 20:57:30 +03:00
|
|
|
const tryExecNTimes = require('./try-n-times');
|
2016-04-13 18:18:49 +03:00
|
|
|
|
2016-04-25 17:15:25 +03:00
|
|
|
const TEMP = exec('mktemp -d /tmp/react-native-XXXXXXXX').stdout.trim();
|
|
|
|
const numberOfRetries = argv.retries || 1;
|
2016-04-13 18:18:49 +03:00
|
|
|
let SERVER_PID;
|
|
|
|
let APPIUM_PID;
|
2016-04-25 17:15:25 +03:00
|
|
|
let exitCode;
|
2016-04-13 18:18:49 +03:00
|
|
|
|
2019-04-10 04:43:02 +03:00
|
|
|
function describe(message) {
|
|
|
|
echo(`\n\n>>>>> ${message}\n\n\n`);
|
2019-02-22 00:12:48 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 17:15:25 +03:00
|
|
|
try {
|
|
|
|
// install CLI
|
2019-02-21 08:47:56 +03:00
|
|
|
const CLI_PACKAGE = 'react-native-cli';
|
2016-04-25 17:15:25 +03:00
|
|
|
if (!argv['skip-cli-install']) {
|
2019-02-22 00:12:48 +03:00
|
|
|
if (exec(`yarn global add ${CLI_PACKAGE}`).code) {
|
2017-09-21 23:34:02 +03:00
|
|
|
echo('Could not install react-native-cli globally.');
|
|
|
|
echo('Run with --skip-cli-install to skip this step');
|
2016-04-25 17:15:25 +03:00
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
|
|
|
|
2016-10-31 22:52:30 +03:00
|
|
|
if (argv.android) {
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
|
|
|
exec(
|
|
|
|
'./gradlew :ReactAndroid:installArchives -Pjobs=1 -Dorg.gradle.jvmargs="-Xmx512m -XX:+HeapDumpOnOutOfMemoryError"',
|
|
|
|
).code
|
|
|
|
) {
|
2016-04-25 17:15:25 +03:00
|
|
|
echo('Failed to compile Android binaries');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
|
|
|
|
2019-04-30 13:05:06 +03:00
|
|
|
if (argv.js) {
|
|
|
|
if (
|
|
|
|
tryExecNTimes(
|
|
|
|
() => {
|
|
|
|
return exec('npm install --save-dev flow-bin').code;
|
|
|
|
},
|
|
|
|
numberOfRetries,
|
|
|
|
() => exec('sleep 10s'),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
echo('Failed to install Flow');
|
|
|
|
echo('Most common reason is npm registry connectivity, try again');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 23:36:07 +03:00
|
|
|
if (exec('yarn pack').code) {
|
2016-04-25 17:15:25 +03:00
|
|
|
echo('Failed to pack react-native');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-13 18:18:49 +03:00
|
|
|
|
2016-04-25 17:15:25 +03:00
|
|
|
const PACKAGE = path.join(ROOT, 'react-native-*.tgz');
|
|
|
|
cd(TEMP);
|
2019-04-10 04:43:02 +03:00
|
|
|
|
|
|
|
echo('Creating EndToEndTest React Native app');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
|
|
|
tryExecNTimes(
|
|
|
|
() => {
|
2019-04-10 04:43:02 +03:00
|
|
|
return exec(`react-native init EndToEndTest --version ${PACKAGE} --npm`)
|
|
|
|
.code;
|
2018-05-11 23:32:37 +03:00
|
|
|
},
|
|
|
|
numberOfRetries,
|
2019-04-10 04:43:02 +03:00
|
|
|
() => {
|
|
|
|
rm('-rf', 'EndToEndTest');
|
|
|
|
exec('sleep 10s');
|
|
|
|
},
|
2018-05-11 23:32:37 +03:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
echo('Failed to execute react-native init');
|
|
|
|
echo('Most common reason is npm registry connectivity, try again');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
2019-05-04 00:34:57 +03:00
|
|
|
|
|
|
|
const METRO_CONFIG = path.join(ROOT, 'metro.config.js');
|
|
|
|
const RN_POLYFILLS = path.join(ROOT, 'rn-get-polyfills.js');
|
|
|
|
cp(METRO_CONFIG, 'EndToEndTest/.');
|
|
|
|
cp(RN_POLYFILLS, 'EndToEndTest/.');
|
|
|
|
|
2016-04-25 17:15:25 +03:00
|
|
|
cd('EndToEndTest');
|
2019-04-10 04:43:02 +03:00
|
|
|
echo('Installing React Native package');
|
|
|
|
exec(`npm install ${PACKAGE}`);
|
|
|
|
echo('Installing node_modules');
|
|
|
|
if (
|
|
|
|
tryExecNTimes(
|
|
|
|
() => {
|
|
|
|
return exec('npm install').code;
|
|
|
|
},
|
|
|
|
numberOfRetries,
|
|
|
|
() => exec('sleep 10s'),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
echo('Failed to execute npm install');
|
|
|
|
echo('Most common reason is npm registry connectivity, try again');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
|
2016-10-31 22:52:30 +03:00
|
|
|
if (argv.android) {
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Executing Android end-to-end tests');
|
2018-06-11 02:53:22 +03:00
|
|
|
echo('Installing end-to-end framework');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
|
|
|
tryExecNTimes(
|
|
|
|
() =>
|
|
|
|
exec(
|
2019-03-18 17:34:26 +03:00
|
|
|
'yarn add --dev appium@1.11.1 mocha@2.4.5 wd@1.11.1 colors@1.0.3 pretty-data2@0.40.1',
|
2018-05-11 23:32:37 +03:00
|
|
|
{silent: true},
|
|
|
|
).code,
|
|
|
|
numberOfRetries,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
echo('Failed to install appium');
|
|
|
|
echo('Most common reason is npm registry connectivity, try again');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
2016-04-25 17:15:25 +03:00
|
|
|
}
|
|
|
|
cp(`${SCRIPTS}/android-e2e-test.js`, 'android-e2e-test.js');
|
|
|
|
cd('android');
|
|
|
|
echo('Downloading Maven deps');
|
|
|
|
exec('./gradlew :app:copyDownloadableDepsToLibs');
|
|
|
|
cd('..');
|
2019-03-18 17:34:26 +03:00
|
|
|
|
|
|
|
exec('rm android/app/debug.keystore');
|
|
|
|
if (
|
|
|
|
exec(
|
|
|
|
'keytool -genkey -v -keystore android/app/debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Android Debug,O=Android,C=US"',
|
|
|
|
).code
|
|
|
|
) {
|
|
|
|
echo('Key could not be generated');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
|
2017-03-15 17:43:20 +03:00
|
|
|
echo(`Starting appium server, ${APPIUM_PID}`);
|
2016-04-25 17:15:25 +03:00
|
|
|
const appiumProcess = spawn('node', ['./node_modules/.bin/appium']);
|
|
|
|
APPIUM_PID = appiumProcess.pid;
|
2017-03-15 17:43:20 +03:00
|
|
|
|
|
|
|
echo('Building the app');
|
2019-03-18 17:34:26 +03:00
|
|
|
if (exec('react-native run-android').code) {
|
|
|
|
echo('could not execute react-native run-android');
|
2016-04-25 17:15:25 +03:00
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2017-03-15 17:43:20 +03:00
|
|
|
|
|
|
|
echo(`Starting packager server, ${SERVER_PID}`);
|
2016-04-25 17:15:25 +03:00
|
|
|
// shelljs exec('', {async: true}) does not emit stdout events, so we rely on good old spawn
|
2018-02-06 23:36:07 +03:00
|
|
|
const packagerProcess = spawn('yarn', ['start', '--max-workers 1'], {
|
2018-05-11 23:32:37 +03:00
|
|
|
env: process.env,
|
2016-04-25 17:15:25 +03:00
|
|
|
});
|
|
|
|
SERVER_PID = packagerProcess.pid;
|
|
|
|
// wait a bit to allow packager to startup
|
2016-06-25 02:54:52 +03:00
|
|
|
exec('sleep 15s');
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Test: Android end-to-end test');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
2019-04-10 04:43:02 +03:00
|
|
|
tryExecNTimes(
|
|
|
|
() => {
|
|
|
|
return exec('node node_modules/.bin/_mocha android-e2e-test.js').code;
|
|
|
|
},
|
|
|
|
numberOfRetries,
|
|
|
|
() => exec('sleep 10s'),
|
|
|
|
)
|
2018-05-11 23:32:37 +03:00
|
|
|
) {
|
2018-06-11 02:53:22 +03:00
|
|
|
echo('Failed to run Android end-to-end tests');
|
2018-05-11 23:32:37 +03:00
|
|
|
echo('Most likely the code is broken');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
2016-04-25 17:15:25 +03:00
|
|
|
}
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
|
2016-12-31 05:30:57 +03:00
|
|
|
if (argv.ios || argv.tvos) {
|
2018-05-11 23:32:37 +03:00
|
|
|
var iosTestType = argv.tvos ? 'tvOS' : 'iOS';
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Executing ' + iosTestType + ' end-to-end tests');
|
2016-04-25 17:15:25 +03:00
|
|
|
cd('ios');
|
|
|
|
// shelljs exec('', {async: true}) does not emit stdout events, so we rely on good old spawn
|
2016-10-31 22:52:30 +03:00
|
|
|
const packagerEnv = Object.create(process.env);
|
2016-04-25 17:15:25 +03:00
|
|
|
packagerEnv.REACT_NATIVE_MAX_WORKERS = 1;
|
2019-05-04 00:34:57 +03:00
|
|
|
const packagerProcess = spawn('yarn', ['start'], {
|
2018-05-11 23:32:37 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
env: packagerEnv,
|
|
|
|
});
|
2016-04-25 17:15:25 +03:00
|
|
|
SERVER_PID = packagerProcess.pid;
|
|
|
|
exec('sleep 15s');
|
|
|
|
// prepare cache to reduce chances of possible red screen "Can't fibd variable __fbBatchedBridge..."
|
2018-05-11 23:32:37 +03:00
|
|
|
exec(
|
|
|
|
'response=$(curl --write-out %{http_code} --silent --output /dev/null localhost:8081/index.bundle?platform=ios&dev=true)',
|
|
|
|
);
|
2016-04-25 17:15:25 +03:00
|
|
|
echo(`Starting packager server, ${SERVER_PID}`);
|
2019-03-22 13:26:05 +03:00
|
|
|
echo('Running pod install');
|
|
|
|
exec('pod install');
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Test: ' + iosTestType + ' end-to-end test');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
2019-04-10 04:43:02 +03:00
|
|
|
tryExecNTimes(
|
|
|
|
() => {
|
2019-04-24 19:26:56 +03:00
|
|
|
let destination = 'platform=iOS Simulator,name=iPhone 6s,OS=12.2';
|
2019-04-10 04:43:02 +03:00
|
|
|
let sdk = 'iphonesimulator';
|
|
|
|
let scheme = 'EndToEndTest';
|
2018-06-11 02:53:22 +03:00
|
|
|
|
2019-04-10 04:43:02 +03:00
|
|
|
if (argv.tvos) {
|
|
|
|
destination = 'platform=tvOS Simulator,name=Apple TV,OS=11.4';
|
|
|
|
sdk = 'appletvsimulator';
|
|
|
|
scheme = 'EndToEndTest-tvOS';
|
|
|
|
}
|
2018-06-11 02:53:22 +03:00
|
|
|
|
2019-04-10 04:43:02 +03:00
|
|
|
return exec(
|
2018-06-11 02:53:22 +03:00
|
|
|
[
|
2019-04-10 04:43:02 +03:00
|
|
|
'xcodebuild',
|
|
|
|
'-workspace',
|
|
|
|
'"EndToEndTest.xcworkspace"',
|
|
|
|
'-destination',
|
|
|
|
`"${destination}"`,
|
|
|
|
'-scheme',
|
|
|
|
`"${scheme}"`,
|
|
|
|
'-sdk',
|
|
|
|
sdk,
|
|
|
|
'-UseModernBuildSystem=NO',
|
|
|
|
'test',
|
2018-06-11 02:53:22 +03:00
|
|
|
].join(' ') +
|
2019-04-10 04:43:02 +03:00
|
|
|
' | ' +
|
|
|
|
[
|
|
|
|
'xcpretty',
|
|
|
|
'--report',
|
|
|
|
'junit',
|
|
|
|
'--output',
|
|
|
|
`"~/react-native/reports/junit/${iosTestType}-e2e/results.xml"`,
|
|
|
|
].join(' ') +
|
|
|
|
' && exit ${PIPESTATUS[0]}',
|
|
|
|
).code;
|
|
|
|
},
|
|
|
|
numberOfRetries,
|
|
|
|
() => exec('sleep 10s'),
|
|
|
|
)
|
2018-05-11 23:32:37 +03:00
|
|
|
) {
|
2018-06-11 02:53:22 +03:00
|
|
|
echo('Failed to run ' + iosTestType + ' end-to-end tests');
|
2018-05-11 23:32:37 +03:00
|
|
|
echo('Most likely the code is broken');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
2016-04-25 17:15:25 +03:00
|
|
|
}
|
|
|
|
cd('..');
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
|
2016-10-31 22:52:30 +03:00
|
|
|
if (argv.js) {
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Executing JavaScript end-to-end tests');
|
2016-04-25 17:15:25 +03:00
|
|
|
// Check the packager produces a bundle (doesn't throw an error)
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Test: Verify packager can generate an Android bundle');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
|
|
|
exec(
|
|
|
|
'react-native bundle --max-workers 1 --platform android --dev true --entry-file index.js --bundle-output android-bundle.js',
|
|
|
|
).code
|
|
|
|
) {
|
2016-11-19 05:25:02 +03:00
|
|
|
echo('Could not build Android bundle');
|
2016-04-25 17:15:25 +03:00
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Test: Verify packager can generate an iOS bundle');
|
2018-05-11 23:32:37 +03:00
|
|
|
if (
|
|
|
|
exec(
|
|
|
|
'react-native --max-workers 1 bundle --platform ios --dev true --entry-file index.js --bundle-output ios-bundle.js',
|
|
|
|
).code
|
|
|
|
) {
|
2016-11-19 05:25:02 +03:00
|
|
|
echo('Could not build iOS bundle');
|
2016-04-25 17:15:25 +03:00
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2019-04-10 04:43:02 +03:00
|
|
|
describe('Test: Flow check');
|
2019-04-30 13:05:06 +03:00
|
|
|
if (exec(path.join(ROOT, '/node_modules/.bin/flow') + ' check').code) {
|
2019-04-10 04:43:02 +03:00
|
|
|
echo('Flow check failed.');
|
|
|
|
exitCode = 1;
|
|
|
|
throw Error(exitCode);
|
|
|
|
}
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
exitCode = 0;
|
|
|
|
} finally {
|
|
|
|
if (SERVER_PID) {
|
|
|
|
echo(`Killing packager ${SERVER_PID}`);
|
|
|
|
exec(`kill -9 ${SERVER_PID}`);
|
|
|
|
// this is quite drastic but packager starts a daemon that we can't kill by killing the parent process
|
|
|
|
// it will be fixed in April (quote David Aurelio), so until then we will kill the zombie by the port number
|
|
|
|
exec("lsof -i tcp:8081 | awk 'NR!=1 {print $2}' | xargs kill");
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
if (APPIUM_PID) {
|
|
|
|
echo(`Killing appium ${APPIUM_PID}`);
|
|
|
|
exec(`kill -9 ${APPIUM_PID}`);
|
2016-04-13 18:18:49 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 17:15:25 +03:00
|
|
|
exit(exitCode);
|
2016-04-13 18:18:49 +03:00
|
|
|
|
|
|
|
/*eslint-enable no-undef */
|