crash-spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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[], customEnv: NodeJS.ProcessEnv) => {
  9. let out = '';
  10. const child = cp.spawn(process.execPath, args, {
  11. env: {
  12. ...process.env,
  13. ...customEnv
  14. }
  15. });
  16. children.push(child);
  17. child.stdout.on('data', (chunk: Buffer) => {
  18. out += chunk.toString();
  19. });
  20. child.stderr.on('data', (chunk: Buffer) => {
  21. out += chunk.toString();
  22. });
  23. type CodeAndSignal = {code: number | null, signal: NodeJS.Signals | null};
  24. const { code, signal } = await new Promise<CodeAndSignal>((resolve) => {
  25. child.on('exit', (code, signal) => {
  26. resolve({ code, signal });
  27. });
  28. });
  29. if (code !== 0 || signal !== null) {
  30. console.error(out);
  31. }
  32. children = children.filter(c => c !== child);
  33. expect(signal).to.equal(null, 'exit signal should be null');
  34. expect(code).to.equal(0, 'should have exited with code 0');
  35. };
  36. const shouldRunCase = (crashCase: string) => {
  37. switch (crashCase) {
  38. // TODO(jkleinsc) fix this flaky test on Windows 32-bit
  39. case 'quit-on-crashed-event': {
  40. return (process.platform !== 'win32' || process.arch !== 'ia32');
  41. }
  42. // TODO(jkleinsc) fix this test on Linux on arm/arm64 and 32bit windows
  43. case 'js-execute-iframe': {
  44. if (process.platform === 'win32') {
  45. return process.arch !== 'ia32';
  46. } else {
  47. return (process.platform !== 'linux' || (process.arch !== 'arm64' && process.arch !== 'arm'));
  48. }
  49. }
  50. default: {
  51. return true;
  52. }
  53. }
  54. };
  55. describe('crash cases', () => {
  56. afterEach(async () => {
  57. for (const child of children) {
  58. child.kill();
  59. }
  60. await waitUntil(() => (children.length === 0));
  61. children.length = 0;
  62. });
  63. const cases = fs.readdirSync(fixturePath);
  64. for (const crashCase of cases) {
  65. ifit(shouldRunCase(crashCase))(`the "${crashCase}" case should not crash`, () => {
  66. const fixture = path.resolve(fixturePath, crashCase);
  67. const argsFile = path.resolve(fixture, 'electron.args');
  68. const envFile = path.resolve(fixture, 'electron.env.json');
  69. const args = [fixture];
  70. let env = process.env;
  71. if (fs.existsSync(argsFile)) {
  72. args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
  73. }
  74. if (fs.existsSync(envFile)) {
  75. env = JSON.parse(fs.readFileSync(envFile, 'utf8'));
  76. }
  77. return runFixtureAndEnsureCleanExit(args, env);
  78. });
  79. }
  80. });