Skip to content

Commit 766c755

Browse files
committed
General clean up
1 parent 979e320 commit 766c755

File tree

3 files changed

+125
-119
lines changed

3 files changed

+125
-119
lines changed

scripts/e2e/android-devices.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const ChildProcess = require('node:child_process');
2+
3+
// Should be kept in sync with the constant defined in e2e workflow file
4+
const DEFAULT_CI_AVD_NAME = 'e2e_emulator';
5+
6+
function detectLocalAndroidEmulator() {
7+
// "RNS_E2E_AVD_NAME" can be set for local developement
8+
const avdName = process.env.RNS_AVD_NAME ?? null;
9+
if (avdName !== null) {
10+
return avdName
11+
}
12+
13+
// Fallback: try to use Android SDK
14+
try {
15+
let stdout = ChildProcess.execSync("emulator -list-avds");
16+
17+
// Possibly convert Buffer to string
18+
if (typeof stdout !== 'string') {
19+
stdout = stdout.toString();
20+
}
21+
22+
const avdList = stdout.trim().split('\n').map(name => name.trim());
23+
24+
if (avdList.length === 0) {
25+
throw new Error('No installed AVDs detected on the device');
26+
}
27+
28+
// Just select first one in the list.
29+
// TODO: consider giving user a choice here.
30+
return avdList[0];
31+
} catch (error) {
32+
const errorMessage = `Failed to find Android emulator. Set "RNS_E2E_AVD_NAME" env variable pointing to one. Cause: ${error}`;
33+
console.error(errorMessage);
34+
throw new Error(errorMessage);
35+
}
36+
}
37+
38+
/**
39+
* @param {boolean} isRunningCI whether this script is run in CI environment
40+
*/
41+
function detectAndroidEmulatorName(isRunningCI) {
42+
return isRunningCI ? DEFAULT_CI_AVD_NAME : detectLocalAndroidEmulator();
43+
}
44+
45+
/**
46+
* @returns {string | null} Device serial as requested by user, first serial from adb list or null
47+
*/
48+
function resolveAndroidDeviceSerial() {
49+
const deviceSerial = process.env.RNS_DEVICE_SERIAL ?? null;
50+
51+
if (deviceSerial !== null) {
52+
return deviceSerial;
53+
}
54+
55+
// Fallback: try to use adb
56+
try {
57+
let stdout = ChildProcess.execSync("adb devices");
58+
59+
// Possibly convert Buffer to string
60+
if (typeof stdout !== 'string') {
61+
stdout = stdout.toString();
62+
}
63+
64+
/** @type {string} */
65+
const stringStdout = stdout;
66+
67+
// Example `adb devices` output:
68+
//
69+
// List of devices attached
70+
// 6lh6huzhr48lu8t8 device
71+
// emulator-5554 device
72+
73+
const deviceList = stringStdout
74+
.trim()
75+
.split('\n')
76+
.map(line => line.trim())
77+
.filter((line, index) => line !== '' && index !== 0) // empty lines & header
78+
.map(line => line.split(' ', 1)[0]);
79+
80+
81+
if (deviceList.length === 0) {
82+
throw new Error("Seems that the attached device list is empty");
83+
}
84+
85+
// Just select first one in the list.
86+
// TODO: consider giving user a choice here.
87+
return deviceList[0];
88+
} catch (error) {
89+
console.error(`Failed to find attached device. Try setting "RNS_DEVICE_SERIAL" env variable pointing to one. Cause: ${error}`);
90+
}
91+
92+
return null;
93+
}
94+
95+
module.exports = {
96+
DEFAULT_CI_AVD_NAME,
97+
detectLocalAndroidEmulator,
98+
detectAndroidEmulatorName,
99+
resolveAndroidDeviceSerial,
100+
}

scripts/e2e/detox-utils.cjs

