Skip to content

Commit

Permalink
add wrapper around process.cwd()
Browse files Browse the repository at this point in the history
- 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 4f01095 commit 0bd7e14
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
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
4 changes: 2 additions & 2 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 @@ -236,7 +236,7 @@ Mocha.prototype.reporter = function(reporter, reporterOptions) {
) {
// Try to load reporters from a path (absolute or relative)
try {
_reporter = require(path.resolve(reporter));
_reporter = require(path.resolve(utils.cwd(), reporter));
} catch (_err) {
_err.code === 'MODULE_NOT_FOUND' ||
_err.message.indexOf('Cannot find module') >= 0
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();
};
10 changes: 5 additions & 5 deletions test/node-unit/mocha.spec.js
Expand Up @@ -26,7 +26,8 @@ describe('Mocha', function() {
supportsEsModules: sandbox.stub().returns(false),
warn: sandbox.stub(),
isString: sandbox.stub(),
noop: sandbox.stub()
noop: sandbox.stub(),
cwd: sandbox.stub().returns(process.cwd())
};
stubs.suite = Object.assign(sandbox.createStubInstance(EventEmitter), {
slow: sandbox.stub(),
Expand Down Expand Up @@ -106,10 +107,9 @@ describe('Mocha', function() {
describe('reporter()', function() {
describe('when a reporter exists relative to the cwd', function() {
beforeEach(function() {
// DANGER!
sandbox
.stub(process, 'cwd')
.returns(path.resolve(__dirname, '..', '..', 'lib', 'reporters'));
stubs.utils.cwd.returns(
path.resolve(__dirname, '..', '..', 'lib', 'reporters')
);
});

it('should load from current working directory', function() {
Expand Down
20 changes: 20 additions & 0 deletions test/node-unit/utils.spec.js
@@ -0,0 +1,20 @@
'use strict';

const rewiremock = require('rewiremock/node');

describe('utils', function() {
let utils;

beforeEach(function() {
// add deps to be mocked as needed to second parameter
utils = rewiremock.proxy('../../lib/utils', {});
});

describe('function', function() {
describe('cwd()', function() {
it('should return the current working directory', function() {
expect(utils.cwd(), 'to be', process.cwd());
});
});
});
});

0 comments on commit 0bd7e14

Please sign in to comment.