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: improve tty.getColorDepth coverage #19446

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
67 changes: 50 additions & 17 deletions test/pseudo-tty/test-tty-get-color-depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,65 @@ const assert = require('assert').strict;
/* eslint-disable no-restricted-properties */
const { WriteStream } = require('tty');

const NODE_DISABLE_COLORS = process.env.NODE_DISABLE_COLORS;
delete process.env.NODE_DISABLE_COLORS;

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();
// Check different environment variables.
[
[{ COLORTERM: '1' }, 4],
[{ TMUX: '1' }, 8],
[{ CI: '1' }, 1],
[{ CI: '1', TRAVIS: '1' }, 8],
[{ CI: '1', CIRCLECI: '1' }, 8],
[{ CI: '1', APPVEYOR: '1' }, 8],
[{ CI: '1', GITLAB_CI: '1' }, 8],
[{ CI: '1', CI_NAME: 'codeship' }, 8],
[{ TEAMCITY_VERSION: '1.0.0' }, 1],
[{ TEAMCITY_VERSION: '9.11.0' }, 4],
[{ TERM_PROGRAM: 'iTerm.app' }, 8],
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
[{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
[{ TERM_PROGRAM: 'HyperTerm' }, 24],
[{ TERM_PROGRAM: 'Hyper' }, 24],
[{ TERM_PROGRAM: 'MacTerm' }, 24],
[{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
[{ TERM: 'xterm-256' }, 8],
[{ TERM: 'ansi' }, 4],
[{ TERM: 'ANSI' }, 4],
[{ TERM: 'color' }, 4],
[{ TERM: 'linux' }, 4],
[{ TERM: 'fail' }, 1],
[{ NODE_DISABLE_COLORS: '1' }, 1],
[{ TERM: 'dumb' }, 1],
[{ TERM: 'dumb', COLORTERM: '1' }, 4],
].forEach(([env, depth], i) => {
const actual = writeStream.getColorDepth(env);
assert.equal(
actual,
depth,
`i: ${i}, expected: ${depth}, actual: ${actual}, env: ${env}`
);
});

assert.equal(depth, 1);
// OS settings
{
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
const [ value, depth1, depth2 ] = process.platform !== 'win32' ?
['win32', 1, 4] : ['linux', 4, 1];

process.env.NODE_DISABLE_COLORS = tmp;
assert.equal(writeStream.getColorDepth({}), depth1);
Object.defineProperty(process, 'platform', { value });
assert.equal(writeStream.getColorDepth({}), depth2);
Object.defineProperty(process, 'platform', platform);
}

process.env.NODE_DISABLE_COLORS = NODE_DISABLE_COLORS;
Copy link
Member

Choose a reason for hiding this comment

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

This test fails when being run with NODE_PENDING_DEPRECATION=1, because NODE_DISABLE_COLORS can be undefined at this point.

I guess you could just drop this line? Setting an environment variable at the end of the script shouldn’t make a difference anyway, I think…