crash-spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. const fixturePath = path.resolve(__dirname, 'fixtures', 'crash-cases');
  6. let children: cp.ChildProcessWithoutNullStreams[] = [];
  7. const runFixtureAndEnsureCleanExit = (args: string[]) => {
  8. let out = '';
  9. const child = cp.spawn(process.execPath, args);
  10. children.push(child);
  11. child.stdout.on('data', (chunk: Buffer) => {
  12. out += chunk.toString();
  13. });
  14. child.stderr.on('data', (chunk: Buffer) => {
  15. out += chunk.toString();
  16. });
  17. return new Promise<void>((resolve) => {
  18. child.on('exit', (code, signal) => {
  19. if (code !== 0 || signal !== null) {
  20. console.error(out);
  21. }
  22. expect(signal).to.equal(null, 'exit signal should be null');
  23. expect(code).to.equal(0, 'should have exited with code 0');
  24. children = children.filter(c => c !== child);
  25. resolve();
  26. });
  27. });
  28. };
  29. describe('crash cases', () => {
  30. afterEach(() => {
  31. for (const child of children) {
  32. child.kill();
  33. }
  34. expect(children).to.have.lengthOf(0, 'all child processes should have exited cleanly');
  35. children.length = 0;
  36. });
  37. const cases = fs.readdirSync(fixturePath);
  38. for (const crashCase of cases) {
  39. it(`the "${crashCase}" case should not crash`, () => {
  40. const fixture = path.resolve(fixturePath, crashCase);
  41. const argsFile = path.resolve(fixture, 'electron.args');
  42. const args = [fixture];
  43. if (fs.existsSync(argsFile)) {
  44. args.push(...fs.readFileSync(argsFile, 'utf8').trim().split('\n'));
  45. }
  46. return runFixtureAndEnsureCleanExit(args);
  47. });
  48. }
  49. });