-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lib: respect terminal capabilities on styleText #54389
Changes from 6 commits
9e3ada1
66cd3f3
cc08b60
0d3a65d
dd807c0
de39601
317c284
5efe9b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this test is being significantly updated, it would be nice if it could be structured more using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it blocking? Otherwise, I prefer to do it in a follow-up PR. |
||
const common = require('../common'); | ||
const assert = require('node:assert'); | ||
const util = require('node:util'); | ||
const { WriteStream } = require('node:tty'); | ||
|
||
const styled = '\u001b[31mtest\u001b[39m'; | ||
const noChange = 'test'; | ||
|
||
[ | ||
undefined, | ||
|
@@ -31,13 +36,69 @@ assert.throws(() => { | |
code: 'ERR_INVALID_ARG_VALUE', | ||
}); | ||
|
||
assert.strictEqual(util.styleText('red', 'test'), '\u001b[31mtest\u001b[39m'); | ||
assert.strictEqual( | ||
util.styleText('red', 'test', { validateStream: false }), | ||
'\u001b[31mtest\u001b[39m', | ||
); | ||
|
||
assert.strictEqual( | ||
util.styleText(['bold', 'red'], 'test', { validateStream: false }), | ||
'\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m', | ||
); | ||
|
||
assert.strictEqual(util.styleText(['bold', 'red'], 'test'), '\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m'); | ||
assert.strictEqual(util.styleText(['bold', 'red'], 'test'), util.styleText('bold', util.styleText('red', 'test'))); | ||
assert.strictEqual( | ||
util.styleText(['bold', 'red'], 'test', { validateStream: false }), | ||
util.styleText( | ||
'bold', | ||
util.styleText('red', 'test', { validateStream: false }), | ||
{ validateStream: false }, | ||
), | ||
); | ||
|
||
assert.throws(() => { | ||
util.styleText(['invalid'], 'text'); | ||
}, { | ||
code: 'ERR_INVALID_ARG_VALUE', | ||
}); | ||
|
||
assert.throws(() => { | ||
util.styleText('red', 'text', { stream: {} }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
|
||
// does not throw | ||
RafaelGSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
util.styleText('red', 'text', { stream: {}, validateStream: false }); | ||
|
||
assert.strictEqual( | ||
util.styleText('red', 'test', { validateStream: false }), | ||
styled, | ||
); | ||
|
||
const fd = common.getTTYfd(); | ||
if (fd !== -1) { | ||
const writeStream = new WriteStream(fd); | ||
|
||
const originalEnv = process.env; | ||
[ | ||
{ isTTY: true, env: {}, expected: styled }, | ||
{ isTTY: false, env: {}, expected: noChange }, | ||
{ isTTY: true, env: { NODE_DISABLE_COLORS: '1' }, expected: noChange }, | ||
{ isTTY: true, env: { NO_COLOR: '1' }, expected: noChange }, | ||
{ isTTY: true, env: { FORCE_COLOR: '1' }, expected: styled }, | ||
{ isTTY: true, env: { FORCE_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
{ isTTY: false, env: { FORCE_COLOR: '1', NO_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
{ isTTY: true, env: { FORCE_COLOR: '1', NO_COLOR: '1', NODE_DISABLE_COLORS: '1' }, expected: styled }, | ||
].forEach((testCase) => { | ||
writeStream.isTTY = testCase.isTTY; | ||
process.env = { | ||
...process.env, | ||
...testCase.env | ||
}; | ||
const output = util.styleText('red', 'test', { stream: writeStream }); | ||
assert.strictEqual(output, testCase.expected); | ||
process.env = originalEnv; | ||
}); | ||
} else { | ||
common.skip('Could not create TTY fd'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add this to the function's signature above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in cc08b60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not what I meant. You have to update
node/doc/api/util.md
Line 1805 in 22daeba
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RafaelGSS I've updated the doc for ya :)