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

103 lines
2.5 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
import { execFileSync } from 'child_process';
import { join } from 'path';
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'];
function launchElectron(attempt: number): string {
if (attempt > MAX_RETRIES) {
console.error(`Failed after ${MAX_RETRIES} retries, exiting.`);
process.exit(1);
}
console.log(`Launching electron for tests, attempt #${attempt}...`);
try {
const stdout = execFileSync(ELECTRON, [ROOT_DIR], {
cwd: ROOT_DIR,
env: {
...process.env,
2024-02-29 22:01:12 +00:00
// Setting NODE_ENV to test triggers main.ts to load
// 'test/index.html' instead of 'background.html', which loads the tests
// via `test.js`
2024-02-29 22:01:12 +00:00
NODE_ENV: 'test',
2023-12-08 01:54:33 +00:00
TEST_QUIT_ON_COMPLETE: 'on',
},
2024-06-27 20:25:36 +00:00
// Since we run `.cmd` file on Windows - use shell
shell: process.platform === 'win32',
2023-12-08 01:54:33 +00:00
encoding: 'utf8',
});
return stdout;
} catch (error) {
console.error('Status', error.status);
// In testing, error.signal is null, so we need to read it from stderr
const signalMatch = error.stderr.match(/exited with signal (\w+)/);
const signal = error.signal || signalMatch?.[1];
console.error('Signal', signal);
console.error(error.output[0] ?? '');
console.error(error.output[1] ?? '');
if (RETRIABLE_SIGNALS.includes(signal)) {
return launchElectron(attempt + 1);
}
process.exit(1);
}
2023-01-26 23:53:22 +00:00
}
2021-12-09 08:06:04 +00:00
2023-12-08 01:54:33 +00:00
const stdout = launchElectron(1);
const debugMatch = stdout.matchAll(/ci:test-electron:debug=(.*)?\n/g);
Array.from(debugMatch).forEach(info => {
try {
const args = JSON.parse(info[1]);
console.log('DEBUG:', args);
} catch {
// this section intentionally left blank
}
});
2021-12-09 08:06:04 +00:00
const match = stdout.match(/ci:test-electron:done=(.*)?\n/);
if (!match) {
throw new Error('No test results were found in stdout');
}
const {
passed,
failed,
}: {
passed: Array<string>;
failed: Array<{ testName: string; error: string }>;
} = JSON.parse(match[1]);
const total = passed.length + failed.length;
for (const { testName, error } of failed) {
console.error(`- ${testName}`);
console.error(error);
console.error('');
}
console.log(
`Passed ${passed.length} | Failed ${failed.length} | Total ${total}`
);
if (failed.length !== 0) {
process.exit(1);
}