diff --git a/test/common/README.md b/test/common/README.md index e0a66e9da0c2c9..7fd36f59852547 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 b24d2158e7d089..1fc029292abb10 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) { diff --git a/test/parallel/test-tty-get-color-depth.js b/test/parallel/test-tty-get-color-depth.js deleted file mode 100644 index a4c7ffacd17f82..00000000000000 --- a/test/parallel/test-tty-get-color-depth.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict'; - -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(); - -// Give up if we did not find a tty -if (fd === -1) - common.skip(); - -const writeStream = new WriteStream(fd); - -let depth = writeStream.getColorDepth(); - -assert.equal(typeof depth, 'number'); -assert(depth >= 1 && depth <= 24); - -// If the terminal does not support colors, skip the rest -if (depth === 1) - common.skip(); - -assert.notEqual(writeStream.getColorDepth({ TERM: 'dumb' }), depth); - -// Deactivate colors -const tmp = process.env.NODE_DISABLE_COLORS; -process.env.NODE_DISABLE_COLORS = 1; - -depth = writeStream.getColorDepth(); - -assert.equal(depth, 1); - -process.env.NODE_DISABLE_COLORS = tmp; diff --git a/test/pseudo-tty/test-tty-get-color-depth.js b/test/pseudo-tty/test-tty-get-color-depth.js new file mode 100644 index 00000000000000..7641290c057d2e --- /dev/null +++ b/test/pseudo-tty/test-tty-get-color-depth.js @@ -0,0 +1,36 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert').strict; +/* eslint-disable no-restricted-properties */ +const { WriteStream } = require('tty'); + +const fd = common.getTTYfd(); +const writeStream = new WriteStream(fd); + +{ + const depth = writeStream.getColorDepth(); + + assert.equal(typeof depth, 'number'); + assert(depth >= 1 && depth <= 24); + + if (depth === 1) { + // Terminal does not support colors, compare to a value that would. + assert.notEqual(writeStream.getColorDepth({ COLORTERM: '1' }), depth); + } else { + // Terminal supports colors, compare to a value that would not. + assert.notEqual(writeStream.getColorDepth({ TERM: 'dumb' }), depth); + } +} + +// Deactivate colors +{ + const tmp = process.env.NODE_DISABLE_COLORS; + process.env.NODE_DISABLE_COLORS = 1; + + const depth = writeStream.getColorDepth(); + + assert.equal(depth, 1); + + process.env.NODE_DISABLE_COLORS = tmp; +} diff --git a/test/pseudo-tty/test-tty-get-color-depth.out b/test/pseudo-tty/test-tty-get-color-depth.out new file mode 100644 index 00000000000000..e69de29bb2d1d6