testbinary.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. module.exports = exports = testbinary;
  3. exports.usage = 'Tests that the binary.node can be required';
  4. const path = require('path');
  5. const log = require('npmlog');
  6. const cp = require('child_process');
  7. const versioning = require('./util/versioning.js');
  8. const napi = require('./util/napi.js');
  9. function testbinary(gyp, argv, callback) {
  10. const args = [];
  11. const options = {};
  12. let shell_cmd = process.execPath;
  13. const package_json = gyp.package_json;
  14. const napi_build_version = napi.get_napi_build_version_from_command_args(argv);
  15. const opts = versioning.evaluate(package_json, gyp.opts, napi_build_version);
  16. // skip validation for runtimes we don't explicitly support (like electron)
  17. if (opts.runtime &&
  18. opts.runtime !== 'node-webkit' &&
  19. opts.runtime !== 'node') {
  20. return callback();
  21. }
  22. const nw = (opts.runtime && opts.runtime === 'node-webkit');
  23. // ensure on windows that / are used for require path
  24. const binary_module = opts.module.replace(/\\/g, '/');
  25. if ((process.arch !== opts.target_arch) ||
  26. (process.platform !== opts.target_platform)) {
  27. let msg = 'skipping validation since host platform/arch (';
  28. msg += process.platform + '/' + process.arch + ')';
  29. msg += ' does not match target (';
  30. msg += opts.target_platform + '/' + opts.target_arch + ')';
  31. log.info('validate', msg);
  32. return callback();
  33. }
  34. if (nw) {
  35. options.timeout = 5000;
  36. if (process.platform === 'darwin') {
  37. shell_cmd = 'node-webkit';
  38. } else if (process.platform === 'win32') {
  39. shell_cmd = 'nw.exe';
  40. } else {
  41. shell_cmd = 'nw';
  42. }
  43. const modulePath = path.resolve(binary_module);
  44. const appDir = path.join(__dirname, 'util', 'nw-pre-gyp');
  45. args.push(appDir);
  46. args.push(modulePath);
  47. log.info('validate', "Running test command: '" + shell_cmd + ' ' + args.join(' ') + "'");
  48. cp.execFile(shell_cmd, args, options, (err, stdout, stderr) => {
  49. // check for normal timeout for node-webkit
  50. if (err) {
  51. if (err.killed === true && err.signal && err.signal.indexOf('SIG') > -1) {
  52. return callback();
  53. }
  54. const stderrLog = stderr.toString();
  55. log.info('stderr', stderrLog);
  56. if (/^\s*Xlib:\s*extension\s*"RANDR"\s*missing\s*on\s*display\s*":\d+\.\d+"\.\s*$/.test(stderrLog)) {
  57. log.info('RANDR', 'stderr contains only RANDR error, ignored');
  58. return callback();
  59. }
  60. return callback(err);
  61. }
  62. return callback();
  63. });
  64. return;
  65. }
  66. args.push('--eval');
  67. args.push("require('" + binary_module.replace(/'/g, '\'') + "')");
  68. log.info('validate', "Running test command: '" + shell_cmd + ' ' + args.join(' ') + "'");
  69. cp.execFile(shell_cmd, args, options, (err, stdout, stderr) => {
  70. if (err) {
  71. return callback(err, { stdout: stdout, stderr: stderr });
  72. }
  73. return callback();
  74. });
  75. }