From 82dd2c28b8dbf162a1d0420c59861f1cf0c0835e Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 4 Jun 2017 01:24:05 +0800 Subject: [PATCH 1/6] test: add hijackStdout and hijackStderr Add `common.hijackStdout` and `common.hijackStderr` to provide monitor for console output. Refs: https://github.com/nodejs/node/blob/917f86ea353e087af3bc4553219ffabaa2193d20/test/parallel/test-global-console-exists.js#L41-L43 --- test/common/README.md | 10 +++++++ test/common/index.js | 16 +++++++++++ test/parallel/test-global-console-exists.js | 31 ++++++++------------- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 492b5acdea586c..71c850198e2c02 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -116,6 +116,16 @@ Checks whether `IPv6` is supported on this platform. Checks if there are multiple localhosts available. +### hijackStdout(listener) +* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; + +Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. + +### hijackStderr(listener) +* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; + +Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. + ### inFreeBSDJail * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) diff --git a/test/common/index.js b/test/common/index.js index d657e36f06c481..035ca45024adbe 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -759,3 +759,19 @@ exports.getTTYfd = function getTTYfd() { } return tty_fd; }; + +// Hijack stdout and stderr +function hijackStdWritable(name, listener) { + const stream = process[name]; + const _write = stream.write.bind(stream); + + stream.writeTimes = 0; + stream.write = function(data) { + listener(data); + _write(data); + stream.writeTimes++; + }; +} + +exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); +exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); diff --git a/test/parallel/test-global-console-exists.js b/test/parallel/test-global-console-exists.js index d18ecb77c36f5a..7cf58fdb0a8ea1 100644 --- a/test/parallel/test-global-console-exists.js +++ b/test/parallel/test-global-console-exists.js @@ -7,37 +7,30 @@ const common = require('../common'); const assert = require('assert'); const EventEmitter = require('events'); -const leak_warning = /EventEmitter memory leak detected\. 2 hello listeners/; +const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/; -let write_calls = 0; +common.hijackStderr(function(data) { + if (process.stderr.writeTimes === 0) { + assert.ok(data.match(leakWarning)); + } else { + assert.fail('stderr.write should be called only once'); + } +}); -process.on('warning', (warning) => { +process.on('warning', function(warning) { // This will be called after the default internal // process warning handler is called. The default // process warning writes to the console, which will // invoke the monkeypatched process.stderr.write // below. - assert.strictEqual(write_calls, 1); - EventEmitter.defaultMaxListeners = old_default; + assert.strictEqual(process.stderr.writeTimes, 1); + EventEmitter.defaultMaxListeners = oldDefault; // when we get here, we should be done }); -process.stderr.write = (data) => { - if (write_calls === 0) - assert.ok(data.match(leak_warning)); - else - assert.fail('stderr.write should be called only once'); - - write_calls++; -}; - -const old_default = EventEmitter.defaultMaxListeners; +const oldDefault = EventEmitter.defaultMaxListeners; EventEmitter.defaultMaxListeners = 1; const e = new EventEmitter(); e.on('hello', common.noop); e.on('hello', common.noop); - -// TODO: Figure out how to validate console. Currently, -// there is no obvious way of validating that console -// exists here exactly when it should. From d261893100acff1551ac4ccd8265051b8c2fd43b Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 4 Jun 2017 15:29:37 +0800 Subject: [PATCH 2/6] update after reviewing --- test/common/README.md | 16 +++++++++--- test/common/index.js | 14 ++++++++--- test/fixtures/echo-close-check.js | 8 +++++- test/fixtures/echo.js | 6 +++++ test/parallel/test-console.js | 33 +++++++++++++++---------- test/parallel/test-process-raw-debug.js | 7 ++---- test/parallel/test-util-log.js | 15 +++++------ 7 files changed, 66 insertions(+), 33 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 71c850198e2c02..0428f4fe27f05d 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -116,15 +116,15 @@ Checks whether `IPv6` is supported on this platform. Checks if there are multiple localhosts available. -### hijackStdout(listener) +### hijackStderr(listener) * `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; -Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. +Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. -### hijackStderr(listener) +### hijackStdout(listener) * `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; -Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. +Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. ### inFreeBSDJail * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) @@ -260,6 +260,14 @@ Port tests are running on. Deletes the 'tmp' dir and recreates it +### restoreStderr + +Restore the original `process.stderr.write`. + +### restoreStdout + +Restore the original `process.stdout.write`. + ### rootDir * return [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) diff --git a/test/common/index.js b/test/common/index.js index 035ca45024adbe..0c07c9f849fc04 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -761,17 +761,25 @@ exports.getTTYfd = function getTTYfd() { }; // Hijack stdout and stderr +const stdWrite = {}; function hijackStdWritable(name, listener) { const stream = process[name]; - const _write = stream.write.bind(stream); + const _write = stdWrite[name] = stream.write.bind(stream); stream.writeTimes = 0; - stream.write = function(data) { + stream.write = function(data, callback) { listener(data); - _write(data); + _write(data, callback); stream.writeTimes++; }; } +function restoreWritable(name) { + process[name].write = stdWrite[name]; + delete process[name].writeTimes; +} + exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); +exports.restoreStdout = restoreWritable.bind(null, 'stdout'); +exports.restoreStderr = restoreWritable.bind(null, 'stderr'); diff --git a/test/fixtures/echo-close-check.js b/test/fixtures/echo-close-check.js index f58515b6b77f1e..d428939766d6da 100644 --- a/test/fixtures/echo-close-check.js +++ b/test/fixtures/echo-close-check.js @@ -26,9 +26,15 @@ const fs = require('fs'); process.stdout.write('hello world\r\n'); -var stdin = process.openStdin(); +const stdin = process.openStdin(); + +let current; +common.hijackStdout(function(data) { + assert.equal(data, current); +}); stdin.on('data', function(data) { + current = data; process.stdout.write(data.toString()); }); diff --git a/test/fixtures/echo.js b/test/fixtures/echo.js index 8d3d9ecf835ab0..41e587efa96fe4 100644 --- a/test/fixtures/echo.js +++ b/test/fixtures/echo.js @@ -26,6 +26,12 @@ process.stdout.write('hello world\r\n'); var stdin = process.openStdin(); +let current; stdin.on('data', function(data) { + current = data; process.stdout.write(data.toString()); }); + +common.hijackStdout(function(data) { + assert.equal(data, current); +}); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 0c413410159199..ec4cee18744f46 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -45,16 +45,14 @@ assert.doesNotThrow(function() { // an Object with a custom .inspect() function const custom_inspect = { foo: 'bar', inspect: () => 'inspect' }; -const stdout_write = global.process.stdout.write; -const stderr_write = global.process.stderr.write; const strings = []; const errStrings = []; -global.process.stdout.write = function(string) { - strings.push(string); -}; -global.process.stderr.write = function(string) { - errStrings.push(string); -}; +common.hijackStdout(function(data) { + strings.push(data); +}); +common.hijackStderr(function(data) { + errStrings.push(data); +}); // test console.log() goes to stdout console.log('foo'); @@ -105,8 +103,10 @@ console.timeEnd('constructor'); console.time('hasOwnProperty'); console.timeEnd('hasOwnProperty'); -global.process.stdout.write = stdout_write; -global.process.stderr.write = stderr_write; +assert.strictEqual(strings.length, process.stdout.writeTimes); +assert.strictEqual(errStrings.length, process.stderr.writeTimes); +common.restoreStdout(); +common.restoreStderr(); // verify that console.timeEnd() doesn't leave dead links const timesMapSize = console._times.size; @@ -146,9 +146,6 @@ assert.ok(/^hasOwnProperty: \d+\.\d{3}ms$/.test(strings.shift().trim())); assert.strictEqual('Trace: This is a {"formatted":"trace"} 10 foo', errStrings.shift().split('\n').shift()); -assert.strictEqual(strings.length, 0); -assert.strictEqual(errStrings.length, 0); - assert.throws(() => { console.assert(false, 'should throw'); }, common.expectsError({ @@ -159,3 +156,13 @@ assert.throws(() => { assert.doesNotThrow(() => { console.assert(true, 'this should not throw'); }); + +// hijack stderr to catch `process.emitWarning` which using `process.nextTick` +common.hijackStderr(common.mustCall(function(data) { + common.restoreStderr(); + + // stderr.write will catch sync error, so use `process.nextTick` here + process.nextTick(function() { + assert(/no such label/.test(data)); + }); +})); diff --git a/test/parallel/test-process-raw-debug.js b/test/parallel/test-process-raw-debug.js index 04599014641456..4d320d54039000 100644 --- a/test/parallel/test-process-raw-debug.js +++ b/test/parallel/test-process-raw-debug.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const os = require('os'); @@ -63,10 +63,7 @@ function child() { throw new Error('No ticking!'); }; - const stderr = process.stderr; - stderr.write = function() { - throw new Error('No writing to stderr!'); - }; + common.hijackStderr(common.mustNotCall('stderr.write mustn\'t be called.')); process._rawDebug('I can still %s!', 'debug'); } diff --git a/test/parallel/test-util-log.js b/test/parallel/test-util-log.js index f32dcecd15a7bf..0b1ee2344a80dc 100644 --- a/test/parallel/test-util-log.js +++ b/test/parallel/test-util-log.js @@ -20,19 +20,18 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const util = require('util'); assert.ok(process.stdout.writable); assert.ok(process.stderr.writable); -const stdout_write = global.process.stdout.write; const strings = []; -global.process.stdout.write = function(string) { - strings.push(string); -}; -console._stderr = process.stdout; +common.hijackStdout(function(data) { + strings.push(data); +}); +common.hijackStderr(common.mustNotCall('stderr.write mustn\'t be called')); const tests = [ {input: 'foo', output: 'foo'}, @@ -56,4 +55,6 @@ tests.forEach(function(test) { assert.strictEqual(match[1], test.output); }); -global.process.stdout.write = stdout_write; +assert.strictEqual(process.stdout.writeTimes, tests.length); + +common.restoreStdout(); From bda1fd13f2f6d9f50f58535fd021e6d8b39d6da7 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 4 Jun 2017 19:04:04 +0800 Subject: [PATCH 3/6] expand contraction --- test/parallel/test-process-raw-debug.js | 2 +- test/parallel/test-util-log.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-process-raw-debug.js b/test/parallel/test-process-raw-debug.js index 4d320d54039000..7b89a8ad18eda7 100644 --- a/test/parallel/test-process-raw-debug.js +++ b/test/parallel/test-process-raw-debug.js @@ -63,7 +63,7 @@ function child() { throw new Error('No ticking!'); }; - common.hijackStderr(common.mustNotCall('stderr.write mustn\'t be called.')); + common.hijackStderr(common.mustNotCall('stderr.write must not be called.')); process._rawDebug('I can still %s!', 'debug'); } diff --git a/test/parallel/test-util-log.js b/test/parallel/test-util-log.js index 0b1ee2344a80dc..24e84e1be9247b 100644 --- a/test/parallel/test-util-log.js +++ b/test/parallel/test-util-log.js @@ -31,7 +31,7 @@ const strings = []; common.hijackStdout(function(data) { strings.push(data); }); -common.hijackStderr(common.mustNotCall('stderr.write mustn\'t be called')); +common.hijackStderr(common.mustNotCall('stderr.write must not be called')); const tests = [ {input: 'foo', output: 'foo'}, From 5a3a66619bc06ab574d38eb7702d0ca79ba82485 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 4 Jun 2017 19:28:28 +0800 Subject: [PATCH 4/6] update after @thefourtheye's reviewing --- test/common/README.md | 20 +++++++++++++++----- test/fixtures/echo-close-check.js | 8 ++++---- test/parallel/test-console.js | 3 ++- test/parallel/test-global-console-exists.js | 4 ++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 0428f4fe27f05d..ec9f2a1eabd56f 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -117,14 +117,24 @@ Checks whether `IPv6` is supported on this platform. Checks if there are multiple localhosts available. ### hijackStderr(listener) -* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; +* `listener` +[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): +a listener with a single parameter called `data`. -Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one then. +Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is +called, `listener` will also be called and the `data` of `write` function will +be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one +then. ### hijackStdout(listener) -* `listener` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): An listener with a single parameter called `data`; - -Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is called, `listener` will also be called and the `data` of `write` function will be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one then. +* `listener` +[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): +a listener with a single parameter called `data`. + +Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is +called, `listener` will also be called and the `data` of `write` function will +be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one +then. ### inFreeBSDJail * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) diff --git a/test/fixtures/echo-close-check.js b/test/fixtures/echo-close-check.js index d428939766d6da..66154fd55b1e77 100644 --- a/test/fixtures/echo-close-check.js +++ b/test/fixtures/echo-close-check.js @@ -29,12 +29,12 @@ process.stdout.write('hello world\r\n'); const stdin = process.openStdin(); let current; -common.hijackStdout(function(data) { - assert.equal(data, current); -}); +common.hijackStdout(common.mustCall(function(data) { + assert.strictEqual(data, current); +})); stdin.on('data', function(data) { - current = data; + current = data.toString(); process.stdout.write(data.toString()); }); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index ec4cee18744f46..8892dad9d19e78 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -157,7 +157,8 @@ assert.doesNotThrow(() => { console.assert(true, 'this should not throw'); }); -// hijack stderr to catch `process.emitWarning` which using `process.nextTick` +// hijack stderr to catch `process.emitWarning` which is using +// `process.nextTick` common.hijackStderr(common.mustCall(function(data) { common.restoreStderr(); diff --git a/test/parallel/test-global-console-exists.js b/test/parallel/test-global-console-exists.js index 7cf58fdb0a8ea1..936a8c547e1ff3 100644 --- a/test/parallel/test-global-console-exists.js +++ b/test/parallel/test-global-console-exists.js @@ -9,13 +9,13 @@ const assert = require('assert'); const EventEmitter = require('events'); const leakWarning = /EventEmitter memory leak detected\. 2 hello listeners/; -common.hijackStderr(function(data) { +common.hijackStderr(common.mustCall(function(data) { if (process.stderr.writeTimes === 0) { assert.ok(data.match(leakWarning)); } else { assert.fail('stderr.write should be called only once'); } -}); +})); process.on('warning', function(warning) { // This will be called after the default internal From ab93f52b467415f76173728b4c7787402bb82a72 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Mon, 5 Jun 2017 11:35:49 +0800 Subject: [PATCH 5/6] update after @refack's reviewing --- test/common/README.md | 28 +++++++++------------------- test/common/index.js | 4 ++-- test/fixtures/echo-close-check.js | 6 ------ test/fixtures/echo.js | 6 ------ test/parallel/test-common.js | 20 ++++++++++++++++++++ test/parallel/test-console.js | 2 +- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index ec9f2a1eabd56f..c6bcddc4bb4c09 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -116,25 +116,13 @@ Checks whether `IPv6` is supported on this platform. Checks if there are multiple localhosts available. -### hijackStderr(listener) -* `listener` -[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): -a listener with a single parameter called `data`. - -Hijack `process.stderr` to listen `write` action. Once `process.stderr.write` is -called, `listener` will also be called and the `data` of `write` function will -be passed to `listener`. What's more, `process.stderr.writeTimes` will plus one -then. - -### hijackStdout(listener) -* `listener` -[<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions): -a listener with a single parameter called `data`. - -Hijack `process.stdout` to listen `write` action. Once `process.stdout.write` is -called, `listener` will also be called and the `data` of `write` function will -be passed to `listener`. What's more, `process.stdout.writeTimes` will plus one -then. +### hijackStderr/Stdout(listener) +* `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`. + +Eavesdrop to `process.stderr.write` or `process.stdout.write` calls. Once +`process.std*.write` is called, `listener` will also be called and the `data` of +`write` function will be passed to `listener`. What's more, +`process.std*.writeTimes` is a count of the number of calls. ### inFreeBSDJail * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) @@ -318,3 +306,5 @@ Node.js [WHATWG URL API](https://nodejs.org/api/url.html#url_the_whatwg_url_api) implementation with tests from [W3C Web Platform Tests](https://github.com/w3c/web-platform-tests). + +[MDN-Function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Normal_objects_and_functions diff --git a/test/common/index.js b/test/common/index.js index 0c07c9f849fc04..75a1edd447a534 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -764,12 +764,12 @@ exports.getTTYfd = function getTTYfd() { const stdWrite = {}; function hijackStdWritable(name, listener) { const stream = process[name]; - const _write = stdWrite[name] = stream.write.bind(stream); + const _write = stdWrite[name] = stream.write; stream.writeTimes = 0; stream.write = function(data, callback) { listener(data); - _write(data, callback); + _write.call(stream, data, callback); stream.writeTimes++; }; } diff --git a/test/fixtures/echo-close-check.js b/test/fixtures/echo-close-check.js index 66154fd55b1e77..f03a35727c6eb3 100644 --- a/test/fixtures/echo-close-check.js +++ b/test/fixtures/echo-close-check.js @@ -28,13 +28,7 @@ process.stdout.write('hello world\r\n'); const stdin = process.openStdin(); -let current; -common.hijackStdout(common.mustCall(function(data) { - assert.strictEqual(data, current); -})); - stdin.on('data', function(data) { - current = data.toString(); process.stdout.write(data.toString()); }); diff --git a/test/fixtures/echo.js b/test/fixtures/echo.js index 41e587efa96fe4..8d3d9ecf835ab0 100644 --- a/test/fixtures/echo.js +++ b/test/fixtures/echo.js @@ -26,12 +26,6 @@ process.stdout.write('hello world\r\n'); var stdin = process.openStdin(); -let current; stdin.on('data', function(data) { - current = data; process.stdout.write(data.toString()); }); - -common.hijackStdout(function(data) { - assert.equal(data, current); -}); diff --git a/test/parallel/test-common.js b/test/parallel/test-common.js index 0a4e1d72f0f487..47ed7d9f3138da 100644 --- a/test/parallel/test-common.js +++ b/test/parallel/test-common.js @@ -88,3 +88,23 @@ for (const p of failFixtures) { assert.strictEqual(firstLine, expected); })); } + +// hijackStderr and hijackStdout +const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ]; +[ 'err', 'out' ].forEach((txt) => { + const stream = process[`std${txt}`]; + const originalWrite = stream.write; + + common[`hijackStd${txt}`](common.mustCall(function(data) { + assert.strictEqual(data, HIJACK_TEST_ARRAY[stream.writeTimes]); + }, HIJACK_TEST_ARRAY.length)); + assert.notStrictEqual(originalWrite, stream.write); + + HIJACK_TEST_ARRAY.forEach((val) => { + stream.write(val, common.mustCall(common.noop)); + }); + + assert.strictEqual(HIJACK_TEST_ARRAY.length, stream.writeTimes); + common[`restoreStd${txt}`](); + assert.strictEqual(originalWrite, stream.write); +}); diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index 8892dad9d19e78..eecfd8334e3ff2 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -164,6 +164,6 @@ common.hijackStderr(common.mustCall(function(data) { // stderr.write will catch sync error, so use `process.nextTick` here process.nextTick(function() { - assert(/no such label/.test(data)); + assert.strictEqual(data.includes('no such label'), true); }); })); From 2a42456773eccf376f0f932ebf14bab6711facb0 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Tue, 6 Jun 2017 20:56:04 +0800 Subject: [PATCH 6/6] update afeter @jasnell's reviewing --- test/common/README.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index c6bcddc4bb4c09..a9f89bec735d24 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -116,13 +116,21 @@ Checks whether `IPv6` is supported on this platform. Checks if there are multiple localhosts available. -### hijackStderr/Stdout(listener) +### hijackStderr(listener) * `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`. -Eavesdrop to `process.stderr.write` or `process.stdout.write` calls. Once -`process.std*.write` is called, `listener` will also be called and the `data` of -`write` function will be passed to `listener`. What's more, -`process.std*.writeTimes` is a count of the number of calls. +Eavesdrop to `process.stderr.write` calls. Once `process.stderr.write` is +called, `listener` will also be called and the `data` of `write` function will +be passed to `listener`. What's more, `process.stderr.writeTimes` is a count of +the number of calls. + +### hijackStdout(listener) +* `listener` [<Function>][MDN-Function]: a listener with a single parameter called `data`. + +Eavesdrop to `process.stdout.write` calls. Once `process.stdout.write` is +called, `listener` will also be called and the `data` of `write` function will +be passed to `listener`. What's more, `process.stdout.writeTimes` is a count of +the number of calls. ### inFreeBSDJail * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) @@ -258,11 +266,11 @@ Port tests are running on. Deletes the 'tmp' dir and recreates it -### restoreStderr +### restoreStderr() Restore the original `process.stderr.write`. -### restoreStdout +### restoreStdout() Restore the original `process.stdout.write`.