Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bin/options.js): Improve readability and documentation #3533

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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)
Expand Down