Skip to content

Commit

Permalink
implement option dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 30, 2021
1 parent 1c4e623 commit 5a920c5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 103 deletions.
1 change: 1 addition & 0 deletions lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const TYPES = (exports.types = {
'color',
'delay',
'diff',
'dry-run',
'exit',
'forbid-only',
'forbid-pending',
Expand Down
4 changes: 4 additions & 0 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ exports.builder = yargs =>
description: 'Show diff on failure',
group: GROUPS.OUTPUT
},
'dry-run': {
description: 'Report tests without executing them',
group: GROUPS.RULES
},
exit: {
description: 'Force Mocha to quit after tests complete',
group: GROUPS.RULES
Expand Down
20 changes: 7 additions & 13 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
EVENT_FILE_POST_REQUIRE,
EVENT_FILE_REQUIRE
} = Suite.constants;
var sQuote = utils.sQuote;
var debug = require('debug')('mocha:mocha');

exports = module.exports = Mocha;
Expand Down Expand Up @@ -164,6 +163,7 @@ exports.run = function(...args) {
* @param {boolean} [options.color] - Color TTY output from reporter?
* @param {boolean} [options.delay] - Delay root suite execution?
* @param {boolean} [options.diff] - Show diff on failure?
* @param {boolean} [options.dryRun] - Report tests without running them?
* @param {string} [options.fgrep] - Test filter given string.
* @param {boolean} [options.forbidOnly] - Tests marked `only` fail the suite?
* @param {boolean} [options.forbidPending] - Pending tests fail the suite?
Expand Down Expand Up @@ -346,23 +346,19 @@ Mocha.prototype.reporter = function(reporterName, reporterOptions) {
reporter = require(path.resolve(utils.cwd(), reporterName));
} catch (_err) {
_err.code === 'MODULE_NOT_FOUND'
? warn(sQuote(reporterName) + ' reporter not found')
? warn(`'${reporterName}' reporter not found`)
: warn(
sQuote(reporterName) +
' reporter blew up with error:\n' +
err.stack
`'${reporterName}' reporter blew up with error:\n ${err.stack}`
);
}
} else {
warn(
sQuote(reporterName) + ' reporter blew up with error:\n' + err.stack
);
warn(`'${reporterName}' reporter blew up with error:\n ${err.stack}`);
}
}
}
if (!reporter) {
throw createInvalidReporterError(
'invalid reporter ' + sQuote(reporterName),
`invalid reporter '${reporterName}'`,
reporterName
);
}
Expand Down Expand Up @@ -396,10 +392,7 @@ Mocha.prototype.ui = function(ui) {
try {
bindInterface = require(ui);
} catch (err) {
throw createInvalidInterfaceError(
'invalid interface ' + sQuote(ui),
ui
);
throw createInvalidInterfaceError(`invalid interface '${ui}'`, ui);
}
}
}
Expand Down Expand Up @@ -1016,6 +1009,7 @@ Mocha.prototype.run = function(fn) {
options.files = this.files;
const runner = new this._runnerClass(suite, {
delay: options.delay,
dryRun: options.dryRun,
cleanReferencesAfterRun: this._cleanReferencesAfterRun
});
createStatsCollector(runner);
Expand Down
23 changes: 11 additions & 12 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Module dependencies.
* @private
*/
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Pending = require('./pending');
var utils = require('./utils');
Expand All @@ -19,8 +18,6 @@ var EVENT_ROOT_SUITE_RUN = Suite.constants.EVENT_ROOT_SUITE_RUN;
var STATE_FAILED = Runnable.constants.STATE_FAILED;
var STATE_PASSED = Runnable.constants.STATE_PASSED;
var STATE_PENDING = Runnable.constants.STATE_PENDING;
var dQuote = utils.dQuote;
var sQuote = utils.sQuote;
var stackFilter = utils.stackTraceFilter();
var stringify = utils.stringify;

Expand Down Expand Up @@ -140,6 +137,7 @@ class Runner extends EventEmitter {
* @param {Suite} suite - Root suite
* @param {Object|boolean} [opts] - Options. If `boolean`, whether or not to delay execution of root suite until ready (for backwards compatibility).
* @param {boolean} [opts.delay] - Whether to delay execution of root suite until ready.
* @param {boolean} [opts.dryRun] - Whether to report tests without running them.
* @param {boolean} [opts.cleanReferencesAfterRun] - Whether to clean references to test fns and hooks when a suite is done.
*/
constructor(suite, opts) {
Expand Down Expand Up @@ -407,9 +405,8 @@ Runner.prototype.checkGlobals = function(test) {
this._globals = this._globals.concat(leaks);

if (leaks.length) {
var msg = 'global leak(s) detected: %s';
var error = new Error(util.format(msg, leaks.map(sQuote).join(', ')));
this.fail(test, error);
var msg = `global leak(s) detected: ${leaks.map(e => `'${e}'`).join(', ')}`;
this.fail(test, new Error(msg));
}
};

Expand Down Expand Up @@ -476,6 +473,8 @@ Runner.prototype.fail = function(test, err, force) {
*/

Runner.prototype.hook = function(name, fn) {
if (this._opts.dryRun) return fn();

var suite = this.suite;
var hooks = suite.getHooks(name);
var self = this;
Expand Down Expand Up @@ -554,16 +553,15 @@ Runner.prototype.hook = function(name, fn) {
function setHookTitle(hook) {
hook.originalTitle = hook.originalTitle || hook.title;
if (hook.ctx && hook.ctx.currentTest) {
hook.title =
hook.originalTitle + ' for ' + dQuote(hook.ctx.currentTest.title);
hook.title = `${hook.originalTitle} for "${hook.ctx.currentTest.title}"`;
} else {
var parentTitle;
if (hook.parent.title) {
parentTitle = hook.parent.title;
} else {
parentTitle = hook.parent.root ? '{root}' : '';
}
hook.title = hook.originalTitle + ' in ' + dQuote(parentTitle);
hook.title = `${hook.originalTitle} in "${parentTitle}"`;
}
}
}
Expand Down Expand Up @@ -609,7 +607,7 @@ Runner.prototype.hooks = function(name, suites, fn) {
};

/**
* Run hooks from the top level down.
* Run 'afterEach' hooks from bottom up.
*
* @param {String} name
* @param {Function} fn
Expand All @@ -621,7 +619,7 @@ Runner.prototype.hookUp = function(name, fn) {
};

/**
* Run hooks from the bottom up.
* Run 'beforeEach' hooks from top level down.
*
* @param {String} name
* @param {Function} fn
Expand Down Expand Up @@ -656,6 +654,8 @@ Runner.prototype.parents = function() {
* @private
*/
Runner.prototype.runTest = function(fn) {
if (this._opts.dryRun) return fn();

var self = this;
var test = this.test;

Expand Down Expand Up @@ -701,7 +701,6 @@ Runner.prototype.runTests = function(suite, fn) {
self.suite = after ? errSuite.parent : errSuite;

if (self.suite) {
// call hookUp afterEach
self.hookUp(HOOK_TYPE_AFTER_EACH, function(err2, errSuite2) {
self.suite = orig;
// some hooks may fail even now
Expand Down
38 changes: 0 additions & 38 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,44 +518,6 @@ exports.clamp = function clamp(value, range) {
return Math.min(Math.max(value, range[0]), range[1]);
};

/**
* Single quote text by combining with undirectional ASCII quotation marks.
*
* @description
* Provides a simple means of markup for quoting text to be used in output.
* Use this to quote names of variables, methods, and packages.
*
* <samp>package 'foo' cannot be found</samp>
*
* @private
* @param {string} str - Value to be quoted.
* @returns {string} quoted value
* @example
* sQuote('n') // => 'n'
*/
exports.sQuote = function(str) {
return "'" + str + "'";
};

/**
* Double quote text by combining with undirectional ASCII quotation marks.
*
* @description
* Provides a simple means of markup for quoting text to be used in output.
* Use this to quote names of datatypes, classes, pathnames, and strings.
*
* <samp>argument 'value' must be "string" or "number"</samp>
*
* @private
* @param {string} str - Value to be quoted.
* @returns {string} quoted value
* @example
* dQuote('number') // => "number"
*/
exports.dQuote = function(str) {
return '"' + str + '"';
};

/**
* It's a noop.
* @public
Expand Down
6 changes: 1 addition & 5 deletions test/integration/reporters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ var fs = require('fs');
var crypto = require('crypto');
var path = require('path');
var run = require('./helpers').runMocha;
var utils = require('../../lib/utils');
var dQuote = utils.dQuote;

describe('reporters', function() {
describe('markdown', function() {
Expand Down Expand Up @@ -215,9 +213,7 @@ describe('reporters', function() {
return;
}

var pattern =
'^Error: invalid or unsupported TAP version: ' +
dQuote(invalidTapVersion);
var pattern = `^Error: invalid or unsupported TAP version: "${invalidTapVersion}"`;
expect(res, 'to satisfy', {
code: 1,
output: new RegExp(pattern, 'm')
Expand Down
32 changes: 15 additions & 17 deletions test/reporters/json-stream.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
var events = require('../../').Runner.constants;
var helpers = require('./helpers');
var reporters = require('../../').reporters;
var utils = require('../../lib/utils');

var JSONStream = reporters.JSONStream;
var createMockRunner = helpers.createMockRunner;
var dQuote = utils.dQuote;
var makeExpectedTest = helpers.makeExpectedTest;
var makeRunReporter = helpers.createRunReporterFunction;

Expand Down Expand Up @@ -71,17 +69,17 @@ describe('JSON Stream reporter', function() {
stdout[0],
'to equal',
'["pass",{"title":' +
dQuote(expectedTitle) +
`"${expectedTitle}"` +
',"fullTitle":' +
dQuote(expectedFullTitle) +
`"${expectedFullTitle}"` +
',"file":' +
dQuote(expectedFile) +
`"${expectedFile}"` +
',"duration":' +
expectedDuration +
',"currentRetry":' +
currentRetry +
',"speed":' +
dQuote(expectedSpeed) +
`"${expectedSpeed}"` +
'}]\n'
);
});
Expand All @@ -106,21 +104,21 @@ describe('JSON Stream reporter', function() {
stdout[0],
'to equal',
'["fail",{"title":' +
dQuote(expectedTitle) +
`"${expectedTitle}"` +
',"fullTitle":' +
dQuote(expectedFullTitle) +
`"${expectedFullTitle}"` +
',"file":' +
dQuote(expectedFile) +
`"${expectedFile}"` +
',"duration":' +
expectedDuration +
',"currentRetry":' +
currentRetry +
',"speed":' +
dQuote(expectedSpeed) +
`"${expectedSpeed}"` +
',"err":' +
dQuote(expectedErrorMessage) +
`"${expectedErrorMessage}"` +
',"stack":' +
dQuote(expectedErrorStack) +
`"${expectedErrorStack}"` +
'}]\n'
);
});
Expand All @@ -144,19 +142,19 @@ describe('JSON Stream reporter', function() {
stdout[0],
'to equal',
'["fail",{"title":' +
dQuote(expectedTitle) +
`"${expectedTitle}"` +
',"fullTitle":' +
dQuote(expectedFullTitle) +
`"${expectedFullTitle}"` +
',"file":' +
dQuote(expectedFile) +
`"${expectedFile}"` +
',"duration":' +
expectedDuration +
',"currentRetry":' +
currentRetry +
',"speed":' +
dQuote(expectedSpeed) +
`"${expectedSpeed}"` +
',"err":' +
dQuote(expectedErrorMessage) +
`"${expectedErrorMessage}"` +
',"stack":null}]\n'
);
});
Expand Down
18 changes: 0 additions & 18 deletions test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,24 +669,6 @@ describe('lib/utils', function() {
});
});

describe('sQuote()', function() {
var str = 'xxx';

it('should return its input as string wrapped in single quotes', function() {
var expected = "'xxx'";
expect(utils.sQuote(str), 'to be', expected);
});
});

describe('dQuote()', function() {
var str = 'xxx';

it('should return its input as string wrapped in double quotes', function() {
var expected = '"xxx"';
expect(utils.dQuote(str), 'to be', expected);
});
});

describe('createMap()', function() {
it('should return an object with a null prototype', function() {
expect(Object.getPrototypeOf(utils.createMap()), 'to be', null);
Expand Down

0 comments on commit 5a920c5

Please sign in to comment.