/
options.js
110 lines (98 loc) · 2.43 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
/**
* Dependencies.
*/
const fs = require('fs');
const path = require('path');
/**
* Export `getOptions`.
*/
module.exports = getOptions;
/**
* Default test directory.
*
* @constant
* @type {string}
* @default
*/
const DEFAULT_TEST_DIRECTORY = 'test';
/**
* Default filename of run-control file.
*
* @constant
* @type {string}
* @default
*/
const DEFAULT_OPTS_FILENAME = '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 &&
(process.argv[2] === '-h' || process.argv[2] === '--help')
) {
return;
}
const optsIndex = process.argv.indexOf('--opts');
const optsPathSpecified = optsIndex !== -1;
const defaultOptsPath = path.join(
DEFAULT_TEST_DIRECTORY,
DEFAULT_OPTS_FILENAME
);
const optsPath = optsPathSpecified
? process.argv[optsIndex + 1]
: defaultOptsPath;
try {
const opts = parseOptions(readOptionsFile(optsPath));
if (opts.length > 0) {
process.argv = process.argv
.slice(0, 2)
.concat(opts.concat(process.argv.slice(2)));
}
} catch (err) {
// Default options file may not exist - rethrow anything else
if (optsPathSpecified || err.code !== 'ENOENT') {
console.error(`failed to load Mocha options file: ${optsPath}`);
throw err;
}
} finally {
// Despite its name, signifies loading was attempted and should not be again
process.env.LOADED_MOCHA_OPTS = '1';
}
}