node-spec-runner.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const cp = require('child_process');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const args = require('minimist')(process.argv.slice(2), {
  5. boolean: ['default'],
  6. string: ['jUnitDir']
  7. });
  8. const BASE = path.resolve(__dirname, '../..');
  9. const DISABLED_TESTS = require('./node-disabled-tests.json');
  10. const NODE_DIR = path.resolve(BASE, 'third_party', 'electron_node');
  11. const JUNIT_DIR = args.jUnitDir ? path.resolve(args.jUnitDir) : null;
  12. const TAP_FILE_NAME = 'test.tap';
  13. const utils = require('./lib/utils');
  14. if (!process.mainModule) {
  15. throw new Error('Must call the node spec runner directly');
  16. }
  17. const defaultOptions = [
  18. 'tools/test.py',
  19. '-p',
  20. 'tap',
  21. '--logfile',
  22. TAP_FILE_NAME,
  23. '--mode=debug',
  24. 'default',
  25. `--skip-tests=${DISABLED_TESTS.join(',')}`,
  26. '--flaky-tests=dontcare',
  27. '--shell',
  28. utils.getAbsoluteElectronExec(),
  29. '-J'
  30. ];
  31. const getCustomOptions = () => {
  32. let customOptions = ['tools/test.py'];
  33. // Add all custom arguments.
  34. const extra = process.argv.slice(2);
  35. if (extra) {
  36. customOptions = customOptions.concat(extra);
  37. }
  38. // We need this unilaterally or Node.js will try
  39. // to run from out/Release/node.
  40. customOptions = customOptions.concat([
  41. '--shell',
  42. utils.getAbsoluteElectronExec()
  43. ]);
  44. return customOptions;
  45. };
  46. async function main () {
  47. const options = args.default ? defaultOptions : getCustomOptions();
  48. const testChild = cp.spawn('python3', options, {
  49. env: {
  50. ...process.env,
  51. ELECTRON_RUN_AS_NODE: 'true',
  52. ELECTRON_EAGER_ASAR_HOOK_FOR_TESTING: 'true'
  53. },
  54. cwd: NODE_DIR,
  55. stdio: 'inherit'
  56. });
  57. testChild.on('exit', (testCode) => {
  58. if (JUNIT_DIR) {
  59. fs.mkdirSync(JUNIT_DIR);
  60. const converterStream = require('tap-xunit')();
  61. fs.createReadStream(
  62. path.resolve(NODE_DIR, TAP_FILE_NAME)
  63. ).pipe(converterStream).pipe(
  64. fs.createWriteStream(path.resolve(JUNIT_DIR, 'nodejs.xml'))
  65. ).on('close', () => {
  66. process.exit(testCode);
  67. });
  68. }
  69. });
  70. }
  71. main().catch((err) => {
  72. console.error('An unhandled error occurred in the node spec runner', err);
  73. process.exit(1);
  74. });