crash-spec.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 = (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. return new Promise<void>((resolve) => {
  19. child.on('exit', (code, signal) => {
  20. if (code !== 0 || signal !== null) {
  21. console.error(out);
  22. }
  23. expect(signal).to.equal(null, 'exit signal should be null');
  24. expect(code).to.equal(0, 'should have exited with code 0');
  25. children = children.filter(c => c !== child);
  26. resolve();
  27. });
  28. });
  29. };
  30. const shouldRunCase = (crashCase: string) => {
  31. switch (crashCase) {
  32. // TODO(jkleinsc) fix this flaky test on Windows 32-bit
  33. case 'quit-on-crashed-event': {
  34. return (process.platform !== 'win32' || process.arch !== 'ia32');
  35. }
  36. // TODO(jkleinsc) fix this test on Linux on arm/arm64
  37. case 'js-execute-iframe': {
  38. return (process.platform !== 'linux' || (process.arch !== 'arm64' && process.arch !== 'arm'));
  39. }
  40. default: {
  41. return true;
  42. }
  43. }
  44. };
  45. describe('crash cases', () => {
  46. afterEach(() => {
  47. for (const child of children) {
  48. child.kill();
  49. }
  50. expect(children).to.have.lengthOf(0, 'all child processes should have exited cleanly');
  51. children.length = 0;
  52. });
  53. const cases = fs.readdirSync(fixturePath);
  54. for (const crashCase of cases) {
  55. ifit(shouldRunCase(crashCase))(`the "${crashCase}" case should not crash`, () => {
  56. const fixture = path.resolve(fixturePath, crashCase);
  57. const argsFile = path.resolve(fixture, 'electron.args');
  58. const args = [fixture];
  59. if (fs.existsSync(argsFile)) {
  60. args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
  61. }
  62. return runFixtureAndEnsureCleanExit(args);
  63. });
  64. }
  65. });