Debian Codegraph @

color-support/test/basic.js

raw symbols SCIP
1 var t = require('tap')
2 var colorSupport = require('../')
3
4 var hasNone = {
5 level: 0,
6 hasBasic: false,
7 has256: false,
8 has16m: false
9 }
10
11 var hasBasic = {
12 level: 1,
13 hasBasic: true,
14 has256: false,
15 has16m: false
16 }
17
18 var has256 = {
19 level: 2,
20 hasBasic: true,
21 has256: true,
22 has16m: false
23 }
24
25 var has16m = {
26 level: 3,
27 hasBasic: true,
28 has256: true,
29 has16m: true
30 }
31
32 t.test('set fields on top level', function (t) {
33 t.match(colorSupport.level, Number)
34 t.match(colorSupport.hasBasic, Boolean)
35 t.match(colorSupport.has256, Boolean)
36 t.match(colorSupport.has16m, Boolean)
37
38 // flags on object match what you get calling it with no opts
39 // t.match() gets confused by functions, and we need to ensure that
40 // *something* gets returned when colors are unsupported
41 t.match(Object.create(colorSupport), colorSupport({ alwaysReturn: true }))
42 t.end()
43 })
44
45 t.test('non-node = no colors', function (t) {
46 var p = process.env
47 process.env = null
48 var supported = colorSupport()
49 process.env = p
50 t.notOk(supported)
51 t.end()
52 })
53
54 t.test('colors depend on being a tty', function (t) {
55 var notty = { isTTY: false }
56 t.notOk(colorSupport({ stream: notty }))
57
58 // if we ignore the tty, it's the same either way tho
59 t.same(colorSupport({
60 stream: notty,
61 ignoreTTY: true
62 }), colorSupport({ ignoreTTY: true }))
63
64 // or if it is a tty, same as ignoring it
65 t.same(colorSupport({
66 stream: { isTTY: true }
67 }), colorSupport({ ignoreTTY: true }))
68
69 t.end()
70 })
71
72 t.test('TERM=dumb', function (t) {
73 t.notOk(colorSupport({ term: 'dumb', ignoreTTY: true, env: {} }))
74
75 t.same(colorSupport({
76 term: 'dumb',
77 ignoreTTY: true,
78 ignoreDumb: true
79 }), colorSupport({ ignoreTTY: true }))
80
81 // with COLORTERM set, term=dumb is ignored
82 t.same(colorSupport({
83 term: 'dumb',
84 ignoreTTY: true,
85 alwaysReturn: true,
86 env: {
87 COLORTERM: 'yolo'
88 }
89 }), colorSupport({
90 ignoreTTY: true,
91 alwaysReturn: true,
92 env: {
93 COLORTERM:'yolo'
94 }
95 }))
96 t.end()
97 })
98
99 t.test('win32', function (t) {
100 t.same(colorSupport({
101 ignoreTTY: true,
102 ignoreDumb: true,
103 platform: 'win32'
104 }), hasBasic)
105 t.end()
106 })
107
108 t.test('tmux', function (t) {
109 t.same(colorSupport({
110 ignoreTTY: true,
111 ignoreDumb: true,
112 platform: 'linux',
113 env: { TMUX: 'some junk' }
114 }), has256)
115 t.end()
116 })
117
118 t.test('TERM_PROGRAM', function (t) {
119 t.same(colorSupport({
120 ignoreTTY: true,
121 ignoreDumb: true,
122 platform: 'darwin',
123 env: {
124 TERM_PROGRAM: 'iTerm.app'
125 }
126 }), has256)
127
128 t.same(colorSupport({
129 ignoreTTY: true,
130 ignoreDumb: true,
131 platform: 'darwin',
132 env: {
133 TERM_PROGRAM: 'Hyper',
134 TERM_PROGRAM_VERSION: '0.8.9'
135 }
136 }), has16m)
137
138 t.same(colorSupport({
139 ignoreTTY: true,
140 ignoreDumb: true,
141 platform: 'darwin',
142 env: {
143 TERM_PROGRAM: 'HyperTerm',
144 TERM_PROGRAM_VERSION: '0.7.0'
145 }
146 }), has16m)
147
148 t.same(colorSupport({
149 ignoreTTY: true,
150 ignoreDumb: true,
151 platform: 'darwin',
152 env: {
153 TERM_PROGRAM: 'iTerm.app',
154 TERM_PROGRAM_VERSION: '3.0.4'
155 }
156 }), has16m)
157
158 t.same(colorSupport({
159 ignoreTTY: true,
160 ignoreDumb: true,
161 platform: 'darwin',
162 env: {
163 TERM_PROGRAM: 'MacTerm'
164 }
165 }), has16m)
166
167 t.same(colorSupport({
168 ignoreTTY: true,
169 ignoreDumb: true,
170 platform: 'darwin',
171 env: {
172 TERM_PROGRAM: 'Apple_Terminal'
173 }
174 }), has256)
175
176 t.end()
177 })
178
179 t.test('colors on CI servers', function (t) {
180 var options = {
181 ignoreTTY: true,
182 alwaysReturn: true,
183 ignoreDumb: true,
184 platform: 'darwin',
185 env: {
186 CI: '1'
187 }
188 }
189 t.same(colorSupport(options), hasNone)
190 options.env.TRAVIS = '1'
191 t.same(colorSupport(options), has256)
192 options.env = { TEAMCITY_VERSION: '1' }
193 t.same(colorSupport(options), hasNone)
194
195 options.ignoreCI = true
196 t.same(colorSupport(options), colorSupport({
197 ignoreTTY: true,
198 alwaysReturn: true,
199 ignoreDumb: true,
200 platform: 'darwin',
201 env: {}
202 }))
203 t.end()
204 })
205
206 t.test('TERM=stuff', function (t) {
207 var options = {
208 ignoreTTY: true,
209 alwaysReturn: true,
210 ignoreDumb: true,
211 platform: 'definitely not windows',
212 env: {}
213 }
214
215 options.term = 'xterm-256etc'
216 t.same(colorSupport(options), has256)
217
218 options.term = 'screen that is blue, of death'
219 t.same(colorSupport(options), hasBasic)
220
221 options.term = 'xterm-something else'
222 t.same(colorSupport(options), hasBasic)
223
224 options.term = 'vt100 and 1 dalmations'
225 t.same(colorSupport(options), hasBasic)
226
227 options.term = 'are we human or are we dansir'
228 t.same(colorSupport(options), hasBasic)
229
230 options.term = 'check out dat linux, gpl af'
231 t.same(colorSupport(options), hasBasic)
232
233 t.end()
234 })
235
236 t.test('env.COLORTERM makes it basic at least', function (t) {
237 var options = {
238 ignoreTTY: true,
239 alwaysReturn: true,
240 ignoreDumb: true,
241 platform: 'definitely not windows',
242 env: { COLORTERM: 'yeah whatever' }
243 }
244
245 t.same(colorSupport(options), hasBasic)
246
247 t.end()
248 })
249
250 t.test('mutates an object passed in', function (t) {
251 var obj = { level: 'up' }
252 colorSupport({}, obj)
253 t.match(obj.level, Number)
254 t.end()
255 })
256
257 t.test('getting a specific level', function (t) {
258 t.match(colorSupport({ level: 0, alwaysReturn: true }), hasNone)
259 t.notOk(colorSupport({ level: 0 }))
260 t.match(colorSupport({ level: 1 }), hasBasic)
261 t.match(colorSupport({ level: 2 }), has256)
262 t.match(colorSupport({ level: 3 }), has16m)
263 t.match(colorSupport({ level: 4 }), colorSupport())
264 t.end()
265 })