@@ -5,32 +5,65 @@ const assert = require('assert').strict;
5
5
/* eslint-disable no-restricted-properties */
6
6
const { WriteStream } = require ( 'tty' ) ;
7
7
8
+ const NODE_DISABLE_COLORS = process . env . NODE_DISABLE_COLORS ;
9
+ delete process . env . NODE_DISABLE_COLORS ;
10
+
8
11
const fd = common . getTTYfd ( ) ;
9
12
const writeStream = new WriteStream ( fd ) ;
10
13
11
14
{
12
15
const depth = writeStream . getColorDepth ( ) ;
13
-
14
16
assert . equal ( typeof depth , 'number' ) ;
15
17
assert ( depth >= 1 && depth <= 24 ) ;
16
-
17
- if ( depth === 1 ) {
18
- // Terminal does not support colors, compare to a value that would.
19
- assert . notEqual ( writeStream . getColorDepth ( { COLORTERM : '1' } ) , depth ) ;
20
- } else {
21
- // Terminal supports colors, compare to a value that would not.
22
- assert . notEqual ( writeStream . getColorDepth ( { TERM : 'dumb' } ) , depth ) ;
23
- }
24
18
}
25
19
26
- // Deactivate colors
27
- {
28
- const tmp = process . env . NODE_DISABLE_COLORS ;
29
- process . env . NODE_DISABLE_COLORS = 1 ;
30
-
31
- const depth = writeStream . getColorDepth ( ) ;
20
+ // Check different environment variables.
21
+ [
22
+ [ { COLORTERM : '1' } , 4 ] ,
23
+ [ { TMUX : '1' } , 8 ] ,
24
+ [ { CI : '1' } , 1 ] ,
25
+ [ { CI : '1' , TRAVIS : '1' } , 8 ] ,
26
+ [ { CI : '1' , CIRCLECI : '1' } , 8 ] ,
27
+ [ { CI : '1' , APPVEYOR : '1' } , 8 ] ,
28
+ [ { CI : '1' , GITLAB_CI : '1' } , 8 ] ,
29
+ [ { CI : '1' , CI_NAME : 'codeship' } , 8 ] ,
30
+ [ { TEAMCITY_VERSION : '1.0.0' } , 1 ] ,
31
+ [ { TEAMCITY_VERSION : '9.11.0' } , 4 ] ,
32
+ [ { TERM_PROGRAM : 'iTerm.app' } , 8 ] ,
33
+ [ { TERM_PROGRAM : 'iTerm.app' , TERM_PROGRAM_VERSION : '3.0' } , 24 ] ,
34
+ [ { TERM_PROGRAM : 'iTerm.app' , TERM_PROGRAM_VERSION : '2.0' } , 8 ] ,
35
+ [ { TERM_PROGRAM : 'HyperTerm' } , 24 ] ,
36
+ [ { TERM_PROGRAM : 'Hyper' } , 24 ] ,
37
+ [ { TERM_PROGRAM : 'MacTerm' } , 24 ] ,
38
+ [ { TERM_PROGRAM : 'Apple_Terminal' } , 8 ] ,
39
+ [ { TERM : 'xterm-256' } , 8 ] ,
40
+ [ { TERM : 'ansi' } , 4 ] ,
41
+ [ { TERM : 'ANSI' } , 4 ] ,
42
+ [ { TERM : 'color' } , 4 ] ,
43
+ [ { TERM : 'linux' } , 4 ] ,
44
+ [ { TERM : 'fail' } , 1 ] ,
45
+ [ { NODE_DISABLE_COLORS : '1' } , 1 ] ,
46
+ [ { TERM : 'dumb' } , 1 ] ,
47
+ [ { TERM : 'dumb' , COLORTERM : '1' } , 4 ] ,
48
+ ] . forEach ( ( [ env , depth ] , i ) => {
49
+ const actual = writeStream . getColorDepth ( env ) ;
50
+ assert . equal (
51
+ actual ,
52
+ depth ,
53
+ `i: ${ i } , expected: ${ depth } , actual: ${ actual } , env: ${ env } `
54
+ ) ;
55
+ } ) ;
32
56
33
- assert . equal ( depth , 1 ) ;
57
+ // OS settings
58
+ {
59
+ const platform = Object . getOwnPropertyDescriptor ( process , 'platform' ) ;
60
+ const [ value , depth1 , depth2 ] = process . platform !== 'win32' ?
61
+ [ 'win32' , 1 , 4 ] : [ 'linux' , 4 , 1 ] ;
34
62
35
- process . env . NODE_DISABLE_COLORS = tmp ;
63
+ assert . equal ( writeStream . getColorDepth ( { } ) , depth1 ) ;
64
+ Object . defineProperty ( process , 'platform' , { value } ) ;
65
+ assert . equal ( writeStream . getColorDepth ( { } ) , depth2 ) ;
66
+ Object . defineProperty ( process , 'platform' , platform ) ;
36
67
}
68
+
69
+ process . env . NODE_DISABLE_COLORS = NODE_DISABLE_COLORS ;
0 commit comments