@@ -8,104 +8,102 @@ const { WriteStream } = require('node:tty');
88const styled = '\u001b[31mtest\u001b[39m' ;
99const noChange = 'test' ;
1010
11- [
12- undefined ,
13- null ,
14- false ,
15- 5n ,
16- 5 ,
17- Symbol ( ) ,
18- ( ) => { } ,
19- { } ,
20- ] . forEach ( ( invalidOption ) => {
21- assert . throws ( ( ) => {
22- util . styleText ( invalidOption , 'test' ) ;
23- } , {
24- code : 'ERR_INVALID_ARG_VALUE' ,
25- } ) ;
26- assert . throws ( ( ) => {
27- util . styleText ( 'red' , invalidOption ) ;
28- } , {
29- code : 'ERR_INVALID_ARG_TYPE'
30- } ) ;
31- } ) ;
11+ [
12+ undefined ,
13+ null ,
14+ false ,
15+ 5n ,
16+ 5 ,
17+ Symbol ( ) ,
18+ ( ) => { } ,
19+ { } ,
20+ ] . forEach ( ( invalidOption ) => {
21+ assert . throws ( ( ) => {
22+ util . styleText ( invalidOption , 'test' ) ;
23+ } , {
24+ code : 'ERR_INVALID_ARG_VALUE' ,
25+ } ) ;
26+ assert . throws ( ( ) => {
27+ util . styleText ( 'red' , invalidOption ) ;
28+ } , {
29+ code : 'ERR_INVALID_ARG_TYPE'
30+ } ) ;
31+ } ) ;
3232
33- assert . throws ( ( ) => {
34- util . styleText ( 'invalid' , 'text' ) ;
35- } , {
36- code : 'ERR_INVALID_ARG_VALUE' ,
37- } ) ;
33+ assert . throws ( ( ) => {
34+ util . styleText ( 'invalid' , 'text' ) ;
35+ } , {
36+ code : 'ERR_INVALID_ARG_VALUE' ,
37+ } ) ;
38+ assert . throws ( ( ) => {
39+ util . styleText ( [ 'invalid' ] , 'text' ) ;
40+ } , {
41+ code : 'ERR_INVALID_ARG_VALUE' ,
42+ } ) ;
3843
39- assert . strictEqual (
40- util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
41- '\u001b[31mtest\u001b[39m' ,
42- ) ;
44+ assert . strictEqual (
45+ util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
46+ '\u001b[31mtest\u001b[39m' ,
47+ ) ;
48+ assert . strictEqual (
49+ util . styleText ( [ 'bold' , 'red' ] , 'test' , { validateStream : false } ) ,
50+ '\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m' ,
51+ ) ;
4352
44- assert . strictEqual (
45- util . styleText ( [ 'bold' , 'red' ] , 'test' , { validateStream : false } ) ,
46- '\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m' ,
47- ) ;
53+ assert . strictEqual (
54+ util . styleText ( [ 'bold' , 'red' ] , 'test' , { validateStream : false } ) ,
55+ util . styleText (
56+ 'bold' ,
57+ util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
58+ { validateStream : false } ,
59+ ) ,
60+ ) ;
4861
49- assert . strictEqual (
50- util . styleText ( [ 'bold' , 'red' ] , 'test' , { validateStream : false } ) ,
51- util . styleText (
52- 'bold' ,
53- util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
54- { validateStream : false } ,
55- ) ,
56- ) ;
62+ assert . throws ( ( ) => {
63+ util . styleText ( 'red' , 'text' , { stream : { } } ) ;
64+ } , {
65+ code : 'ERR_INVALID_ARG_TYPE' ,
66+ } ) ;
5767
58- assert . throws ( ( ) => {
59- util . styleText ( [ 'invalid' ] , 'text' ) ;
60- } , {
61- code : 'ERR_INVALID_ARG_VALUE' ,
62- } ) ;
68+ assert . doesNotThrow ( ( ) => {
69+ util . styleText ( 'red' , 'text' , { stream : { } , validateStream : false } ) ;
70+ } ) ;
71+ assert . strictEqual (
72+ util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
73+ styled ,
74+ ) ;
6375
64- assert . throws ( ( ) => {
65- util . styleText ( 'red' , 'text' , { stream : { } } ) ;
66- } , {
67- code : 'ERR_INVALID_ARG_TYPE' ,
68- } ) ;
76+ const fd = common . getTTYfd ( ) ;
77+ if ( fd !== - 1 ) {
78+ const writeStream = new WriteStream ( fd ) ;
6979
70- // does not throw
71- util . styleText ( 'red' , 'text' , { stream : { } , validateStream : false } ) ;
72-
73- assert . strictEqual (
74- util . styleText ( 'red' , 'test' , { validateStream : false } ) ,
75- styled ,
76- ) ;
77-
78- const fd = common . getTTYfd ( ) ;
79- if ( fd !== - 1 ) {
80- const writeStream = new WriteStream ( fd ) ;
81-
82- const originalEnv = process . env ;
83- [
84- { isTTY : true , env : { } , expected : styled } ,
85- { isTTY : false , env : { } , expected : noChange } ,
86- { isTTY : true , env : { NODE_DISABLE_COLORS : '1' } , expected : noChange } ,
87- { isTTY : true , env : { NO_COLOR : '1' } , expected : noChange } ,
88- { isTTY : true , env : { FORCE_COLOR : '1' } , expected : styled } ,
89- { isTTY : true , env : { FORCE_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
90- { isTTY : false , env : { FORCE_COLOR : '1' , NO_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
91- { isTTY : true , env : { FORCE_COLOR : '1' , NO_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
92- ] . forEach ( ( testCase ) => {
93- writeStream . isTTY = testCase . isTTY ;
94- process . env = {
95- ...process . env ,
96- ...testCase . env
97- } ;
98- {
99- const output = util . styleText ( 'red' , 'test' , { stream : writeStream } ) ;
100- assert . strictEqual ( output , testCase . expected ) ;
101- }
102- {
103- // Check that when passing an array of styles, the output behaves the same
104- const output = util . styleText ( [ 'red' ] , 'test' , { stream : writeStream } ) ;
105- assert . strictEqual ( output , testCase . expected ) ;
80+ const originalEnv = process . env ;
81+ [
82+ { isTTY : true , env : { } , expected : styled } ,
83+ { isTTY : false , env : { } , expected : noChange } ,
84+ { isTTY : true , env : { NODE_DISABLE_COLORS : '1' } , expected : noChange } ,
85+ { isTTY : true , env : { NO_COLOR : '1' } , expected : noChange } ,
86+ { isTTY : true , env : { FORCE_COLOR : '1' } , expected : styled } ,
87+ { isTTY : true , env : { FORCE_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
88+ { isTTY : false , env : { FORCE_COLOR : '1' , NO_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
89+ { isTTY : true , env : { FORCE_COLOR : '1' , NO_COLOR : '1' , NODE_DISABLE_COLORS : '1' } , expected : styled } ,
90+ ] . forEach ( ( testCase ) => {
91+ writeStream . isTTY = testCase . isTTY ;
92+ process . env = {
93+ ...process . env ,
94+ ...testCase . env
95+ } ;
96+ {
97+ const output = util . styleText ( 'red' , 'test' , { stream : writeStream } ) ;
98+ assert . strictEqual ( output , testCase . expected ) ;
99+ }
100+ {
101+ // Check that when passing an array of styles, the output behaves the same
102+ const output = util . styleText ( [ 'red' ] , 'test' , { stream : writeStream } ) ;
103+ assert . strictEqual ( output , testCase . expected ) ;
104+ }
105+ process . env = originalEnv ;
106+ } ) ;
107+ } else {
108+ common . skip ( 'Could not create TTY fd' ) ;
106109 }
107- process . env = originalEnv ;
108- } ) ;
109- } else {
110- common . skip ( 'Could not create TTY fd' ) ;
111- }
0 commit comments