Skip to content

Commit

Permalink
test: reverse dumpio into quiet, serialize output (#3491)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 15, 2020
1 parent 73d2dc1 commit c44f841
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 37 deletions.
9 changes: 0 additions & 9 deletions test/runner/fixtures.js
Expand Up @@ -109,15 +109,6 @@ class FixturePool {
return fn(params);
}

patchToEnableFixtures(object, name) {
const original = object[name];
object[name] = fn => {
return original(async () => {
return await this.resolveParametersAndRun(fn);
});
}
}

wrapTestCallback(callback) {
if (!callback)
return callback;
Expand Down
29 changes: 16 additions & 13 deletions test/runner/fixturesUI.js
Expand Up @@ -58,7 +58,7 @@ function specBuilder(modifiers, specCallback) {
return builder({}, null);
}

function fixturesUI(trialRun, suite) {
function fixturesUI(testRunner, suite) {
const suites = [suite];

suite.on(Suite.constants.EVENT_FILE_PRE_REQUIRE, function(context, file, mocha) {
Expand All @@ -69,7 +69,7 @@ function fixturesUI(trialRun, suite) {
if (suite.isPending())
fn = null;
let wrapper;
if (trialRun) {
if (testRunner.trialRun) {
if (fn)
wrapper = () => {};
} else {
Expand Down Expand Up @@ -113,17 +113,20 @@ function fixturesUI(trialRun, suite) {
return suite;
});

context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
if (trialRun) {
context.beforeEach = () => {};
context.afterEach = () => {};
} else {
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
fixturePool.patchToEnableFixtures(context, 'beforeEach');
fixturePool.patchToEnableFixtures(context, 'afterEach');
}
context.beforeEach = (fn) => {
if (testRunner.trialRun)
return;
return common.beforeEach(async () => {
return await fixturePool.resolveParametersAndRun(fn);
});
};
context.afterEach = (fn) => {
if (testRunner.trialRun)
return;
return common.afterEach(async () => {
return await fixturePool.resolveParametersAndRun(fn);
});
};

context.run = mocha.options.delay && common.runWithSuite(suite);

Expand Down
4 changes: 2 additions & 2 deletions test/runner/index.js
Expand Up @@ -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('--reporter <reporter>', 'Specify reporter to use', '')
.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('--timeout <timeout>', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000)
.action(async (command) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ program
const jobs = (command.trialRun || command.debug) ? 1 : command.jobs;
const runner = new Runner(rootSuite, {
debug: command.debug,
dumpio: command.dumpio,
quiet: command.quiet,
grep: command.grep,
jobs,
reporter: command.reporter,
Expand Down
26 changes: 16 additions & 10 deletions test/runner/runner.js
Expand Up @@ -197,17 +197,17 @@ class OopWorker extends EventEmitter {
});
this.stdout = [];
this.stderr = [];
this.on('stdout', data => {
if (runner._options.dumpio)
process.stdout.write(data);
else
this.stdout.push(data);
this.on('stdout', params => {
const chunk = chunkFromParams(params);
if (!runner._options.quiet)
process.stdout.write(chunk);
this.stdout.push(chunk);
});
this.on('stderr', data => {
if (runner._options.dumpio)
process.stderr.write(data);
else
this.stderr.push(data);
this.on('stderr', params => {
const chunk = chunkFromParams(params);
if (!runner._options.quiet)
process.stderr.write(chunk);
this.stderr.push(chunk);
});
this.on('debug', data => {
process.stderr.write(data + '\n');
Expand Down Expand Up @@ -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 };
3 changes: 2 additions & 1 deletion test/runner/testRunner.js
Expand Up @@ -39,7 +39,7 @@ class TestRunner extends EventEmitter {
forbidOnly: options.forbidOnly,
reporter: NullReporter,
timeout: options.timeout,
ui: fixturesUI.bind(null, options.trialRun),
ui: fixturesUI.bind(null, this),
});
if (options.grep)
this.mocha.grep(options.grep);
Expand All @@ -49,6 +49,7 @@ class TestRunner extends EventEmitter {
this.suite = this.mocha.suite;
this._lastOrdinal = -1;
this._failedWithError = false;
this.trialRun = options.trialRun;
}

async run() {
Expand Down
13 changes: 11 additions & 2 deletions test/runner/worker.js
Expand Up @@ -18,17 +18,26 @@ const debug = require('debug');
const { fixturePool } = require('./fixturesUI');
const { gracefullyCloseAll } = require('../../lib/server/processLauncher');
const { TestRunner } = require('./testRunner');
const util = require('util');

let closed = false;

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 => {
sendMessageToParent('stdout', chunk);
sendMessageToParent('stdout', chunkToParams(chunk));
};

process.stderr.write = chunk => {
sendMessageToParent('stderr', chunk);
sendMessageToParent('stderr', chunkToParams(chunk));
};

debug.log = data => {
Expand Down

0 comments on commit c44f841

Please sign in to comment.