crash-spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { expect } from 'chai';
  2. import * as cp from 'node:child_process';
  3. import * as fs from 'node:fs';
  4. import * as path from 'node:path';
  5. import { ifit, waitUntil } from './lib/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 and 32bit windows
  38. case 'js-execute-iframe': {
  39. if (process.platform === 'win32') {
  40. return process.arch !== 'ia32';
  41. } else {
  42. return (process.platform !== 'linux' || (process.arch !== 'arm64' && process.arch !== 'arm'));
  43. }
  44. }
  45. default: {
  46. return true;
  47. }
  48. }
  49. };
  50. describe('crash cases', () => {
  51. afterEach(async () => {
  52. for (const child of children) {
  53. child.kill();
  54. }
  55. await waitUntil(() => (children.length === 0));
  56. children.length = 0;
  57. });
  58. const cases = fs.readdirSync(fixturePath);
  59. for (const crashCase of cases) {
  60. ifit(shouldRunCase(crashCase))(`the "${crashCase}" case should not crash`, () => {
  61. const fixture = path.resolve(fixturePath, crashCase);
  62. const argsFile = path.resolve(fixture, 'electron.args');
  63. const args = [fixture];
  64. if (fs.existsSync(argsFile)) {
  65. args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
  66. }
  67. return runFixtureAndEnsureCleanExit(args);
  68. });
  69. }
  70. });