From 5da834f685f85af477afcb193d283f14a5244025 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 22 Aug 2018 09:17:19 -0700 Subject: test: move hijackstdio out of require('common') Move the hijackstdio functions out of common so that they are imported only into the tests that actually need them PR-URL: https://github.com/nodejs/node/pull/22462 Reviewed-By: Ruben Bridgewater Reviewed-By: Refael Ackermann Reviewed-By: Trivikram Kamat Reviewed-By: Luigi Pinca --- test/common/README.md | 78 ++++++++++++++++++++++++++++------------------ test/common/hijackstdio.js | 33 ++++++++++++++++++++ test/common/index.js | 28 ----------------- test/common/index.mjs | 8 ----- 4 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 test/common/hijackstdio.js (limited to 'test/common') diff --git a/test/common/README.md b/test/common/README.md index e44dcd299c..487c252af3 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -173,24 +173,6 @@ Indicates whether `IPv6` is supported on this platform. Indicates if there are multiple localhosts available. -### hijackStderr(listener) -* `listener` [<Function>]: a listener with a single parameter - called `data`. - -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>]: 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 * [<boolean>] @@ -355,16 +337,6 @@ A port number for tests to use if one is needed. Logs '1..0 # Skipped: ' + `msg` -### restoreStderr() - -Restore the original `process.stderr.write`. Used to restore `stderr` to its -original state after calling [`common.hijackStdErr()`][]. - -### restoreStdout() - -Restore the original `process.stdout.write`. Used to restore `stdout` to its -original state after calling [`common.hijackStdOut()`][]. - ### rootDir * [<string>] @@ -596,6 +568,52 @@ validateSnapshotNodes('TLSWRAP', [ ]); ``` +## hijackstdio Module + +The `hijackstdio` module provides utility functions for temporarily redirecting +`stdout` and `stderr` output. + + +```js +const { hijackStdout, restoreStdout } = require('../common/hijackstdio'); + +hijackStdout((data) => { + /* Do something with data */ + restoreStdout(); +}); + +console.log('this is sent to the hijacked listener'); +``` + +### hijackStderr(listener) +* `listener` [<Function>]: a listener with a single parameter + called `data`. + +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>]: 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. + +### restoreStderr() + +Restore the original `process.stderr.write()`. Used to restore `stderr` to its +original state after calling [`hijackstdio.hijackStdErr()`][]. + +### restoreStdout() + +Restore the original `process.stdout.write()`. Used to restore `stdout` to its +original state after calling [`hijackstdio.hijackStdOut()`][]. + + ## HTTP/2 Module The http2.js module provides a handful of utilities for creating mock HTTP/2 @@ -773,6 +791,6 @@ implementation with tests from [<boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type [<number>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type [<string>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type -[`common.hijackStdErr()`]: #hijackstderrlistener -[`common.hijackStdOut()`]: #hijackstdoutlistener +[`hijackstdio.hijackStdErr()`]: #hijackstderrlistener +[`hijackstdio.hijackStdOut()`]: #hijackstdoutlistener [internationalization]: https://github.com/nodejs/node/wiki/Intl diff --git a/test/common/hijackstdio.js b/test/common/hijackstdio.js new file mode 100644 index 0000000000..fcc98208f0 --- /dev/null +++ b/test/common/hijackstdio.js @@ -0,0 +1,33 @@ +/* eslint-disable node-core/required-modules */ +'use strict'; + +// Hijack stdout and stderr +const stdWrite = {}; +function hijackStdWritable(name, listener) { + const stream = process[name]; + const _write = stdWrite[name] = stream.write; + + stream.writeTimes = 0; + stream.write = function(data, callback) { + try { + listener(data); + } catch (e) { + process.nextTick(() => { throw e; }); + } + + _write.call(stream, data, callback); + stream.writeTimes++; + }; +} + +function restoreWritable(name) { + process[name].write = stdWrite[name]; + delete process[name].writeTimes; +} + +module.exports = { + hijackStdout: hijackStdWritable.bind(null, 'stdout'), + hijackStderr: hijackStdWritable.bind(null, 'stderr'), + restoreStdout: restoreWritable.bind(null, 'stdout'), + restoreStderr: restoreWritable.bind(null, 'stderr') +}; diff --git a/test/common/index.js b/test/common/index.js index 0f453d3af5..f1b8964815 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -787,30 +787,6 @@ exports.getTTYfd = function getTTYfd() { return ttyFd; }; -// Hijack stdout and stderr -const stdWrite = {}; -function hijackStdWritable(name, listener) { - const stream = process[name]; - const _write = stdWrite[name] = stream.write; - - stream.writeTimes = 0; - stream.write = function(data, callback) { - try { - listener(data); - } catch (e) { - process.nextTick(() => { throw e; }); - } - - _write.call(stream, data, callback); - stream.writeTimes++; - }; -} - -function restoreWritable(name) { - process[name].write = stdWrite[name]; - delete process[name].writeTimes; -} - exports.runWithInvalidFD = function(func) { let fd = 1 << 30; // Get first known bad file descriptor. 1 << 30 is usually unlikely to @@ -824,10 +800,6 @@ exports.runWithInvalidFD = function(func) { exports.printSkipMessage('Could not generate an invalid fd'); }; -exports.hijackStdout = hijackStdWritable.bind(null, 'stdout'); -exports.hijackStderr = hijackStdWritable.bind(null, 'stderr'); -exports.restoreStdout = restoreWritable.bind(null, 'stdout'); -exports.restoreStderr = restoreWritable.bind(null, 'stderr'); exports.isCPPSymbolsNotMapped = exports.isWindows || exports.isSunOS || exports.isAIX || diff --git a/test/common/index.mjs b/test/common/index.mjs index 3ad51d4cae..5a0d547d59 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -52,10 +52,6 @@ const { disableCrashOnUnhandledRejection, getTTYfd, runWithInvalidFD, - hijackStdout, - hijackStderr, - restoreStdout, - restoreStderr, isCPPSymbolsNotMapped } = common; @@ -109,9 +105,5 @@ export { disableCrashOnUnhandledRejection, getTTYfd, runWithInvalidFD, - hijackStdout, - hijackStderr, - restoreStdout, - restoreStderr, isCPPSymbolsNotMapped }; -- cgit v1.2.3