node-flags.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 = process.allowedNodeEnvironmentFlags;
  8. const {isMochaFlag} = require('./run-option-metadata');
  9. const unparse = require('yargs-unparser');
  10. /**
  11. * These flags are considered "debug" flags.
  12. * @see {@link impliesNoTimeouts}
  13. * @private
  14. */
  15. const debugFlags = new Set(['inspect', 'inspect-brk']);
  16. /**
  17. * Mocha has historical support for various `node` and V8 flags which might not
  18. * appear in `process.allowedNodeEnvironmentFlags`.
  19. * These include:
  20. * - `--preserve-symlinks`
  21. * - `--harmony-*`
  22. * - `--gc-global`
  23. * - `--trace-*`
  24. * - `--es-staging`
  25. * - `--use-strict`
  26. * - `--v8-*` (but *not* `--v8-options`)
  27. * @summary Whether or not to pass a flag along to the `node` executable.
  28. * @param {string} flag - Flag to test
  29. * @param {boolean} [bareword=true] - If `false`, we expect `flag` to have one or two leading dashes.
  30. * @returns {boolean} If the flag is considered a "Node" flag.
  31. * @private
  32. */
  33. exports.isNodeFlag = (flag, bareword = true) => {
  34. if (!bareword) {
  35. // check if the flag begins with dashes; if not, not a node flag.
  36. if (!/^--?/.test(flag)) {
  37. return false;
  38. }
  39. // strip the leading dashes to match against subsequent checks
  40. flag = flag.replace(/^--?/, '');
  41. }
  42. return (
  43. // check actual node flags from `process.allowedNodeEnvironmentFlags`,
  44. // then historical support for various V8 and non-`NODE_OPTIONS` flags
  45. // and also any V8 flags with `--v8-` prefix
  46. (!isMochaFlag(flag) && nodeFlags && nodeFlags.has(flag)) ||
  47. debugFlags.has(flag) ||
  48. /(?:preserve-symlinks(?:-main)?|harmony(?:[_-]|$)|(?:trace[_-].+$)|gc[_-]global$|es[_-]staging$|use[_-]strict$|v8[_-](?!options).+?$)/.test(
  49. flag
  50. )
  51. );
  52. };
  53. /**
  54. * Returns `true` if the flag is a "debug-like" flag. These require timeouts
  55. * to be suppressed, or pausing the debugger on breakpoints will cause test failures.
  56. * @param {string} flag - Flag to test
  57. * @returns {boolean}
  58. * @private
  59. */
  60. exports.impliesNoTimeouts = flag => debugFlags.has(flag);
  61. /**
  62. * All non-strictly-boolean arguments to node--those with values--must specify those values using `=`, e.g., `--inspect=0.0.0.0`.
  63. * Unparse these arguments using `yargs-unparser` (which would result in `--inspect 0.0.0.0`), then supply `=` where we have values.
  64. * There's probably an easier or more robust way to do this; fixes welcome
  65. * @param {Object} opts - Arguments object
  66. * @returns {string[]} Unparsed arguments using `=` to specify values
  67. * @private
  68. */
  69. exports.unparseNodeFlags = opts => {
  70. var args = unparse(opts);
  71. return args.length
  72. ? args
  73. .join(' ')
  74. .split(/\b/)
  75. .map(arg => (arg === ' ' ? '=' : arg))
  76. .join('')
  77. .split(' ')
  78. : [];
  79. };