forked from fisker/node-style-text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
106 lines (96 loc) · 3.24 KB
/
index.test.js
File metadata and controls
106 lines (96 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import assert from 'node:assert/strict'
import process from 'node:process'
import test from 'node:test'
import {styleText as nodeUtilStyleText} from 'node:util'
import styleText, {stderr, stdout} from './index.js'
const supportsNone = (() => {
try {
return nodeUtilStyleText('none', 'foo') === 'foo'
} catch {
return false
}
})()
test('Main [Node.js]', () => {
process.env.FORCE_COLOR = '1'
assert.equal(styleText.bold('foo'), '\u001B[1mfoo\u001B[22m')
assert.equal(styleText.underline('foo'), '\u001B[4mfoo\u001B[24m')
assert.equal(styleText.red('foo'), '\u001B[31mfoo\u001B[39m')
assert.equal(styleText.bgRed('foo'), '\u001B[41mfoo\u001B[49m')
assert.equal(
styleText.red.bgGreen.underline('foo'),
'\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m',
)
assert.equal(
styleText.underline.red.bgGreen('foo'),
'\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m',
)
assert.equal(
styleText.cyan.underline`hello ${'world'}`,
'\u001B[36m\u001B[4mhello world\u001B[24m\u001B[39m',
)
assert.equal(
styleText.cyan.underline`hello\u0020${'world'}`,
'\u001B[36m\u001B[4mhello world\u001B[24m\u001B[39m',
)
// Support alias https://nodejs.org/api/util.html#customizing-utilinspect-colors
const aliases = [
['strikethrough', ['strikeThrough', 'crossedout', 'crossedOut']],
['hidden', ['conceal']],
['dim', ['faint']],
['inverse', ['swapcolors', 'swapColors']],
['doubleunderline', ['doubleUnderline']],
['gray', ['grey', 'blackBright']],
['bgGray', ['bgGrey', 'bgBlackBright']],
].flatMap(([style, aliases]) => aliases.map((alias) => ({style, alias})))
for (const {style, alias} of aliases) {
assert.equal(
typeof styleText[alias],
'function',
`Should support alias: '${styleText.blue(alias)}'.`,
)
assert.equal(
styleText[style]('foo'),
styleText[alias]('foo'),
`Alias '${styleText.blue(alias)}' should output the same as '${styleText.blue(style)}'.`,
)
}
assert.throws(() => styleText.nonExists('foo'), TypeError)
if (supportsNone) {
assert.equal(styleText.none('foo'), 'foo')
}
})
// This test is not really working
// Figure out how to test
test('stream', () => {
const notColored = 'foo'
const redColored = '\u001B[31mfoo\u001B[39m'
assert.notDeepEqual(overrideColorDepth({stream: process.stdout, depth: 0}), {
stdout: notColored,
stderr: redColored,
})
assert.notDeepEqual(overrideColorDepth({stream: process.stderr, depth: 0}), {
stdout: redColored,
stderr: notColored,
})
})
// https://github.com/nodejs/node/blob/a822a1cbe73789a41ba30ab61deda88f2982ba3d/lib/internal/util/colors.js#L23C21-L23C34
function overrideColorDepth({stream, depth}) {
const originalIsTty = stream.isTTY
const originalGetColorDepth = stream.getColorDepth
const originalForceColor = process.env.FORCE_COLOR
try {
delete process.env.FORCE_COLOR
stream.isTTY = true
stream.getColorDepth = () => depth
return {
stdout: stdout.red('foo'),
stderr: stderr.red('foo'),
}
} finally {
if (originalForceColor !== undefined) {
process.env.FORCE_COLOR = originalForceColor
}
stream.isTTY = originalIsTty
stream.getColorDepth = originalGetColorDepth
}
}