From 5f1a01d816d0284fb4b0bc58192c7c1319eabf4b Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 20 Dec 2017 06:19:10 -0800 Subject: [PATCH] test: fix buggy getTTYfd() implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/17781 Ref: https://github.com/nodejs/node/pull/17781#discussion_r158030728 Reviewed-By: Michaƫl Zasso Reviewed-By: Gireesh Punathil Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Timothy Gu Reviewed-By: Jon Moss Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/sequential/test-async-wrap-getasyncid.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/sequential/test-async-wrap-getasyncid.js b/test/sequential/test-async-wrap-getasyncid.js index e1cc38ab7bbef7..45f53ee9d7a82d 100644 --- a/test/sequential/test-async-wrap-getasyncid.js +++ b/test/sequential/test-async-wrap-getasyncid.js @@ -258,28 +258,25 @@ if (common.hasCrypto) { // eslint-disable-line crypto-check // Do our best to grab a tty fd. function getTTYfd() { const tty = require('tty'); - let tty_fd = 0; - if (!tty.isatty(tty_fd)) tty_fd++; - else if (!tty.isatty(tty_fd)) tty_fd++; - else if (!tty.isatty(tty_fd)) tty_fd++; - else { + let ttyFd = [0, 1, 2].find(tty.isatty); + if (ttyFd === undefined) { try { - tty_fd = fs.openSync('/dev/tty'); + ttyFd = fs.openSync('/dev/tty'); } catch (e) { // There aren't any tty fd's available to use. return -1; } } - return tty_fd; + return ttyFd; } - const tty_fd = getTTYfd(); - if (tty_fd >= 0) { + const ttyFd = getTTYfd(); + if (ttyFd >= 0) { const tty_wrap = process.binding('tty_wrap'); // fd may still be invalid, so guard against it. const handle = (() => { try { - return new tty_wrap.TTY(tty_fd, false); + return new tty_wrap.TTY(ttyFd, false); } catch (e) { return null; }