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';
|
|
|
|
import z from 'zod';
|
|
|
|
import split2 from 'split2';
|
|
|
|
import logSymbols from 'log-symbols';
|
|
|
|
|
|
|
|
import { explodePromise } from '../util/explodePromise';
|
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
2021-12-09 08:06:04 +00:00
|
|
|
|
|
|
|
const ROOT_DIR = join(__dirname, '..', '..');
|
|
|
|
|
|
|
|
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-06-28 00:22:06 +00:00
|
|
|
const failSchema = z.object({
|
|
|
|
type: z.literal('fail'),
|
|
|
|
title: z.string().array(),
|
|
|
|
error: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const eventSchema = z
|
|
|
|
.object({
|
|
|
|
type: z.literal('pass'),
|
|
|
|
title: z.string().array(),
|
|
|
|
})
|
|
|
|
.or(failSchema)
|
|
|
|
.or(
|
|
|
|
z.object({
|
|
|
|
type: z.literal('end'),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
async function launchElectron(attempt: number): Promise<void> {
|
2023-12-08 01:54:33 +00:00
|
|
|
if (attempt > MAX_RETRIES) {
|
|
|
|
console.error(`Failed after ${MAX_RETRIES} retries, exiting.`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Launching electron for tests, attempt #${attempt}...`);
|
|
|
|
|
2024-06-28 00:22:06 +00:00
|
|
|
const proc = spawn(ELECTRON, [ROOT_DIR, ...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',
|
|
|
|
},
|
|
|
|
// Since we run `.cmd` file on Windows - use shell
|
|
|
|
shell: process.platform === 'win32',
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
const failures = new Array<z.infer<typeof failSchema>>();
|
|
|
|
let done = false;
|
|
|
|
let stack = new Array<string>();
|
|
|
|
|
|
|
|
function enter(path: ReadonlyArray<string>): void {
|
|
|
|
// Find the first different fragment
|
|
|
|
let i: number;
|
|
|
|
for (i = 0; i < path.length - 1; i += 1) {
|
|
|
|
if (stack[i] !== path[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 01:54:33 +00:00
|
|
|
|
2024-06-28 00:22:06 +00:00
|
|
|
// Separate sections
|
|
|
|
if (i !== stack.length) {
|
|
|
|
console.log('');
|
2023-12-08 01:54:33 +00:00
|
|
|
|
2024-06-28 00:22:06 +00:00
|
|
|
// Remove different fragments
|
|
|
|
stack = stack.slice(0, i);
|
2023-12-08 01:54:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 00:22:06 +00:00
|
|
|
for (; i < path.length - 1; i += 1) {
|
|
|
|
const fragment = path[i];
|
|
|
|
|
|
|
|
console.log(indent(fragment));
|
|
|
|
stack.push(fragment);
|
|
|
|
}
|
2023-12-08 01:54:33 +00:00
|
|
|
}
|
2021-12-09 08:06:04 +00:00
|
|
|
|
2024-06-28 00:22:06 +00:00
|
|
|
function indent(value: string): string {
|
|
|
|
return `${' '.repeat(stack.length)}${value}`;
|
|
|
|
}
|
2023-12-08 01:54:33 +00:00
|
|
|
|
2023-10-04 00:12:57 +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) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = eventSchema.parse(JSON.parse(match[1]));
|
|
|
|
if (event.type === 'pass') {
|
|
|
|
pass += 1;
|
|
|
|
enter(event.title);
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
indent(`${logSymbols.success} ${event.title.at(-1)}`)
|
|
|
|
);
|
|
|
|
} else if (event.type === 'fail') {
|
|
|
|
failures.push(event);
|
|
|
|
enter(event.title);
|
|
|
|
|
|
|
|
console.error(
|
|
|
|
indent(`${logSymbols.error} ${event.title.at(-1)}`)
|
|
|
|
);
|
|
|
|
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)) {
|
|
|
|
return launchElectron(attempt + 1);
|
|
|
|
}
|
|
|
|
throw error;
|
2023-10-04 00:12:57 +00:00
|
|
|
}
|
|
|
|
|
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-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-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
|
|
|
async function main() {
|
|
|
|
await launchElectron(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
|
|
|
});
|