Skip to content

Commit 5f1a01d

Browse files
Trotttargos
authored andcommitted
test: fix buggy getTTYfd() implementation
PR-URL: #17781 Ref: #17781 (comment) Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent c6b993b commit 5f1a01d

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

test/sequential/test-async-wrap-getasyncid.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -258,28 +258,25 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check
258258
// Do our best to grab a tty fd.
259259
function getTTYfd() {
260260
const tty = require('tty');
261-
let tty_fd = 0;
262-
if (!tty.isatty(tty_fd)) tty_fd++;
263-
else if (!tty.isatty(tty_fd)) tty_fd++;
264-
else if (!tty.isatty(tty_fd)) tty_fd++;
265-
else {
261+
let ttyFd = [0, 1, 2].find(tty.isatty);
262+
if (ttyFd === undefined) {
266263
try {
267-
tty_fd = fs.openSync('/dev/tty');
264+
ttyFd = fs.openSync('/dev/tty');
268265
} catch (e) {
269266
// There aren't any tty fd's available to use.
270267
return -1;
271268
}
272269
}
273-
return tty_fd;
270+
return ttyFd;
274271
}
275272

276-
const tty_fd = getTTYfd();
277-
if (tty_fd >= 0) {
273+
const ttyFd = getTTYfd();
274+
if (ttyFd >= 0) {
278275
const tty_wrap = process.binding('tty_wrap');
279276
// fd may still be invalid, so guard against it.
280277
const handle = (() => {
281278
try {
282-
return new tty_wrap.TTY(tty_fd, false);
279+
return new tty_wrap.TTY(ttyFd, false);
283280
} catch (e) {
284281
return null;
285282
}

0 commit comments

Comments
 (0)