crash-spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { expect } from 'chai';
  2. import * as cp from 'child_process';
  3. import * as fs from 'fs';
  4. import * as path from 'path';
  5. import { ifit } from './spec-helpers';
  6. const fixturePath = path.resolve(__dirname, 'fixtures', 'crash-cases');
  7. let children: cp.ChildProcessWithoutNullStreams[] = [];
  8. const runFixtureAndEnsureCleanExit = async (args: string[]) => {
  9. let out = '';
  10. const child = cp.spawn(process.execPath, args);
  11. children.push(child);
  12. child.stdout.on('data', (chunk: Buffer) => {
  13. out += chunk.toString();
  14. });
  15. child.stderr.on('data', (chunk: Buffer) => {
  16. out += chunk.toString();
  17. });
  18. type CodeAndSignal = {code: number | null, signal: NodeJS.Signals | null};
  19. const { code, signal } = await new Promise<CodeAndSignal>((resolve) => {
  20. child.on('exit', (code, signal) => {
  21. resolve({ code, signal });
  22. });
  23. });
  24. if (code !== 0 || signal !== null) {
  25. console.error(out);
  26. }
  27. children = children.filter(c => c !== child);
  28. expect(signal).to.equal(null, 'exit signal should be null');
  29. expect(code).to.equal(0, 'should have exited with code 0');
  30. };
  31. const shouldRunCase = (crashCase: string) => {
  32. switch (crashCase) {
  33. // TODO(jkleinsc) fix this flaky test on Windows 32-bit
  34. case 'quit-on-crashed-event': {
  35. return (process.platform !== 'win32' || process.arch !== 'ia32');
  36. }
  37. // TODO(jkleinsc) fix this test on Linux on arm/arm64
  38. case 'js-execute-iframe': {
  39. return (process.platform !== 'linux' || (process.arch !== 'arm64' && process.arch !== 'arm'));
  40. }
  41. default: {
  42. return true;
  43. }
  44. }
  45. };
  46. describe('crash cases', () => {
  47. afterEach(() => {
  48. for (const child of children) {
  49. child.kill();
  50. }
  51. expect(children).to.have.lengthOf(0, 'all child processes should have exited cleanly');
  52. children.length = 0;
  53. });
  54. const cases = fs.readdirSync(fixturePath);
  55. for (const crashCase of cases) {
  56. ifit(shouldRunCase(crashCase))(`the "${crashCase}" case should not crash`, () => {
  57. const fixture = path.resolve(fixturePath, crashCase);
  58. const argsFile = path.resolve(fixture, 'electron.args');
  59. const args = [fixture];
  60. if (fs.existsSync(argsFile)) {
  61. args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
  62. }
  63. return runFixtureAndEnsureCleanExit(args);
  64. });
  65. }
  66. });