diff options
author | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2018-02-16 12:09:54 -0500 |
---|---|---|
committer | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2018-02-20 12:31:16 -0500 |
commit | 92bf2492cd6b78b1ab26765660d912ee34bb90c6 (patch) | |
tree | 154aa20b09c922abbe505ab1ecc68c96d1fceb87 /test/common | |
parent | 0089860757753f305a45a4c6d19db54e162c36c2 (diff) | |
download | android-node-v8-92bf2492cd6b78b1ab26765660d912ee34bb90c6.tar.gz android-node-v8-92bf2492cd6b78b1ab26765660d912ee34bb90c6.tar.bz2 android-node-v8-92bf2492cd6b78b1ab26765660d912ee34bb90c6.zip |
test: move getTTYfd() to common helpers
This utility is fairly generic and likely useful for more than one test.
PR-URL: https://github.com/nodejs/node/pull/18800
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/common')
-rw-r--r-- | test/common/README.md | 6 | ||||
-rw-r--r-- | test/common/index.js | 17 |
2 files changed, 23 insertions, 0 deletions
diff --git a/test/common/README.md b/test/common/README.md index e0a66e9da0..7fd36f5985 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -679,6 +679,12 @@ The realpath of the testing temporary directory. Deletes and recreates the testing temporary directory. +### getTTYfd() + +Attempts to get a valid TTY file descriptor. Returns `-1` if it fails. + +The TTY file descriptor is assumed to be capable of being writable. + ## WPT Module The wpt.js module is a port of parts of diff --git a/test/common/index.js b/test/common/index.js index b24d2158e7..1fc029292a 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -775,6 +775,23 @@ exports.crashOnUnhandledRejection = function() { (err) => process.nextTick(() => { throw err; })); }; +exports.getTTYfd = function getTTYfd() { + // Do our best to grab a tty fd. + const tty = require('tty'); + // Don't attempt fd 0 as it is not writable on Windows. + // Ref: ef2861961c3d9e9ed6972e1e84d969683b25cf95 + const ttyFd = [1, 2, 4, 5].find(tty.isatty); + if (ttyFd === undefined) { + try { + return fs.openSync('/dev/tty'); + } catch (e) { + // There aren't any tty fd's available to use. + return -1; + } + } + return ttyFd; +}; + // Hijack stdout and stderr const stdWrite = {}; function hijackStdWritable(name, listener) { |