forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-release-local.js
More file actions
403 lines (343 loc) · 12.2 KB
/
Copy pathtest-release-local.js
File metadata and controls
403 lines (343 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
/*
* This script is a re-interpretation of the old test-manual.e2e.sh script.
* the idea is to provide a better DX for the manual testing.
* It's using Javascript over Bash for consistency with the rest of the recent scripts
* and to make it more accessible for other devs to play around with.
*/
const {initNewProjectFromSource} = require('../e2e/init-project-e2e');
const {REPO_ROOT} = require('../shared/consts');
const {
checkPackagerRunning,
launchPackagerInSeparateWindow,
maybeLaunchAndroidEmulator,
prepareArtifacts,
setupGHAArtifacts,
} = require('./utils/testing-utils');
const fs = require('fs');
const path = require('path');
const {cd, exec, popd, pushd, pwd, sed} = require('shelljs');
const {styleText} = require('util');
const yargs = require('yargs');
/* ::
type Unwrap<T> = T extends Promise<infer U> ? U : T;
*/
const argv = yargs
.option('t', {
alias: 'target',
default: 'RNTester',
choices: ['RNTester', 'RNTestProject'],
})
.option('p', {
alias: 'platform',
default: 'ios',
coerce: platform => platform.toLowerCase(),
choices: ['ios', 'android'],
})
.option('c', {
alias: 'ciToken',
type: 'string',
})
.option('useLastSuccessfulPipeline', {
desc:
'Use the last successful CI pipeline to download build artifacts.' +
'Only use this option if you are certain that this is sufficient for ' +
'the configuration you are testing.',
type: 'boolean',
default: false,
}).argv;
// === RNTester === //
/**
* Start the test for RNTester on iOS.
*
* Parameters:
* - @ciArtifacts manager object to manage all the download of ciArtifacts. If null, it will fallback not to use them.
* - @onReleaseBranch whether we are on a release branch or not
*/
async function testRNTesterIOS(
ciArtifacts /*: Unwrap<ReturnType<typeof setupGHAArtifacts>> */,
onReleaseBranch /*: boolean */,
) {
console.info(
`We're going to test the 'Hermes' version of RNTester iOS with the new Architecture enabled`,
);
// if everything succeeded so far, we can launch Metro and the app
// start the Metro server in a separate window
launchPackagerInSeparateWindow(pwd().toString());
// remember that for this to be successful
// you should have run bundle install once
// in your local setup
if (ciArtifacts != null) {
const appOutputFolder = path.join(
ciArtifacts.baseTmpPath(),
'RNTester.app',
);
exec(`rm -rf ${appOutputFolder}`);
// download hermes App
const hermesAppUrl = await ciArtifacts.artifactURLForHermesRNTesterApp();
const hermesAppZipPath = path.join(
ciArtifacts.baseTmpPath(),
'RNTesterAppHermes.zip',
);
ciArtifacts.downloadArtifact(hermesAppUrl, hermesAppZipPath);
exec(`unzip ${hermesAppZipPath} -d ${appOutputFolder}`);
// boot device
bootSimulatorIfNeeded();
// install app on device
exec(`xcrun simctl install booted ${appOutputFolder}`);
} else {
exec(
`USE_HERMES=1 CI=${onReleaseBranch.toString()} RCT_NEW_ARCH_ENABLED=1 bundle exec pod install --ansi`,
);
// boot device
bootSimulatorIfNeeded();
// build the app on iOS simulator
exec(
'xcodebuild -workspace RNTesterPods.xcworkspace -scheme RNTester -sdk "iphonesimulator" -destination "generic/platform=iOS Simulator" -derivedDataPath "/tmp/RNTesterBuild"',
);
// install app on device
exec(
'xcrun simctl install booted "/tmp/RNTesterBuild/Build/Products/Debug-iphonesimulator/RNTester.app"',
);
// bring iOS simulator to the front
exec('open -a simulator');
}
// launch the app on iOS simulator
exec('xcrun simctl launch booted com.meta.RNTester.localDevelopment');
}
/**
* Start the test for RNTester on Android.
*
* Parameters:
* - @ciArtifacts manager object to manage all the download of ciArtifacts. If null, it will fallback not to use them.
*/
async function testRNTesterAndroid(
ciArtifacts /*: Unwrap<ReturnType<typeof setupGHAArtifacts>> */,
) {
maybeLaunchAndroidEmulator();
console.info(
`We're going to test RNTester Android with the new Architecture enabled`,
);
// Start the Metro server so it will be ready if the app can be built and installed successfully.
launchPackagerInSeparateWindow(pwd().toString());
// Wait for the Android Emulator to be properly loaded and bootstrapped
exec(
"adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;",
);
if (ciArtifacts != null) {
const downloadPath = path.join(ciArtifacts.baseTmpPath(), 'rntester.zip');
const emulatorArch = exec('adb shell getprop ro.product.cpu.abi').trim();
// Github Actions zips all the APKs in a single archive
console.info('Start Downloading APK');
const rntesterAPKURL =
await ciArtifacts.artifactURLForRNTesterAPK(emulatorArch);
ciArtifacts.downloadArtifact(rntesterAPKURL, downloadPath);
const unzipFolder = path.join(ciArtifacts.baseTmpPath(), 'rntester-apks');
exec(`rm -rf ${unzipFolder}`);
exec(`unzip ${downloadPath} -d ${unzipFolder}`);
let apkPath = path.join(unzipFolder, `app-${emulatorArch}-debug.apk`);
exec(`adb install ${apkPath}`);
} else {
exec(`../../gradlew :packages:rn-tester:android:app:installDebug --quiet`);
}
// launch the app
// TODO: we should find a way to make it work like for iOS, via npx react-native run-android
// currently, that fails with an error.
exec(
'adb shell am start -n com.facebook.react.uiapp/com.facebook.react.uiapp.RNTesterActivity',
);
// just to make sure that the Android up won't have troubles finding the Metro server
exec('adb reverse tcp:8081 tcp:8081');
}
/**
* Function that start testing on RNTester.
*
* Parameters:
* - @ciArtifacts manager object to manage all the download of ciArtifacts. If null, it will fallback not to use them.
* - @onReleaseBranch whether we are on a release branch or not
*/
async function testRNTester(
ciArtifacts /*:Unwrap<ReturnType<typeof setupGHAArtifacts>> */,
onReleaseBranch /*: boolean */,
) {
// Build Codegen as we're on a empty environment and metro needs it.
// This can be removed once we have codegen hooked in the `yarn build` step.
console.info('[Codegen] >>> Building react-native-codegen...');
exec('./packages/react-native-codegen/scripts/oss/build.sh');
// FIXME: make sure that the commands retains colors
// (--ansi) doesn't always work
// see also https://github.com/shelljs/shelljs/issues/86
pushd('packages/rn-tester');
if (argv.platform === 'ios') {
await testRNTesterIOS(ciArtifacts, onReleaseBranch);
} else {
await testRNTesterAndroid(ciArtifacts);
}
popd();
}
// === RNTestProject === //
async function testRNTestProject(
ciArtifacts /*: Unwrap<ReturnType<typeof setupGHAArtifacts>> */,
) {
console.info("We're going to test a fresh new RN project");
// create the local npm package to feed the CLI
// base setup required (specular to publish-npm.js)
// in local testing, 1000.0.0 mean we are on main, every other case means we are
// working on a release version
const shortCommit = exec('git rev-parse HEAD', {silent: true})
.toString()
.trim()
.slice(0, 9);
const releaseVersion = `1000.0.0-${shortCommit}`;
const buildType = 'dry-run';
const reactNativePackagePath = `${REPO_ROOT}/packages/react-native`;
const localNodeTGZPath = `${reactNativePackagePath}/react-native-${releaseVersion}.tgz`;
const mavenLocalPath =
ciArtifacts != null
? path.join(ciArtifacts.baseTmpPath(), 'maven-local')
: '/private/tmp/maven-local';
const {newLocalNodeTGZ} = await prepareArtifacts(
ciArtifacts,
mavenLocalPath,
localNodeTGZPath,
releaseVersion,
buildType,
reactNativePackagePath,
);
// If artifacts were built locally, we need to pack the react-native package
if (ciArtifacts == null) {
exec('npm pack --pack-destination ', {cwd: reactNativePackagePath});
// node pack does not creates a version of React Native with the right name on main.
// Let's add some defensive programming checks:
if (!fs.existsSync(newLocalNodeTGZ)) {
const tarfile = fs
.readdirSync(reactNativePackagePath)
.find(
name => name.startsWith('react-native-') && name.endsWith('.tgz'),
);
if (tarfile == null) {
throw new Error("Couldn't find a zipped version of react-native");
}
exec(
`cp ${path.join(reactNativePackagePath, tarfile)} ${newLocalNodeTGZ}`,
);
}
}
const currentBranch = exec('git rev-parse --abbrev-ref HEAD')
.toString()
.trim();
pushd('/tmp/');
// Cleanup RNTestProject folder. This makes it easier to rerun the script when it fails
exec('rm -rf /tmp/RNTestProject');
await initNewProjectFromSource({
projectName: 'RNTestProject',
directory: '/tmp/RNTestProject',
pathToLocalReactNative: newLocalNodeTGZ,
currentBranch,
});
cd('RNTestProject');
// need to do this here so that Android will be properly setup either way
exec(
`echo "\nreact.internal.mavenLocalRepo=${mavenLocalPath}" >> android/gradle.properties`,
);
// Only build the simulator architecture. CI is however generating only that one.
sed(
'-i',
'reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64',
'reactNativeArchitectures=arm64-v8a',
'android/gradle.properties',
);
if (argv.platform === 'ios') {
// doing the pod install here so that it's easier to play around RNTestProject
cd('ios');
exec('bundle install');
exec(`USE_HERMES=1 bundle exec pod install --ansi`);
cd('..');
exec('npm run ios');
} else {
// android
exec('npm run android');
}
popd();
}
function bootSimulatorIfNeeded() {
const bootedDevices = String(
exec('xcrun simctl list devices booted', {silent: true}),
).trim();
// Check if there is at least one booted device by looking for a line with a UDID
if (/\([A-F0-9-]+\)/i.test(bootedDevices)) {
console.info('An iOS simulator is already booted, skipping boot.');
return;
}
// Find the latest available iPhone simulator by picking the last iOS runtime
// and the first iPhone device listed under it.
const devicesJson = JSON.parse(
String(
exec('xcrun simctl list devices iPhone available -j', {silent: true}),
),
);
const runtimes = Object.keys(devicesJson.devices)
.filter(r => r.startsWith('com.apple.CoreSimulator.SimRuntime.iOS'))
.sort();
const latestRuntime = runtimes[runtimes.length - 1];
const device =
latestRuntime != null
? devicesJson.devices[latestRuntime].find(d => d.name.includes('iPhone'))
: null;
if (device != null) {
console.info(`Booting ${device.name} (${latestRuntime})...`);
exec(`xcrun simctl boot "${device.udid}"`);
} else {
console.error('No available iPhone simulator found.');
process.exit(1);
}
}
async function main() {
/*
* see the test-local-e2e.js script for clean up process
*/
// command order: we ask the user to select if they want to test RN tester
// or RNTestProject
// if they select RN tester, we ask if iOS or Android, and then we run the tests
// if they select RNTestProject, we run the RNTestProject test
checkPackagerRunning();
const branchName = exec('git rev-parse --abbrev-ref HEAD', {
silent: true,
}).stdout.trim();
const onReleaseBranch = branchName.endsWith('-stable');
let ghaArtifacts = await setupGHAArtifacts(
// $FlowFixMe[prop-missing]
argv.ciToken,
branchName,
// $FlowFixMe[prop-missing]
argv.useLastSuccessfulPipeline,
);
if (argv.target === 'RNTester') {
await testRNTester(ghaArtifacts, onReleaseBranch);
} else {
await testRNTestProject(ghaArtifacts);
console.warn(
styleText(
'yellow',
`
================================================================================
NOTE: Verdaccio may still be running on after this script has finished. Please
Force Quit via Activity Monitor.
================================================================================
`,
),
);
}
}
if (require.main === module) {
void main();
}