signal-desktop/ts/scripts/test-electron.ts

231 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
2021-12-09 08:06:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2024-06-28 00:22:06 +00:00
import { spawn } from 'node:child_process';
import { join } from 'node:path';
import { pipeline } from 'node:stream/promises';
2024-07-22 19:27:09 +00:00
import { cpus, tmpdir } from 'node:os';
import { mkdtemp, rm } from 'node:fs/promises';
2024-06-28 00:22:06 +00:00
import z from 'zod';
import split2 from 'split2';
import logSymbols from 'log-symbols';
import { explodePromise } from '../util/explodePromise';
import { missingCaseError } from '../util/missingCaseError';
2024-07-22 19:27:09 +00:00
import { SECOND } from '../util/durations';
2024-10-02 19:03:10 +00:00
import { parseUnknown } from '../util/schemas';
2021-12-09 08:06:04 +00:00
const ROOT_DIR = join(__dirname, '..', '..');
2024-07-22 19:27:09 +00:00
const WORKER_COUNT = process.env.WORKER_COUNT
? parseInt(process.env.WORKER_COUNT, 10)
: Math.min(8, cpus().length);
2021-12-09 08:06:04 +00:00
const ELECTRON = join(
ROOT_DIR,
'node_modules',
'.bin',
process.platform === 'win32' ? 'electron.cmd' : 'electron'
);
2023-12-08 01:54:33 +00:00
const MAX_RETRIES = 3;
const RETRIABLE_SIGNALS = ['SIGBUS'];
2024-07-22 19:27:09 +00:00
const failureSchema = z.object({
2024-06-28 00:22:06 +00:00
type: z.literal('fail'),
title: z.string().array(),
error: z.string(),
});
2024-07-22 19:27:09 +00:00
type Failure = z.infer<typeof failureSchema>;
2024-06-28 00:22:06 +00:00
const eventSchema = z
.object({
type: z.literal('pass'),
title: z.string().array(),
2024-07-22 19:27:09 +00:00
duration: z.number(),
2024-06-28 00:22:06 +00:00
})
2024-07-22 19:27:09 +00:00
.or(failureSchema)
2024-06-28 00:22:06 +00:00
.or(
z.object({
type: z.literal('end'),
})
);
2024-07-22 19:27:09 +00:00
async function launchElectron(
worker: number,
attempt: number
): Promise<{ pass: number; failures: Array<Failure> }> {
2023-12-08 01:54:33 +00:00
if (attempt > MAX_RETRIES) {
console.error(`Failed after ${MAX_RETRIES} retries, exiting.`);
process.exit(1);
}
2024-07-22 19:27:09 +00:00
if (attempt !== 1) {
console.log(
`Launching electron ${worker} for tests, attempt #${attempt}...`
);
}
const storagePath = await mkdtemp(join(tmpdir(), 'signal-test-'));
const proc = spawn(
ELECTRON,
[
'ci.js',
'--worker',
worker.toString(),
'--worker-count',
WORKER_COUNT.toString(),
...process.argv.slice(2),
],
{
cwd: ROOT_DIR,
env: {
...process.env,
// Setting NODE_ENV to test triggers main.ts to load
// 'test/index.html' instead of 'background.html', which loads the tests
// via `test.js`
NODE_ENV: 'test',
TEST_QUIT_ON_COMPLETE: 'on',
SIGNAL_CI_CONFIG: JSON.stringify({
storagePath,
}),
},
// Since we run `.cmd` file on Windows - use shell
shell: process.platform === 'win32',
}
);
2024-06-28 00:22:06 +00:00
const { resolve, reject, promise: exitPromise } = explodePromise<void>();
let exitSignal: string | undefined;
proc.on('exit', (code, signal) => {
if (code === 0) {
resolve();
} else {
exitSignal = signal || undefined;
reject(new Error(`Exit code: ${code}`));
}
});
let pass = 0;
2024-07-22 19:27:09 +00:00
const failures = new Array<Failure>();
2024-06-28 00:22:06 +00:00
let done = false;
2023-12-08 01:54:33 +00:00
try {
2024-06-28 00:22:06 +00:00
await Promise.all([
exitPromise,
pipeline(
proc.stdout,
split2()
.resume()
.on('data', line => {
if (!line) {
return;
}
const match = line.match(/^ci:test-electron:event=(.*)/);
if (!match) {
2024-07-02 19:56:40 +00:00
const debugMatch = line.match(/ci:test-electron:debug=(.*)?/);
if (debugMatch) {
try {
console.log('DEBUG:', JSON.parse(debugMatch[1]));
} catch {
// pass
}
}
2024-06-28 00:22:06 +00:00
return;
}
2024-10-02 19:03:10 +00:00
const event = parseUnknown(
eventSchema,
JSON.parse(match[1]) as unknown
);
2024-06-28 00:22:06 +00:00
if (event.type === 'pass') {
pass += 1;
2024-07-22 19:27:09 +00:00
process.stdout.write(logSymbols.success);
if (event.duration > SECOND) {
console.error('');
console.error(
` ${logSymbols.warning} ${event.title.join(' ')} ` +
`took ${event.duration}ms`
);
}
2024-06-28 00:22:06 +00:00
} else if (event.type === 'fail') {
failures.push(event);
2024-07-22 19:27:09 +00:00
console.error('');
console.error(` ${logSymbols.error} ${event.title.join(' ')}`);
2024-06-28 00:22:06 +00:00
console.error('');
console.error(event.error);
} else if (event.type === 'end') {
done = true;
} else {
throw missingCaseError(event);
}
})
),
]);
} catch (error) {
if (exitSignal && RETRIABLE_SIGNALS.includes(exitSignal)) {
2024-07-22 19:27:09 +00:00
return launchElectron(worker, attempt + 1);
2024-06-28 00:22:06 +00:00
}
throw error;
2024-07-22 19:27:09 +00:00
} finally {
try {
await rm(storagePath, { recursive: true });
} catch {
// Ignore
}
}
2024-06-28 00:22:06 +00:00
if (!done) {
throw new Error('Tests terminated early!');
}
2021-12-09 08:06:04 +00:00
2024-07-22 19:27:09 +00:00
return { pass, failures };
}
async function main() {
const promises = [];
for (let i = 0; i < WORKER_COUNT; i += 1) {
promises.push(launchElectron(i, 1));
}
const results = await Promise.all(promises);
let pass = 0;
let failures = new Array<Failure>();
for (const result of results) {
pass += result.pass;
failures = failures.concat(result.failures);
}
2024-06-28 00:22:06 +00:00
if (failures.length) {
console.error('');
console.error('Failing tests:');
console.error('');
for (const { title, error } of failures) {
console.log(` ${logSymbols.error} ${title.join(' ')}`);
console.log(error);
console.log('');
}
}
2021-12-09 08:06:04 +00:00
2024-07-22 19:27:09 +00:00
console.log('');
2024-06-28 00:22:06 +00:00
console.log(
`Passed ${pass} | Failed ${failures.length} | ` +
`Total ${pass + failures.length}`
);
2021-12-09 08:06:04 +00:00
2024-06-28 00:22:06 +00:00
if (failures.length !== 0) {
process.exit(1);
}
2021-12-09 08:06:04 +00:00
}
2024-06-28 00:22:06 +00:00
main().catch(error => {
console.error(error);
2021-12-09 08:06:04 +00:00
process.exit(1);
2024-06-28 00:22:06 +00:00
});