Lines changed: 16 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
const ChildProcess = require('node:child_process');
2-
const { iosDevice } = require('./ios-devices');
3-
4-
// Should be kept in sync with the constant defined in e2e workflow file
5-
const DEFAULT_CI_AVD_NAME = 'e2e_emulator';
1+
const AppleDeviceUtil = require('./ios-devices');
2+
const AndroidDeviceUtil = require('./android-devices');
63

74
const isRunningCI = process.env.CI != null;
85

@@ -12,105 +9,24 @@ const apkBulidArchitecture = isRunningCI ? 'x86_64' : 'arm64-v8a';
129
// it is assumed here that arm64-v8a AOSP emulator is not available in local setup.
1310
const testButlerApkPath = isRunningCI ? ['../Example/e2e/apps/test-butler-app-2.2.1.apk'] : undefined;
1411

15-
function detectLocalAndroidEmulator() {
16-
// "RNS_E2E_AVD_NAME" can be set for local developement
17-
const avdName = process.env.RNS_AVD_NAME ?? null;
18-
if (avdName !== null) {
19-
return avdName
20-
}
21-
22-
// Fallback: try to use Android SDK
23-
try {
24-
let stdout = ChildProcess.execSync("emulator -list-avds");
25-
26-
// Possibly convert Buffer to string
27-
if (typeof stdout !== 'string') {
28-
stdout = stdout.toString();
29-
}
30-
31-
const avdList = stdout.trim().split('\n').map(name => name.trim());
32-
33-
if (avdList.length === 0) {
34-
throw new Error('No installed AVDs detected on the device');
35-
}
36-
37-
// Just select first one in the list.
38-
// TODO: consider giving user a choice here.
39-
return avdList[0];
40-
} catch (error) {
41-
const errorMessage = `Failed to find Android emulator. Set "RNS_E2E_AVD_NAME" env variable pointing to one. Cause: ${error}`;
42-
console.error(errorMessage);
43-
throw new Error(errorMessage);
44-
}
45-
}
46-
47-
function detectAndroidEmulatorName() {
48-
// "RNS_E2E_AVD_NAME" can be set for local developement
49-
return isRunningCI ? DEFAULT_CI_AVD_NAME : detectLocalAndroidEmulator();
50-
}
51-
52-
/**
53-
* @returns {string | null} Device serial as requested by user, first serial from adb list or null
54-
*/
55-
function resolveAndroidDeviceSerial() {
56-
const deviceSerial = process.env.RNS_DEVICE_SERIAL ?? null;
57-
58-
if (deviceSerial !== null) {
59-
return deviceSerial;
60-
}
61-
62-
// Fallback: try to use adb
63-
try {
64-
let stdout = ChildProcess.execSync("adb devices");
65-
66-
// Possibly convert Buffer to string
67-
if (typeof stdout !== 'string') {
68-
stdout = stdout.toString();
69-
}
70-
71-
/** @type {string} */
72-
const stringStdout = stdout;
73-
74-
// Example `adb devices` output:
75-
//
76-
// List of devices attached
77-
// 6lh6huzhr48lu8t8 device
78-
// emulator-5554 device
79-
80-
const deviceList = stringStdout
81-
.trim()
82-
.split('\n')
83-
.map(line => line.trim())
84-
.filter((line, index) => line !== '' && index !== 0) // empty lines & header
85-
.map(line => line.split(' ', 1)[0]);
86-
87-
88-
if (deviceList.length === 0) {
89-
throw new Error("Seems that the attached device list is empty");
90-
}
91-
92-
// Just select first one in the list.
93-
// TODO: consider giving user a choice here.
94-
return deviceList[0];
95-
} catch (error) {
96-
console.error(`Failed to find attached device. Try setting "RNS_DEVICE_SERIAL" env variable pointing to one. Cause: ${error}`);
97-
}
98-
99-
return null;
100-
}
101-
10212
/**
10313
* The output of this function can be controlled through couple of env vars.
10414
*
10515
* * `RNS_DEVICE_SERIAL` env var can be specified in case of running
106-
* tests with an attached device. It can also be an emulator.
16+
* tests with an attached Android device. It can also be an emulator.
10717
* The expected value here is the same as you would pass to `adb -s`.
10818
* You can find device serial by running `adb devices` command.
10919
*
110-
* * `RNS_AVD_NAME` env var can be specified in case of running tests on emulator.
20+
* * `RNS_AVD_NAME` env var can be specified in case of running tests on Android emulator.
11121
* The exepected value here is the same as displayed in Android Studio or listed by
11222
* `emulator -list-avds`.
11323
*
24+
* * `RNS_APPLE_SIM_NAME` env var can be set in case of running tests on iOS simulator.
25+
* The expected value here is exactly as one listed in XCode.
26+
*
27+
* * `RNS_IOS_VERSION` env var can be specified to request particular iOS version
28+
* for the given simulator. Note that required SDK & simulators must be installed.
29+
*
11430
* @param {string} applicationName name (FabricExample / ScreensExample)
11531
* @returns {Detox.DetoxConfig}
11632
*/
@@ -157,19 +73,22 @@ function commonDetoxConfigFactory(applicationName) {
15773
devices: {
15874
simulator: {
15975
type: 'ios.simulator',
160-
device: iosDevice,
76+
device: {
77+
type: AppleDeviceUtil.resolveAppleSimulatorName(),
78+
os: AppleDeviceUtil.getIOSVersion(),
79+
},
16180
},
16281
attached: {
16382
type: 'android.attached',
16483
device: {
165-
adbName: resolveAndroidDeviceSerial(),
84+
adbName: AndroidDeviceUtil.resolveAndroidDeviceSerial(),
16685
},
16786
utilBinaryPaths: testButlerApkPath,
16887
},
16988
emulator: {
17089
type: 'android.emulator',
17190
device: {
172-
avdName: detectAndroidEmulatorName(),
91+
avdName: AndroidDeviceUtil.detectAndroidEmulatorName(),
17392
},
17493
utilBinaryPaths: testButlerApkPath,
17594
},

scripts/e2e/ios-devices.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
1-
const DEFAULT_APPLE_DEVICE = 'iPhone 17';
2-
const DEFEAULT_IOS_VERSION = 'iOS 26.2';
1+
const DEFAULT_APPLE_SIMULATOR_NAME = 'iPhone 17';
2+
const DEFAULT_IOS_VERSION = 'iOS 26.2';
33

44
/**
55
* @return {string}
66
*/
7-
function getAppleDevice() {
8-
return process.env.RNS_E2E_APPLE_SIM_NAME || DEFAULT_APPLE_DEVICE;
7+
function resolveAppleSimulatorName() {
8+
return process.env.RNS_APPLE_SIM_NAME ?? DEFAULT_APPLE_SIMULATOR_NAME;
99
}
1010
/**
1111
* @return {`iOS ${string}`} requested version of ios, or default if not specified
1212
*/
1313
function getIOSVersion() {
14-
const passedVersion = process.env.RNS_E2E_IOS_VERSION;
14+
const passedVersion = process.env.RNS_IOS_VERSION;
1515
if (passedVersion) {
1616
if (passedVersion.startsWith('iOS ')) {
1717
return /** @type {`iOS ${string}`} */ (passedVersion);
1818
}
1919
return `iOS ${passedVersion}`;
2020
}
21-
return DEFEAULT_IOS_VERSION;
21+
return DEFAULT_IOS_VERSION;
2222
}
2323

24-
/**
25-
* @typedef {Object} AppleDevice - creates a new type named 'SpecialType'
26-
* @property {string} type - a string which represents a model of an iPhone
27-
* @property {`iOS ${string}`} os - operation system version
28-
*/
29-
30-
/**
31-
* @satisfies {AppleDevice}
32-
* @readonly
33-
* */
34-
const iosDevice = {
35-
type: getAppleDevice(),
36-
os: getIOSVersion(),
37-
};
38-
3924
module.exports = {
40-
iosDevice,
25+
resolveAppleSimulatorName,
26+
getIOSVersion,
4127
};
28+

0 commit comments

Comments
 (0)