2020-05-08 18:30:37 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cp from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2021-03-16 21:02:47 +00:00
|
|
|
import { ifdescribe } from './spec-helpers';
|
2020-05-08 18:17:28 +00:00
|
|
|
|
2020-05-08 18:30:37 +00:00
|
|
|
const fixturePath = path.resolve(__dirname, 'fixtures', 'crash-cases');
|
2020-05-08 18:17:28 +00:00
|
|
|
|
2020-05-08 18:30:37 +00:00
|
|
|
let children: cp.ChildProcessWithoutNullStreams[] = [];
|
2020-05-08 18:17:28 +00:00
|
|
|
|
|
|
|
const runFixtureAndEnsureCleanExit = (args: string[]) => {
|
2020-05-08 18:30:37 +00:00
|
|
|
let out = '';
|
|
|
|
const child = cp.spawn(process.execPath, args);
|
|
|
|
children.push(child);
|
2020-05-08 18:17:28 +00:00
|
|
|
child.stdout.on('data', (chunk: Buffer) => {
|
2020-05-08 18:30:37 +00:00
|
|
|
out += chunk.toString();
|
|
|
|
});
|
2020-05-08 18:17:28 +00:00
|
|
|
child.stderr.on('data', (chunk: Buffer) => {
|
2020-05-08 18:30:37 +00:00
|
|
|
out += chunk.toString();
|
|
|
|
});
|
2021-01-22 19:25:47 +00:00
|
|
|
return new Promise<void>((resolve) => {
|
2020-05-08 18:17:28 +00:00
|
|
|
child.on('exit', (code, signal) => {
|
|
|
|
if (code !== 0 || signal !== null) {
|
2020-05-08 18:30:37 +00:00
|
|
|
console.error(out);
|
2020-05-08 18:17:28 +00:00
|
|
|
}
|
2020-05-08 18:30:37 +00:00
|
|
|
expect(signal).to.equal(null, 'exit signal should be null');
|
|
|
|
expect(code).to.equal(0, 'should have exited with code 0');
|
|
|
|
children = children.filter(c => c !== child);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-05-08 18:17:28 +00:00
|
|
|
|
2021-03-16 21:02:47 +00:00
|
|
|
// Running child app under ASan might receive SIGKILL because of OOM.
|
|
|
|
ifdescribe(!process.env.IS_ASAN)('crash cases', () => {
|
2020-05-08 18:17:28 +00:00
|
|
|
afterEach(() => {
|
|
|
|
for (const child of children) {
|
2020-05-08 18:30:37 +00:00
|
|
|
child.kill();
|
2020-05-08 18:17:28 +00:00
|
|
|
}
|
2020-05-08 18:30:37 +00:00
|
|
|
expect(children).to.have.lengthOf(0, 'all child processes should have exited cleanly');
|
|
|
|
children.length = 0;
|
|
|
|
});
|
|
|
|
const cases = fs.readdirSync(fixturePath);
|
2020-05-08 18:17:28 +00:00
|
|
|
|
|
|
|
for (const crashCase of cases) {
|
|
|
|
it(`the "${crashCase}" case should not crash`, () => {
|
2020-05-08 18:30:37 +00:00
|
|
|
const fixture = path.resolve(fixturePath, crashCase);
|
|
|
|
const argsFile = path.resolve(fixture, 'electron.args');
|
|
|
|
const args = [fixture];
|
2020-05-08 18:17:28 +00:00
|
|
|
if (fs.existsSync(argsFile)) {
|
2020-05-08 18:30:37 +00:00
|
|
|
args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
|
2020-05-08 18:17:28 +00:00
|
|
|
}
|
2020-05-08 18:30:37 +00:00
|
|
|
return runFixtureAndEnsureCleanExit(args);
|
|
|
|
});
|
2020-05-08 18:17:28 +00:00
|
|
|
}
|
2020-05-08 18:30:37 +00:00
|
|
|
});
|