This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
basic.js
508 lines (471 loc) · 17.1 KB
/
basic.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
const t = require('tap')
const log = require('../')
const stream = require('stream')
const result = []
const logEvents = []
const logInfoEvents = []
const logPrefixEvents = []
const resultExpect = [
// eslint-disable-next-line max-len
'\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[7msill\u001b[0m \u001b[0m\u001b[35msilly prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mverb\u001b[0m \u001b[0m\u001b[35mverbose prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32minfo\u001b[0m \u001b[0m\u001b[35minfo prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32;40mtiming\u001b[0m \u001b[0m\u001b[35mtiming prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32;40mhttp\u001b[0m \u001b[0m\u001b[35mhttp prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m \u001b[0m\u001b[35mnotice prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[30;43mWARN\u001b[0m \u001b[0m\u001b[35mwarn prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35merror prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32minfo\u001b[0m \u001b[0m\u001b[35minfo prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32;40mtiming\u001b[0m \u001b[0m\u001b[35mtiming prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[32;40mhttp\u001b[0m \u001b[0m\u001b[35mhttp prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m \u001b[0m\u001b[35mnotice prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[30;43mWARN\u001b[0m \u001b[0m\u001b[35mwarn prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35merror prefix\u001b[0m x = {"foo":{"bar":"baz"}}\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35m404\u001b[0m This is a longer\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35m404\u001b[0m message, with some details\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35m404\u001b[0m and maybe a stack.\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[31;40mERR!\u001b[0m \u001b[0m\u001b[35m404\u001b[0m \n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u0007noise\u001b[0m\u001b[35m\u001b[0m LOUD NOISES\n',
// eslint-disable-next-line max-len
'\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u0007noise\u001b[0m \u001b[0m\u001b[35merror\u001b[0m erroring\n',
'\u001b[0m',
]
const logPrefixEventsExpect = [
{ id: 2,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 11,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 20,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
]
// should be the same.
const logInfoEventsExpect = logPrefixEventsExpect
const logEventsExpect = [
{ id: 0,
level: 'silly',
prefix: 'silly prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 1,
level: 'verbose',
prefix: 'verbose prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 2,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 3,
level: 'timing',
prefix: 'timing prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 4,
level: 'http',
prefix: 'http prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 5,
level: 'notice',
prefix: 'notice prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 6,
level: 'warn',
prefix: 'warn prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 7,
level: 'error',
prefix: 'error prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 8,
level: 'silent',
prefix: 'silent prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 9,
level: 'silly',
prefix: 'silly prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 10,
level: 'verbose',
prefix: 'verbose prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 11,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 12,
level: 'timing',
prefix: 'timing prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 13,
level: 'http',
prefix: 'http prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 14,
level: 'notice',
prefix: 'notice prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 15,
level: 'warn',
prefix: 'warn prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 16,
level: 'error',
prefix: 'error prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 17,
level: 'silent',
prefix: 'silent prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 18,
level: 'silly',
prefix: 'silly prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 19,
level: 'verbose',
prefix: 'verbose prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 20,
level: 'info',
prefix: 'info prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 21,
level: 'timing',
prefix: 'timing prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 22,
level: 'http',
prefix: 'http prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 23,
level: 'notice',
prefix: 'notice prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 24,
level: 'warn',
prefix: 'warn prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 25,
level: 'error',
prefix: 'error prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 26,
level: 'silent',
prefix: 'silent prefix',
message: 'x = {"foo":{"bar":"baz"}}',
messageRaw: ['x = %j', { foo: { bar: 'baz' } }] },
{ id: 27,
level: 'error',
prefix: '404',
message: 'This is a longer\nmessage, with some details\nand maybe a stack.\n',
messageRaw: ['This is a longer\nmessage, with some details\nand maybe a stack.\n'] },
{ id: 28,
level: 'noise',
prefix: false,
message: 'LOUD NOISES',
messageRaw: ['LOUD NOISES'] },
{ id: 29,
level: 'noise',
prefix: 'error',
message: 'erroring',
messageRaw: ['erroring'] },
]
const Stream = require('stream').Stream
const s = new Stream()
s.write = function (m) {
result.push(m)
}
s.writable = true
s.isTTY = true
s.end = function () {}
log.stream = s
log.heading = 'npm'
t.test('basic', async t => {
t.same(log.stream, s, 'stream getter works')
log.on('log', logEvents.push.bind(logEvents))
log.on('log.info', logInfoEvents.push.bind(logInfoEvents))
log.on('info prefix', logPrefixEvents.push.bind(logPrefixEvents))
console.error('log.level=silly')
log.level = 'silly'
log.silly('silly prefix', 'x = %j', { foo: { bar: 'baz' } })
log.verbose('verbose prefix', 'x = %j', { foo: { bar: 'baz' } })
log.info('info prefix', 'x = %j', { foo: { bar: 'baz' } })
log.timing('timing prefix', 'x = %j', { foo: { bar: 'baz' } })
log.http('http prefix', 'x = %j', { foo: { bar: 'baz' } })
log.notice('notice prefix', 'x = %j', { foo: { bar: 'baz' } })
log.warn('warn prefix', 'x = %j', { foo: { bar: 'baz' } })
log.error('error prefix', 'x = %j', { foo: { bar: 'baz' } })
log.silent('silent prefix', 'x = %j', { foo: { bar: 'baz' } })
console.error('log.level=silent')
log.level = 'silent'
log.silly('silly prefix', 'x = %j', { foo: { bar: 'baz' } })
log.verbose('verbose prefix', 'x = %j', { foo: { bar: 'baz' } })
log.info('info prefix', 'x = %j', { foo: { bar: 'baz' } })
log.timing('timing prefix', 'x = %j', { foo: { bar: 'baz' } })
log.http('http prefix', 'x = %j', { foo: { bar: 'baz' } })
log.notice('notice prefix', 'x = %j', { foo: { bar: 'baz' } })
log.warn('warn prefix', 'x = %j', { foo: { bar: 'baz' } })
log.error('error prefix', 'x = %j', { foo: { bar: 'baz' } })
log.silent('silent prefix', 'x = %j', { foo: { bar: 'baz' } })
console.error('log.level=info')
log.level = 'info'
log.silly('silly prefix', 'x = %j', { foo: { bar: 'baz' } })
log.verbose('verbose prefix', 'x = %j', { foo: { bar: 'baz' } })
log.info('info prefix', 'x = %j', { foo: { bar: 'baz' } })
log.timing('timing prefix', 'x = %j', { foo: { bar: 'baz' } })
log.http('http prefix', 'x = %j', { foo: { bar: 'baz' } })
log.notice('notice prefix', 'x = %j', { foo: { bar: 'baz' } })
log.warn('warn prefix', 'x = %j', { foo: { bar: 'baz' } })
log.error('error prefix', 'x = %j', { foo: { bar: 'baz' } })
log.silent('silent prefix', 'x = %j', { foo: { bar: 'baz' } })
log.error('404', 'This is a longer\n' +
'message, with some details\n' +
'and maybe a stack.\n')
log.addLevel('noise', 10000, { beep: true })
log.noise(false, 'LOUD NOISES')
log.noise('error', 'erroring')
t.same(result.join('').trim(), resultExpect.join('').trim(), 'result')
t.same(log.record, logEventsExpect, 'record')
t.same(logEvents, logEventsExpect, 'logEvents')
t.same(logInfoEvents, logInfoEventsExpect, 'logInfoEvents')
t.same(logPrefixEvents, logPrefixEventsExpect, 'logPrefixEvents')
})
t.test('util functions', async t => {
t.teardown(() => {
log.resume()
log.gauge.enable()
})
t.test('enableColor', async t => {
t.teardown(() => {
log.disableColor()
})
log.enableColor()
t.same(log.useColor(), true, 'log has color enabled')
t.match(log.gauge._theme, { hasColor: true }, 'gauge has color enabled')
})
t.test('disableColor', async t => {
log.disableColor()
t.same(log.useColor(), false, 'color is disabled')
t.match(log.gauge._theme, { hasColor: false }, 'gauge has color disabled')
})
t.test('enableUnicode', async t => {
t.teardown(() => {
log.disableUnicode()
})
log.enableUnicode()
t.match(log.gauge._theme, { hasUnicode: true }, 'gauge has unicode enabled')
})
t.test('disableUnicode', async t => {
log.disableUnicode()
t.match(log.gauge._theme, { hasUnicode: false }, 'gauge has unicode disabled')
})
t.test('themes', async t => {
const _themes = log.gauge._themes
t.teardown(() => {
log.setGaugeThemeset(_themes)
})
const themes = require('gauge/lib/themes')
const newThemes = themes.newThemeSet()
log.setGaugeThemeset(newThemes)
t.match(log.gauge._themes, newThemes, 'gauge has new theme set')
})
t.test('template', async t => {
const _template = log.gauge._gauge.template
t.teardown(() => {
log.gauge._gauge.template = _template
})
const template = [{ type: 'progressbar', length: 100 }]
log.setGaugeTemplate(template)
t.match(log.gauge._gauge.template, template)
})
t.test('enableProgress while paused', async t => {
t.teardown(() => {
log.enableProgress()
log.resume()
})
log.disableProgress()
log.pause()
log.enableProgress()
t.same(log.gauge.isEnabled(), false, 'gauge is still disabled')
})
t.test('pause while progressEnabled', async t => {
t.teardown(() => {
log.resume()
})
log.pause()
t.same(log.gauge.isEnabled(), false, 'gauge is disabled')
})
t.test('_buffer while paused', async t => {
t.teardown(() => {
log.resume()
})
// const bufferLength = this._buffer.length
log.pause()
log.log('verbose', 'test', 'test log')
t.equal(log._buffer.length, 1, 'message was buffered')
log.resume()
t.equal(log._buffer.length, 0, 'message was unbuffered')
})
})
t.test('log.log', async t => {
t.test('emits error on bad loglevel', t => {
log.once('error', (err) => {
t.match(err, /Undefined log level: "asdf"/)
t.end()
})
log.log('asdf', 'bad loglevel')
})
t.test('resolves stack traces to a plain string', t => {
log.once('log', (m) => {
t.match(m.message, 'Error: with a stack trace', 'has error message')
t.match(m.message, 'at Test', 'has stack info')
t.end()
})
const err = new Error('with a stack trace')
log.log('verbose', 'oops', err)
})
t.test('max record size', async t => {
const mrs = log.maxRecordSize
t.teardown(() => {
log.maxRecordSize = mrs
})
log.maxRecordSize = 3
log.log('verbose', 'test', 'log 1')
log.log('verbose', 'test', 'log 2')
log.log('verbose', 'test', 'log 3')
log.log('verbose', 'test', 'log 4')
t.equal(log.record.length, 3, 'only maxRecordSize entries in record')
})
})
t.test('write with no stream', async t => {
const gauge = log.gauge
t.teardown(() => {
log.gauge = gauge
log.stream = s
})
log.gauge = null
log.stream = null
log.write('message')
t.ok('does not throw')
})
t.test('emitLog to nonexistant level', async t => {
t.teardown(() => {
log.stream = s
})
const badStream = new stream.Writable()
badStream.on('data', () => {
throw new Error('should not have gotten data!')
})
log.emitLog({ prefix: 'test', level: 'asdf' })
t.ok('does not throw')
})
t.test('emitLog to nonexistant level', async t => {
t.teardown(() => {
log.stream = s
})
const badStream = new stream.Writable()
badStream.on('data', () => {
throw new Error('should not have gotten data!')
})
log.emitLog({ prefix: 'test', level: 'asdf' })
t.ok('does not throw')
})
t.test('_format with nonexistant stream', async t => {
const gauge = log.gauge
t.teardown(() => {
log.gauge = gauge
log.stream = s
})
log.gauge = null
log.stream = null
t.match(log._format('message'), undefined, 'does nothing')
})
t.test('_format', async t => {
t.teardown(() => {
log.disableColor()
})
t.test('nonexistant stream', async t => {
const gauge = log.gauge
t.teardown(() => {
log.gauge = gauge
log.stream = s
})
log.gauge = null
log.stream = null
t.match(log._format('message'), undefined, 'does nothing')
})
t.test('fg', async t => {
log.enableColor()
const o = log._format('test message', { bg: 'blue' })
t.match(o, '\u001b[44mtest message\u001b[0m')
})
t.test('bg', async t => {
log.enableColor()
const o = log._format('test message', { bg: 'white' })
t.match(o, '\u001b[47mtest message\u001b[0m')
})
t.test('bold', async t => {
log.enableColor()
const o = log._format('test message', { bold: true })
t.match(o, '\u001b[1mtest message\u001b[0m')
})
t.test('underline', async t => {
log.enableColor()
const o = log._format('test message', { underline: true })
t.match(o, '\u001b[4mtest message\u001b[0m')
})
t.test('inverse', async t => {
log.enableColor()
const o = log._format('test message', { inverse: true })
t.match(o, '\u001b[7mtest message\u001b[0m')
})
})