From c44f841f330eccaff455af87c9a9d1df2cd6f68e Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Sat, 15 Aug 2020 00:19:20 -0700 Subject: [PATCH] test: reverse dumpio into quiet, serialize output (#3491) --- test/runner/fixtures.js | 9 --------- test/runner/fixturesUI.js | 29 ++++++++++++++++------------- test/runner/index.js | 4 ++-- test/runner/runner.js | 26 ++++++++++++++++---------- test/runner/testRunner.js | 3 ++- test/runner/worker.js | 13 +++++++++++-- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/test/runner/fixtures.js b/test/runner/fixtures.js index 3c787e5657a73..89deeff705d2f 100644 --- a/test/runner/fixtures.js +++ b/test/runner/fixtures.js @@ -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; diff --git a/test/runner/fixturesUI.js b/test/runner/fixturesUI.js index d997f8adefadf..0f8d55a8763d2 100644 --- a/test/runner/fixturesUI.js +++ b/test/runner/fixturesUI.js @@ -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) { @@ -69,7 +69,7 @@ function fixturesUI(trialRun, suite) { if (suite.isPending()) fn = null; let wrapper; - if (trialRun) { + if (testRunner.trialRun) { if (fn) wrapper = () => {}; } else { @@ -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); diff --git a/test/runner/index.js b/test/runner/index.js index 6e080590bc9e5..91f2b38d14652 100644 --- a/test/runner/index.js +++ b/test/runner/index.js @@ -29,7 +29,7 @@ program .option('-j, --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 ', '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 ', 'Specify test timeout threshold (in milliseconds), default: 10000', 10000) .action(async (command) => { @@ -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, diff --git a/test/runner/runner.js b/test/runner/runner.js index 2cfcf6a3199e6..76aec8f8fb4b2 100644 --- a/test/runner/runner.js +++ b/test/runner/runner.js @@ -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'); @@ -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 }; diff --git a/test/runner/testRunner.js b/test/runner/testRunner.js index 80ebcc6b4dd6e..80725b298f9cd 100644 --- a/test/runner/testRunner.js +++ b/test/runner/testRunner.js @@ -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); @@ -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() { diff --git a/test/runner/worker.js b/test/runner/worker.js index 6081bcdf14e6f..9c1ae81b43ec1 100644 --- a/test/runner/worker.js +++ b/test/runner/worker.js @@ -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 => {