node-flags.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. /**
  3. * Some settings and code related to Mocha's handling of Node.js/V8 flags.
  4. * @private
  5. * @module
  6. */
  7. const nodeFlags = require('node-environment-flags');
  8. const unparse = require('yargs-unparser');
  9. /**
  10. * These flags are considered "debug" flags.
  11. * @see {@link impliesNoTimeouts}
  12. * @private
  13. */
  14. const debugFlags = new Set(['debug', 'debug-brk', 'inspect', 'inspect-brk']);
  15. /**
  16. * Mocha has historical support for various `node` and V8 flags which might not
  17. * appear in `process.allowedNodeEnvironmentFlags`.
  18. * These include:
  19. * - `--preserve-symlinks`
  20. * - `--harmony-*`
  21. * - `--gc-global`
  22. * - `--trace-*`
  23. * - `--es-staging`
  24. * - `--use-strict`
  25. * - `--v8-*` (but *not* `--v8-options`)
  26. * @summary Whether or not to pass a flag along to the `node` executable.
  27. * @param {string} flag - Flag to test
  28. * @returns {boolean}
  29. * @private
  30. */
  31. exports.isNodeFlag = flag =>
  32. !/^(?:require|r)$/.test(flag) &&
  33. (nodeFlags.has(flag) ||
  34. debugFlags.has(flag) ||
  35. /(?:preserve-symlinks(?:-main)?|harmony(?:[_-]|$)|(?:trace[_-].+$)|gc(?:[_-]global)?$|es[_-]staging$|use[_-]strict$|v8[_-](?!options).+?$)/.test(
  36. flag
  37. ));
  38. /**
  39. * Returns `true` if the flag is a "debug-like" flag. These require timeouts
  40. * to be suppressed, or pausing the debugger on breakpoints will cause test failures.
  41. * @param {string} flag - Flag to test
  42. * @returns {boolean}
  43. * @private
  44. */
  45. exports.impliesNoTimeouts = flag => debugFlags.has(flag);
  46. /**
  47. * All non-strictly-boolean arguments to node--those with values--must specify those values using `=`, e.g., `--inspect=0.0.0.0`.
  48. * Unparse these arguments using `yargs-unparser` (which would result in `--inspect 0.0.0.0`), then supply `=` where we have values.
  49. * There's probably an easier or more robust way to do this; fixes welcome
  50. * @param {Object} opts - Arguments object
  51. * @returns {string[]} Unparsed arguments using `=` to specify values
  52. * @private
  53. */
  54. exports.unparseNodeFlags = opts => {
  55. var args = unparse(opts);
  56. return args.length
  57. ? args
  58. .join(' ')
  59. .split(/\b/)
  60. .map(arg => (arg === ' ' ? '=' : arg))
  61. .join('')
  62. : [];
  63. };