node-spec-runner.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. '--shell',
  27. utils.getAbsoluteElectronExec(),
  28. '-J'
  29. ];
  30. const getCustomOptions = () => {
  31. let customOptions = ['tools/test.py'];
  32. // Add all custom arguments.
  33. const extra = process.argv.slice(2);
  34. if (extra) {
  35. customOptions = customOptions.concat(extra);
  36. }
  37. // We need this unilaterally or Node.js will try
  38. // to run from out/Release/node.
  39. customOptions = customOptions.concat([
  40. '--shell',
  41. utils.getAbsoluteElectronExec()
  42. ]);
  43. return customOptions;
  44. };
  45. async function main () {
  46. const options = args.default ? defaultOptions : getCustomOptions();
  47. const testChild = cp.spawn('python', options, {
  48. env: {
  49. ...process.env,
  50. ELECTRON_RUN_AS_NODE: 'true',
  51. ELECTRON_EAGER_ASAR_HOOK_FOR_TESTING: 'true'
  52. },
  53. cwd: NODE_DIR,
  54. stdio: 'inherit'
  55. });
  56. testChild.on('exit', (testCode) => {
  57. if (JUNIT_DIR) {
  58. fs.mkdirSync(JUNIT_DIR);
  59. const converterStream = require('tap-xunit')();
  60. fs.createReadStream(
  61. path.resolve(NODE_DIR, TAP_FILE_NAME)
  62. ).pipe(converterStream).pipe(
  63. fs.createWriteStream(path.resolve(JUNIT_DIR, 'nodejs.xml'))
  64. ).on('close', () => {
  65. process.exit(testCode);
  66. });
  67. }
  68. });
  69. }
  70. main().catch((err) => {
  71. console.error('An unhandled error occurred in the node spec runner', err);
  72. process.exit(1);
  73. });