22
22
'use strict' ;
23
23
const common = require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
+ const util = require ( 'util' ) ;
25
26
26
27
const [ , , modeArgv , sectionArgv ] = process . argv ;
27
28
@@ -54,19 +55,36 @@ function parent() {
54
55
test ( '*-test' , true , 'abc-test' ) ;
55
56
}
56
57
57
- function test ( environ , shouldWrite , section ) {
58
+ function test ( environ , shouldWrite , section , forceColors = false ) {
58
59
let expectErr = '' ;
59
60
const expectOut = 'ok\n' ;
60
61
61
62
const spawn = require ( 'child_process' ) . spawn ;
62
63
const child = spawn ( process . execPath , [ __filename , 'child' , section ] , {
63
- env : Object . assign ( process . env , { NODE_DEBUG : environ } )
64
+ env : Object . assign ( process . env , {
65
+ NODE_DEBUG : environ ,
66
+ FORCE_COLOR : forceColors ? 'true' : 'false'
67
+ } )
64
68
} ) ;
65
69
66
70
if ( shouldWrite ) {
67
- expectErr =
68
- `${ section . toUpperCase ( ) } ${ child . pid } : this { is: 'a' } /debugging/\n${
69
- section . toUpperCase ( ) } ${ child . pid } : num=1 str=a obj={"foo":"bar"}\n`;
71
+ if ( forceColors ) {
72
+ const { colors, styles } = util . inspect ;
73
+ const addCodes = ( arr ) => [ `\x1B[${ arr [ 0 ] } m` , `\x1B[${ arr [ 1 ] } m` ] ;
74
+ const num = addCodes ( colors [ styles . number ] ) ;
75
+ const str = addCodes ( colors [ styles . string ] ) ;
76
+ const regexp = addCodes ( colors [ styles . regexp ] ) ;
77
+ const start = `${ section . toUpperCase ( ) } ${ num [ 0 ] } ${ child . pid } ${ num [ 1 ] } ` ;
78
+ const debugging = `${ regexp [ 0 ] } /debugging/${ regexp [ 1 ] } ` ;
79
+ expectErr =
80
+ `${ start } : this { is: ${ str [ 0 ] } 'a'${ str [ 1 ] } } ${ debugging } \n` +
81
+ `${ start } : num=1 str=a obj={"foo":"bar"}\n` ;
82
+ } else {
83
+ const start = `${ section . toUpperCase ( ) } ${ child . pid } ` ;
84
+ expectErr =
85
+ `${ start } : this { is: 'a' } /debugging/\n` +
86
+ `${ start } : num=1 str=a obj={"foo":"bar"}\n` ;
87
+ }
70
88
}
71
89
72
90
let err = '' ;
@@ -85,12 +103,20 @@ function test(environ, shouldWrite, section) {
85
103
assert ( ! c ) ;
86
104
assert . strictEqual ( err , expectErr ) ;
87
105
assert . strictEqual ( out , expectOut ) ;
106
+ // Run the test again, this time with colors enabled.
107
+ if ( ! forceColors ) {
108
+ test ( environ , shouldWrite , section , true ) ;
109
+ }
88
110
} ) ) ;
89
111
}
90
112
91
113
92
114
function child ( section ) {
93
- const util = require ( 'util' ) ;
115
+ const tty = require ( 'tty' ) ;
116
+ // Make sure we check for colors, no matter of the stream's default.
117
+ Object . defineProperty ( process . stderr , 'hasColors' , {
118
+ value : tty . WriteStream . prototype . hasColors
119
+ } ) ;
94
120
const debug = util . debuglog ( section ) ;
95
121
debug ( 'this' , { is : 'a' } , / d e b u g g i n g / ) ;
96
122
debug ( 'num=%d str=%s obj=%j' , 1 , 'a' , { foo : 'bar' } ) ;
0 commit comments