nan-spec-runner.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 outDir = utils.getOutDir({ shouldLog: true });
  17. const nodeDir = path.resolve(BASE, 'out', outDir, 'gen', 'node_headers');
  18. const env = {
  19. ...process.env,
  20. npm_config_nodedir: nodeDir,
  21. npm_config_msvs_version: '2019',
  22. npm_config_arch: process.env.NPM_CONFIG_ARCH,
  23. npm_config_yes: 'true'
  24. };
  25. const clangDir = path.resolve(BASE, 'third_party', 'llvm-build', 'Release+Asserts', 'bin');
  26. const cc = path.resolve(clangDir, 'clang');
  27. const cxx = path.resolve(clangDir, 'clang++');
  28. const ld = path.resolve(clangDir, 'lld');
  29. const platformFlags = [];
  30. if (process.platform === 'darwin') {
  31. const sdkPath = path.resolve(BASE, 'out', outDir, 'sdk', 'xcode_links');
  32. const sdks = (await fs.promises.readdir(sdkPath)).filter(fileName => fileName.endsWith('.sdk'));
  33. const sdkToUse = sdks[0];
  34. if (!sdkToUse) {
  35. console.error('Could not find an SDK to use for the NAN tests');
  36. process.exit(1);
  37. }
  38. if (sdks.length) {
  39. console.warn(`Multiple SDKs found in the xcode_links directory - using ${sdkToUse}`);
  40. }
  41. platformFlags.push(
  42. `-isysroot ${path.resolve(sdkPath, sdkToUse)}`
  43. );
  44. }
  45. // TODO(ckerr) this is cribbed from read obj/electron/electron_app.ninja.
  46. // Maybe it would be better to have this script literally open up that
  47. // file and pull cflags_cc from it instead of using bespoke code here?
  48. // I think it's unlikely to work; but if it does, it would be more futureproof
  49. const cxxflags = [
  50. '-std=c++17',
  51. '-nostdinc++',
  52. `-I"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++')}"`,
  53. `-isystem"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++', 'trunk', 'include')}"`,
  54. `-isystem"${path.resolve(BASE, 'buildtools', 'third_party', 'libc++abi', 'trunk', 'include')}"`,
  55. '-fPIC',
  56. '-D_LIBCPP_ABI_NAMESPACE=Cr',
  57. ...platformFlags
  58. ].join(' ');
  59. const ldflags = [
  60. '-stdlib=libc++',
  61. '-fuse-ld=lld',
  62. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++abi')}"`,
  63. `-L"${path.resolve(BASE, 'out', outDir, 'obj', 'buildtools', 'third_party', 'libc++')}"`,
  64. '-lc++abi',
  65. ...platformFlags
  66. ].join(' ');
  67. if (process.platform !== 'win32') {
  68. env.CC = cc;
  69. env.CFLAGS = cxxflags;
  70. env.CXX = cxx;
  71. env.LD = ld;
  72. env.CXXFLAGS = cxxflags;
  73. env.LDFLAGS = ldflags;
  74. }
  75. const { status: buildStatus } = cp.spawnSync(NPX_CMD, ['node-gyp', 'rebuild', '--verbose', '--directory', 'test', '-j', 'max'], {
  76. env,
  77. cwd: NAN_DIR,
  78. stdio: 'inherit'
  79. });
  80. if (buildStatus !== 0) {
  81. console.error('Failed to build nan test modules');
  82. return process.exit(buildStatus);
  83. }
  84. const { status: installStatus } = cp.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install'], {
  85. env,
  86. cwd: NAN_DIR,
  87. stdio: 'inherit'
  88. });
  89. if (installStatus !== 0) {
  90. console.error('Failed to install nan node_modules');
  91. return process.exit(installStatus);
  92. }
  93. const onlyTests = args.only && args.only.split(',');
  94. const DISABLED_TESTS = [
  95. 'nannew-test.js',
  96. 'buffer-test.js'
  97. ];
  98. const testsToRun = fs.readdirSync(path.resolve(NAN_DIR, 'test', 'js'))
  99. .filter(test => !DISABLED_TESTS.includes(test))
  100. .filter(test => {
  101. return !onlyTests || onlyTests.includes(test) || onlyTests.includes(test.replace('.js', '')) || onlyTests.includes(test.replace('-test.js', ''));
  102. })
  103. .map(test => `test/js/${test}`);
  104. const testChild = cp.spawn(utils.getAbsoluteElectronExec(), ['node_modules/.bin/tap', ...testsToRun], {
  105. env: {
  106. ...process.env,
  107. ELECTRON_RUN_AS_NODE: 'true'
  108. },
  109. cwd: NAN_DIR,
  110. stdio: 'inherit'
  111. });
  112. testChild.on('exit', (testCode) => {
  113. process.exit(testCode);
  114. });
  115. }
  116. main().catch((err) => {
  117. console.error('An unhandled error occurred in the nan spec runner', err);
  118. process.exit(1);
  119. });