Skip to content

Commit

Permalink
fix improper warnings for invalid reporters (#4275)
Browse files Browse the repository at this point in the history
* fix improper warnings for invalid reporters

    also: reorganize, add, and refactor a bunch of problematic unit tests for `lib/mocha.js`.  better isolation except where we can't really do that (calling `require`)

    there's still missing tests in here, but this is an improvement.

* restrict use of Object.assign in ESLint config
* add wrapper around process.cwd()
    - better isolation
    - a use of `process.cwd()` in `lib/runner.js` was left in place to avoid conflicts, because another PR will remove it
  • Loading branch information
boneskull committed May 14, 2020
1 parent 81e203c commit 1a4646d
Show file tree
Hide file tree
Showing 11 changed files with 900 additions and 554 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.yml
Expand Up @@ -14,6 +14,11 @@ rules:
strict:
- error
- safe
# disallow Object.assign
no-restricted-properties:
- error
- object: 'Object'
property: 'assign'
overrides:
- files:
- scripts/**/*.js
Expand All @@ -31,6 +36,11 @@ overrides:
ecmaVersion: 2018
env:
browser: false
rules:
no-restricted-properties:
- off
- object: 'Object'
property: 'assign'
- files:
- esm-utils.js
parserOptions:
Expand Down Expand Up @@ -79,7 +89,6 @@ overrides:
# disallow property access of `global.<timer>.*`
- selector: '*[object.object.name=global][object.property.name=/(Date|(set|clear)(Timeout|Immediate|Interval))/]:expression'
message: *GH-237

- files:
- test/**/*.mjs
parserOptions:
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/cli.js
Expand Up @@ -19,6 +19,7 @@ const {loadOptions, YARGS_PARSER_CONFIG} = require('./options');
const commands = require('./commands');
const ansi = require('ansi-colors');
const {repository, homepage, version, gitter} = require('../../package.json');
const {cwd} = require('../utils');

/**
* - Accepts an `Array` of arguments
Expand All @@ -31,7 +32,7 @@ exports.main = (argv = process.argv.slice(2)) => {
debug('entered main with raw args', argv);
// ensure we can require() from current working directory
if (typeof module.paths !== 'undefined') {
module.paths.push(process.cwd(), path.resolve('node_modules'));
module.paths.push(cwd(), path.resolve('node_modules'));
}

Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/config.js
Expand Up @@ -11,6 +11,7 @@ const fs = require('fs');
const path = require('path');
const debug = require('debug')('mocha:cli:config');
const findUp = require('find-up');
const utils = require('../utils');

/**
* These are the valid config files, in order of precedence;
Expand Down Expand Up @@ -92,7 +93,7 @@ exports.loadConfig = filepath => {
* @param {string} [cwd] - Current working directory
* @returns {string|null} Filepath to config, if found
*/
exports.findConfig = (cwd = process.cwd()) => {
exports.findConfig = (cwd = utils.cwd()) => {
const filepath = findUp.sync(exports.CONFIG_FILES, {cwd});
if (filepath) {
debug('findConfig: found config file %s', filepath);
Expand Down
18 changes: 9 additions & 9 deletions lib/mocha.js
Expand Up @@ -58,7 +58,7 @@ var mochaStates = utils.defineConstants({
*/

if (!process.browser && typeof module.paths !== 'undefined') {
var cwd = process.cwd();
var cwd = utils.cwd();
module.paths.push(cwd, path.join(cwd, 'node_modules'));
}

Expand Down Expand Up @@ -231,24 +231,24 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
_reporter = require(reporter);
} catch (err) {
if (
err.code !== 'MODULE_NOT_FOUND' ||
err.message.indexOf('Cannot find module') !== -1
err.code === 'MODULE_NOT_FOUND' ||
err.message.indexOf('Cannot find module') >= 0
) {
// Try to load reporters from a path (absolute or relative)
try {
_reporter = require(path.resolve(process.cwd(), reporter));
_reporter = require(path.resolve(utils.cwd(), reporter));
} catch (_err) {
_err.code !== 'MODULE_NOT_FOUND' ||
_err.message.indexOf('Cannot find module') !== -1
? console.warn(sQuote(reporter) + ' reporter not found')
: console.warn(
_err.code === 'MODULE_NOT_FOUND' ||
_err.message.indexOf('Cannot find module') >= 0
? utils.warn(sQuote(reporter) + ' reporter not found')
: utils.warn(
sQuote(reporter) +
' reporter blew up with error:\n' +
err.stack
);
}
} else {
console.warn(
utils.warn(
sQuote(reporter) + ' reporter blew up with error:\n' + err.stack
);
}
Expand Down
12 changes: 11 additions & 1 deletion lib/utils.js
Expand Up @@ -637,7 +637,7 @@ exports.stackTraceFilter = function() {
var slash = path.sep;
var cwd;
if (is.node) {
cwd = process.cwd() + slash;
cwd = exports.cwd() + slash;
} else {
cwd = (typeof location === 'undefined'
? window.location
Expand Down Expand Up @@ -821,3 +821,13 @@ exports.supportsEsModules = function() {
}
}
};

/**
* Returns current working directory
*
* Wrapper around `process.cwd()` for isolation
* @private
*/
exports.cwd = function cwd() {
return process.cwd();
};

0 comments on commit 1a4646d

Please sign in to comment.