Skip to content

Commit

Permalink
test: move getTTYfd() to common helpers
Browse files Browse the repository at this point in the history
This utility is fairly generic and likely useful for more than one test.

PR-URL: #18800
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
Fishrock123 committed Feb 20, 2018
1 parent 0089860 commit 92bf249
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 6 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 1 addition & 18 deletions test/parallel/test-tty-get-color-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,9 @@
const common = require('../common');
const assert = require('assert').strict;
/* eslint-disable no-restricted-properties */
const { openSync } = require('fs');
const tty = require('tty');

const { WriteStream } = require('tty');

// Do our best to grab a tty fd.
function getTTYfd() {
const ttyFd = [1, 2, 4, 5].find(tty.isatty);
if (ttyFd === undefined) {
try {
return openSync('/dev/tty');
} catch (e) {
// There aren't any tty fd's available to use.
return -1;
}
}
return ttyFd;
}

const fd = getTTYfd();
const fd = common.getTTYfd();

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

0 comments on commit 92bf249

Please sign in to comment.