-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtest-matrix.mjs
More file actions
349 lines (319 loc) · 8.07 KB
/
test-matrix.mjs
File metadata and controls
349 lines (319 loc) · 8.07 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
// @ts-check
/**
* Reminder that this script is meant to be runnable without installing
* dependencies. It can therefore not rely on any external libraries.
*/
import { spawn, spawnSync } from "node:child_process";
import * as fs from "node:fs";
import { URL, fileURLToPath } from "node:url";
import * as util from "node:util";
import { readTextFile } from "../helpers.js";
import { setReactVersion } from "../internal/set-react-version.mjs";
import { green, red, yellow } from "../utils/colors.mjs";
import { getIOSSimulatorName, installPods } from "./test-apple.mjs";
import { $, $$, test } from "./test-e2e.mjs";
/**
* @import { BuildConfig, PlatformConfig, TargetPlatform } from "../types.js";
*/
const DEFAULT_PLATFORMS = ["android", "ios"];
const TEST_VARIANTS = /** @type {const} */ (["paper", "fabric"]);
/** @type {Record<TargetPlatform, PlatformConfig>} */
const PLATFORM_CONFIG = {
android: {
name: "Android",
engines: ["hermes"],
isAvailable: ({ engine }) => engine === "hermes",
prebuild: ({ variant }) => {
if (variant === "fabric") {
const properties = "android/gradle.properties";
const content = readTextFile(properties);
fs.writeFileSync(
properties,
content.replace("#newArchEnabled=true", "newArchEnabled=true")
);
}
return Promise.resolve();
},
},
ios: {
name: "iOS",
engines: ["jsc", "hermes"],
isAvailable: () => process.platform === "darwin",
prebuild: installPods,
},
macos: {
name: "macOS",
engines: ["jsc", "hermes"],
isAvailable: () => false,
prebuild: installPods,
},
visionos: {
name: "visionOS",
engines: ["jsc", "hermes"],
isAvailable: () => false,
prebuild: installPods,
},
windows: {
name: "Windows",
engines: ["hermes"],
isAvailable: () => false,
prebuild: () => Promise.resolve(),
},
};
const PACKAGE_MANAGER = "yarn";
const TAG = "┃";
const rootDir = fileURLToPath(new URL("../..", import.meta.url));
function log(message = "", tag = TAG) {
console.log(tag, message);
}
/**
* Invokes `npm run` and redirects stdout/stderr to specified file.
* @param {string} script
* @param {string} logPath
*/
function run(script, logPath) {
const fd = fs.openSync(logPath, "a", 0o644);
const proc = spawn(PACKAGE_MANAGER, ["run", script], {
stdio: ["ignore", fd, fd],
});
return proc;
}
/**
* @param {string} message
*/
function showBanner(message) {
log();
log(message, "┗━━▶");
log("", "");
}
/**
* Starts Appium server.
*/
function startAppiumServer(logPath = "appium.log") {
log(`Appium log path: ${logPath}`);
return run("appium", logPath);
}
/**
* Starts Metro dev server.
*/
function startDevServer(logPath = "metro.server.log") {
log(`Metro log path: ${logPath}`);
return run("start", logPath);
}
/**
* @param {string[]} platforms
* @returns {TargetPlatform[]}
*/
function validatePlatforms(platforms) {
/** @type {TargetPlatform[]} */
const filtered = [];
for (const platform of platforms) {
switch (platform) {
case "android":
case "ios":
filtered.push(platform);
break;
case "macos":
case "visionos":
case "windows":
log(yellow(`⚠ Unsupported platform: ${platform}`));
break;
default:
log(yellow(`⚠ Unknown platform: ${platform}`));
break;
}
}
return filtered;
}
/**
* @param {string[]} args
*/
function parseArgs(args) {
const { values, positionals } = util.parseArgs({
args,
options: {
android: {
description: "Test Android",
type: "boolean",
},
ios: {
description: "Test iOS",
type: "boolean",
},
},
strict: true,
allowPositionals: true,
tokens: false,
});
const flags = Object.keys(values);
return {
version: positionals[0],
platforms: validatePlatforms(
flags.length === 0 ? DEFAULT_PLATFORMS : flags
),
};
}
function prestart() {
return !process.stdin.isTTY
? Promise.resolve()
: new Promise((resolve) => {
const stdin = process.stdin;
const rawMode = stdin.isRaw;
const encoding = stdin.readableEncoding || undefined;
stdin.setRawMode(true);
stdin.setEncoding("utf-8");
stdin.resume();
stdin.once("data", (key) => {
process.stdout.write("\n");
stdin.pause();
stdin.setEncoding(encoding);
stdin.setRawMode(rawMode);
if (typeof key === "string" && key === "\u0003") {
showBanner("❌ Canceled");
// eslint-disable-next-line local/no-process-exit
process.exit(1);
}
resolve(true);
});
process.stdout.write(
`${TAG} Before continuing, make sure all emulators/simulators and Appium/Metro instances are closed.\n${TAG}\n${TAG} Press any key to continue...`
);
});
}
/**
* Invokes `react-native run-<platform>`.
* @param {TargetPlatform} platform
*/
function buildAndRun(platform) {
switch (platform) {
case "ios": {
const simulator = getIOSSimulatorName();
$(PACKAGE_MANAGER, platform, "--simulator", simulator, "--no-packager");
break;
}
default: {
$(PACKAGE_MANAGER, platform, "--no-packager");
break;
}
}
}
/**
* @param {BuildConfig} config
*/
async function buildRunTest({ platform, variant }) {
const setup = PLATFORM_CONFIG[platform];
if (!setup) {
log(yellow(`⚠ Unknown platform: ${platform}`));
return;
}
for (const engine of setup.engines) {
const configWithEngine = { platform, variant, engine };
if (!setup.isAvailable(configWithEngine)) {
continue;
}
showBanner(`Build ${setup.name} [${variant}, ${engine}]`);
await setup.prebuild(configWithEngine);
buildAndRun(platform);
await test(platform, [variant, engine]);
}
}
/**
* @param {string} rootDir
*/
function reset(rootDir) {
log("Resetting...");
process.chdir(rootDir);
try {
$$(process.platform === "win32" ? "tskill" : "killall", "watchman");
$$("watchman", "watch-del-all", rootDir);
} catch (_) {
// Watchman may not be installed
}
$("git", "checkout", "--quiet", ".");
$(
"git",
"clean",
"-dfqx",
"--exclude=.yarn/cache",
"--exclude=example/*.png"
);
}
/**
* Invokes callback within the context of specified React Native version.
* @param {string} version
* @param {() => Promise<void>} action
*/
async function withReactNativeVersion(version, action) {
reset(rootDir);
if (version) {
await setReactVersion(version, true);
} else {
log();
}
$(PACKAGE_MANAGER, "install");
log();
let appiumServer;
let devServer;
try {
process.chdir("example");
appiumServer = startAppiumServer();
devServer = startDevServer();
await action();
} finally {
appiumServer?.kill();
devServer?.kill();
}
}
const { version, platforms } = parseArgs(process.argv.slice(2));
if (platforms.length === 0) {
process.exitCode = 1;
showBanner(red("No valid platforms were specified"));
} else {
TEST_VARIANTS.reduce((job, variant) => {
return job.then(() =>
withReactNativeVersion(version, async () => {
for (const platform of platforms) {
await buildRunTest({ platform, variant });
}
})
);
}, prestart())
.then(() => {
showBanner(`Initialize new app`);
$(
PACKAGE_MANAGER,
"init-test-app",
"--destination",
"template-example",
"--name",
"TemplateExample",
"--platform",
"android",
"--platform",
"ios"
);
})
.then(() => {
showBanner(`Reconfigure existing app`);
const args = [
"configure-test-app",
"-p",
"android",
"-p",
"ios",
"-p",
"macos",
"-p",
"visionos",
"-p",
"windows",
];
const { status } = spawnSync(PACKAGE_MANAGER, args, { stdio: "inherit" });
if (status !== 1) {
throw new Error("Expected an error");
}
})
.then(() => {
showBanner(green("✔ Pass"));
});
}