Skip to content

Commit b7da409

Browse files
Fishrock123BridgeAR
authored andcommitted
test: move getTTYfd() to common helpers
This utility is fairly generic and likely useful for more than one test. PR-URL: nodejs#18800 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent d37952e commit b7da409

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

test/common/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,12 @@ The realpath of the testing temporary directory.
679679

680680
Deletes and recreates the testing temporary directory.
681681

682+
### getTTYfd()
683+
684+
Attempts to get a valid TTY file descriptor. Returns `-1` if it fails.
685+
686+
The TTY file descriptor is assumed to be capable of being writable.
687+
682688
## WPT Module
683689

684690
The wpt.js module is a port of parts of

test/common/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,23 @@ exports.crashOnUnhandledRejection = function() {
775775
(err) => process.nextTick(() => { throw err; }));
776776
};
777777

778+
exports.getTTYfd = function getTTYfd() {
779+
// Do our best to grab a tty fd.
780+
const tty = require('tty');
781+
// Don't attempt fd 0 as it is not writable on Windows.
782+
// Ref: ef2861961c3d9e9ed6972e1e84d969683b25cf95
783+
const ttyFd = [1, 2, 4, 5].find(tty.isatty);
784+
if (ttyFd === undefined) {
785+
try {
786+
return fs.openSync('/dev/tty');
787+
} catch (e) {
788+
// There aren't any tty fd's available to use.
789+
return -1;
790+
}
791+
}
792+
return ttyFd;
793+
};
794+
778795
// Hijack stdout and stderr
779796
const stdWrite = {};
780797
function hijackStdWritable(name, listener) {

test/parallel/test-tty-get-color-depth.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,9 @@
33
const common = require('../common');
44
const assert = require('assert').strict;
55
/* eslint-disable no-restricted-properties */
6-
const { openSync } = require('fs');
7-
const tty = require('tty');
8-
96
const { WriteStream } = require('tty');
107

11-
// Do our best to grab a tty fd.
12-
function getTTYfd() {
13-
const ttyFd = [1, 2, 4, 5].find(tty.isatty);
14-
if (ttyFd === undefined) {
15-
try {
16-
return openSync('/dev/tty');
17-
} catch (e) {
18-
// There aren't any tty fd's available to use.
19-
return -1;
20-
}
21-
}
22-
return ttyFd;
23-
}
24-
25-
const fd = getTTYfd();
8+
const fd = common.getTTYfd();
269

2710
// Give up if we did not find a tty
2811
if (fd === -1)

0 commit comments

Comments
 (0)