summaryrefslogtreecommitdiff
path: root/test/common
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2018-08-22 09:17:19 -0700
committerJames M Snell <jasnell@gmail.com>2018-08-24 09:53:58 -0700
commit5da834f685f85af477afcb193d283f14a5244025 (patch)
tree1d86045f11e1cad9622be34a8a907b4972493624 /test/common
parentea8b932f30665436796c7e5abd485f048a0f41c8 (diff)
downloadandroid-node-v8-5da834f685f85af477afcb193d283f14a5244025.tar.gz
android-node-v8-5da834f685f85af477afcb193d283f14a5244025.tar.bz2
android-node-v8-5da834f685f85af477afcb193d283f14a5244025.zip
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 <ruben@bridgewater.de> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'test/common')
-rw-r--r--test/common/README.md78
-rw-r--r--test/common/hijackstdio.js33
-rw-r--r--test/common/index.js28
-rw-r--r--test/common/index.mjs8
4 files changed, 81 insertions, 66 deletions
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` [&lt;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` [&lt;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
* [&lt;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
* [&lt;string>]
@@ -596,6 +568,52 @@ validateSnapshotNodes('TLSWRAP', [
]);
```
+## hijackstdio Module
+
+The `hijackstdio` module provides utility functions for temporarily redirecting
+`stdout` and `stderr` output.
+
+<!-- eslint-disable no-undef, node-core/required-modules -->
+```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` [&lt;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` [&lt;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
[&lt;boolean>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type
[&lt;number>]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
[&lt;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
};