test: reverse dumpio into quiet, serialize output (#3491)

This commit is contained in:
Pavel Feldman 2020-08-15 00:19:20 -07:00 committed by GitHub
parent 73d2dc11d7
commit c44f841f33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 37 deletions

View File

@ -109,15 +109,6 @@ class FixturePool {
return fn(params); return fn(params);
} }
patchToEnableFixtures(object, name) {
const original = object[name];
object[name] = fn => {
return original(async () => {
return await this.resolveParametersAndRun(fn);
});
}
}
wrapTestCallback(callback) { wrapTestCallback(callback) {
if (!callback) if (!callback)
return callback; return callback;

View File

@ -58,7 +58,7 @@ function specBuilder(modifiers, specCallback) {
return builder({}, null); return builder({}, null);
} }
function fixturesUI(trialRun, suite) { function fixturesUI(testRunner, suite) {
const suites = [suite]; const suites = [suite];
suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) { suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
@ -69,7 +69,7 @@ function fixturesUI(trialRun, suite) {
if (suite.isPending()) if (suite.isPending())
fn = null; fn = null;
let wrapper; let wrapper;
if (trialRun) { if (testRunner.trialRun) {
if (fn) if (fn)
wrapper = () => {}; wrapper = () => {};
} else { } else {
@ -113,17 +113,20 @@ function fixturesUI(trialRun, suite) {
return suite; return suite;
}); });
context.beforeEach = common.beforeEach; context.beforeEach = (fn) => {
context.afterEach = common.afterEach; if (testRunner.trialRun)
if (trialRun) { return;
context.beforeEach = () => {}; return common.beforeEach(async () => {
context.afterEach = () => {}; return await fixturePool.resolveParametersAndRun(fn);
} else { });
context.beforeEach = common.beforeEach; };
context.afterEach = common.afterEach; context.afterEach = (fn) => {
fixturePool.patchToEnableFixtures(context, 'beforeEach'); if (testRunner.trialRun)
fixturePool.patchToEnableFixtures(context, 'afterEach'); return;
} return common.afterEach(async () => {
return await fixturePool.resolveParametersAndRun(fn);
});
};
context.run = mocha.options.delay && common.runWithSuite(suite); context.run = mocha.options.delay && common.runWithSuite(suite);

View File

@ -29,7 +29,7 @@ program
.option('-j, --jobs <jobs>', 'Number of concurrent jobs for --parallel; use 1 to run in serial, default: (number of CPU cores / 2)', Math.ceil(require('os').cpus().length / 2)) .option('-j, --jobs <jobs>', 'Number of concurrent jobs for --parallel; use 1 to run in serial, default: (number of CPU cores / 2)', Math.ceil(require('os').cpus().length / 2))
.option('--reporter <reporter>', 'Specify reporter to use', '') .option('--reporter <reporter>', 'Specify reporter to use', '')
.option('--trial-run', 'Only collect the matching tests and report them as passing') .option('--trial-run', 'Only collect the matching tests and report them as passing')
.option('--dumpio', 'Dump stdout and stderr from workers', false) .option('--quiet', 'Suppress stdio', false)
.option('--debug', 'Run tests in-process for debugging', false) .option('--debug', 'Run tests in-process for debugging', false)
.option('--timeout <timeout>', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000) .option('--timeout <timeout>', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000)
.action(async (command) => { .action(async (command) => {
@ -70,7 +70,7 @@ program
const jobs = (command.trialRun || command.debug) ? 1 : command.jobs; const jobs = (command.trialRun || command.debug) ? 1 : command.jobs;
const runner = new Runner(rootSuite, { const runner = new Runner(rootSuite, {
debug: command.debug, debug: command.debug,
dumpio: command.dumpio, quiet: command.quiet,
grep: command.grep, grep: command.grep,
jobs, jobs,
reporter: command.reporter, reporter: command.reporter,

View File

@ -197,17 +197,17 @@ class OopWorker extends EventEmitter {
}); });
this.stdout = []; this.stdout = [];
this.stderr = []; this.stderr = [];
this.on('stdout', data => { this.on('stdout', params => {
if (runner._options.dumpio) const chunk = chunkFromParams(params);
process.stdout.write(data); if (!runner._options.quiet)
else process.stdout.write(chunk);
this.stdout.push(data); this.stdout.push(chunk);
}); });
this.on('stderr', data => { this.on('stderr', params => {
if (runner._options.dumpio) const chunk = chunkFromParams(params);
process.stderr.write(data); if (!runner._options.quiet)
else process.stderr.write(chunk);
this.stderr.push(data); this.stderr.push(chunk);
}); });
this.on('debug', data => { this.on('debug', data => {
process.stderr.write(data + '\n'); process.stderr.write(data + '\n');
@ -273,4 +273,10 @@ class InProcessWorker extends EventEmitter {
} }
} }
function chunkFromParams(params) {
if (typeof params === 'string')
return params;
return Buffer.from(params.buffer, 'base64');
}
module.exports = { Runner }; module.exports = { Runner };

View File

@ -39,7 +39,7 @@ class TestRunner extends EventEmitter {
forbidOnly: options.forbidOnly, forbidOnly: options.forbidOnly,
reporter: NullReporter, reporter: NullReporter,
timeout: options.timeout, timeout: options.timeout,
ui: fixturesUI.bind(null, options.trialRun), ui: fixturesUI.bind(null, this),
}); });
if (options.grep) if (options.grep)
this.mocha.grep(options.grep); this.mocha.grep(options.grep);
@ -49,6 +49,7 @@ class TestRunner extends EventEmitter {
this.suite = this.mocha.suite; this.suite = this.mocha.suite;
this._lastOrdinal = -1; this._lastOrdinal = -1;
this._failedWithError = false; this._failedWithError = false;
this.trialRun = options.trialRun;
} }
async run() { async run() {

View File

@ -18,17 +18,26 @@ const debug = require('debug');
const { fixturePool } = require('./fixturesUI'); const { fixturePool } = require('./fixturesUI');
const { gracefullyCloseAll } = require('../../lib/server/processLauncher'); const { gracefullyCloseAll } = require('../../lib/server/processLauncher');
const { TestRunner } = require('./testRunner'); const { TestRunner } = require('./testRunner');
const util = require('util');
let closed = false; let closed = false;
sendMessageToParent('ready'); sendMessageToParent('ready');
function chunkToParams(chunk) {
if (chunk instanceof Buffer)
return { buffer: chunk.toString('base64') };
if (typeof chunk !== 'string')
return util.inspect(chunk);
return chunk;
}
process.stdout.write = chunk => { process.stdout.write = chunk => {
sendMessageToParent('stdout', chunk); sendMessageToParent('stdout', chunkToParams(chunk));
}; };
process.stderr.write = chunk => { process.stderr.write = chunk => {
sendMessageToParent('stderr', chunk); sendMessageToParent('stderr', chunkToParams(chunk));
}; };
debug.log = data => { debug.log = data => {