nan-spec-runner.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const cp = require('child_process');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const BASE = path.resolve(__dirname, '../..');
  5. const NAN_DIR = path.resolve(BASE, 'third_party', 'nan');
  6. const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx';
  7. const utils = require('./lib/utils');
  8. const { YARN_VERSION } = require('./yarn');
  9. if (!process.mainModule) {
  10. throw new Error('Must call the nan spec runner directly');
  11. }
  12. const args = require('minimist')(process.argv.slice(2), {
  13. string: ['only']
  14. });
  15. async function main () {
  16. const nodeDir = path.resolve(BASE, `out/${utils.getOutDir({ shouldLog: true })}/gen/node_headers`);
  17. const env = Object.assign({}, process.env, {
  18. npm_config_nodedir: nodeDir,
  19. npm_config_msvs_version: '2019',
  20. npm_config_arch: process.env.NPM_CONFIG_ARCH,
  21. npm_config_yes: 'true'
  22. });
  23. const clangDir = path.resolve(BASE, 'third_party', 'llvm-build', 'Release+Asserts', 'bin');
  24. const cc = path.resolve(clangDir, 'clang');
  25. const cxx = path.resolve(clangDir, 'clang++');
  26. const ld = path.resolve(clangDir, 'lld');
  27. // TODO(ckerr) this is cribbed from read obj/electron/electron_app.ninja.
  28. // Maybe it would be better to have this script literally open up that
  29. // file and pull cflags_cc from it instead of using bespoke code here?
  30. // I think it's unlikely to work; but if it does, it would be more futureproof
  31. const cxxflags = [
  32. '-std=c++14',
  33. '-nostdinc++',
  34. `-isystem"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++')}"`,
  35. `-isystem"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++', 'trunk', 'include')}"`,
  36. `-isystem"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++abi', 'trunk', 'include')}"`,
  37. '-fPIC'
  38. ].join(' ');
  39. const ldflags = [
  40. '-stdlib=libc++',
  41. '-fuse-ld=lld',
  42. `-L"${path.resolve(BASE, 'out', `${utils.getOutDir({ shouldLog: true })}`, 'obj', 'buildtools', 'third_party', 'libc++abi')}"`,
  43. `-L"${path.resolve(BASE, 'out', `${utils.getOutDir({ shouldLog: true })}`, 'obj', 'buildtools', 'third_party', 'libc++')}"`,
  44. '-lc++abi'
  45. ].join(' ');
  46. if (process.platform !== 'win32') {
  47. env.CC = cc;
  48. env.CFLAGS = cxxflags;
  49. env.CXX = cxx;
  50. env.LD = ld;
  51. env.CXXFLAGS = cxxflags;
  52. env.LDFLAGS = ldflags;
  53. }
  54. const { status: buildStatus } = cp.spawnSync(NPX_CMD, ['node-gyp', 'rebuild', '--verbose', '--directory', 'test', '-j', 'max'], {
  55. env,
  56. cwd: NAN_DIR,
  57. stdio: 'inherit'
  58. });
  59. if (buildStatus !== 0) {
  60. console.error('Failed to build nan test modules');
  61. return process.exit(buildStatus);
  62. }
  63. const { status: installStatus } = cp.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install'], {
  64. env,
  65. cwd: NAN_DIR,
  66. stdio: 'inherit'
  67. });
  68. if (installStatus !== 0) {
  69. console.error('Failed to install nan node_modules');
  70. return process.exit(installStatus);
  71. }
  72. const onlyTests = args.only && args.only.split(',');
  73. const DISABLED_TESTS = [
  74. 'nannew-test.js'
  75. ];
  76. const testsToRun = fs.readdirSync(path.resolve(NAN_DIR, 'test', 'js'))
  77. .filter(test => !DISABLED_TESTS.includes(test))
  78. .filter(test => {
  79. return !onlyTests || onlyTests.includes(test) || onlyTests.includes(test.replace('.js', '')) || onlyTests.includes(test.replace('-test.js', ''));
  80. })
  81. .map(test => `test/js/${test}`);
  82. const testChild = cp.spawn(utils.getAbsoluteElectronExec(), ['node_modules/.bin/tap', ...testsToRun], {
  83. env: {
  84. ...process.env,
  85. ELECTRON_RUN_AS_NODE: 'true'
  86. },
  87. cwd: NAN_DIR,
  88. stdio: 'inherit'
  89. });
  90. testChild.on('exit', (testCode) => {
  91. process.exit(testCode);
  92. });
  93. }
  94. main().catch((err) => {
  95. console.error('An unhandled error occurred in the nan spec runner', err);
  96. process.exit(1);
  97. });