☕️ Classic, reliable, trusted test framework for Node.js and the browser
refactor(bin/options.js): Refactor and improve documentation (#3533)
P. Roebuck835ac33e3b76
diff --git a/bin/options.js b/bin/options.jsindex a25a18e2..0c27ae5f 100644--- a/bin/options.js+++ b/bin/options.js@@ -13,9 +13,54 @@ const fs = require('fs');module.exports = getOptions;/**- * Get options.+ * Default pathname for run-control file.+ *+ * @constant+ * @type {string}+ * @default*/+const defaultPathname = 'test/mocha.opts';+/**+ * Reads contents of the run-control file.+ *+ * @private+ * @param {string} pathname - Pathname of run-control file.+ * @returns {string} file contents+ */+function readOptionsFile(pathname) {+ return fs.readFileSync(pathname, 'utf8');+}++/**+ * Parses options read from run-control file.+ *+ * @private+ * @param {string} content - Content read from run-control file.+ * @returns {string[]} cmdline options (and associated arguments)+ */+function parseOptions(content) {+ /*+ * Replaces comments with empty strings+ * Replaces escaped spaces (e.g., 'xxx\ yyy') with HTML space+ * Splits on whitespace, creating array of substrings+ * Filters empty string elements from array+ * Replaces any HTML space with space+ */+ return content+ .replace(/^#.*$/gm, '')+ .replace(/\\\s/g, '%20')+ .split(/\s/)+ .filter(Boolean)+ .map(value => value.replace(/%20/g, ' '));+}++/**+ * Prepends options from run-control file to the command line arguments.+ *+ * @public+ * @see {@link https://mochajs.org/#mochaopts|mocha.opts}+ */function getOptions() {if (process.argv.length === 3 &&@@ -26,17 +71,11 @@ function getOptions() {const optsPath =process.argv.indexOf('--opts') === -1- ? 'test/mocha.opts'+ ? defaultPathname: process.argv[process.argv.indexOf('--opts') + 1];try {- const opts = fs- .readFileSync(optsPath, 'utf8')- .replace(/^#.*$/gm, '')- .replace(/\\\s/g, '%20')- .split(/\s/)- .filter(Boolean)- .map(value => value.replace(/%20/g, ' '));+ const opts = parseOptions(readOptionsFile(optsPath));process.argv = process.argv.slice(0, 2)