Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: actually test tty getColorDepth() #18800

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something similar wa sin here at some point and is used in another test file. It would be good to consolidate in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is, I'm not sure where it is at. If there are tests somewhere that I missed that use something like this, they probably don't actually work...? I couldn't find any in a quick look.

There are some tests that don't even bother to go for something like this and just go for fd 0 instead. (Which could probably be added to the lookup list here? I'll do that.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one in sequential/test-async-wrap-getasyncid.js. And zero was removed because it can cause issues on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part of that test definitely never ever runs. 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move that out next and base it off of this then, I suppose.

};

// Hijack stdout and stderr
const stdWrite = {};
function hijackStdWritable(name, listener) {
Expand Down
52 changes: 0 additions & 52 deletions test/parallel/test-tty-get-color-depth.js

This file was deleted.

36 changes: 36 additions & 0 deletions test/pseudo-tty/test-tty-get-color-depth.js
Original file line number Diff line number Diff line change
@@ -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;
}
Empty file.