forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
145 lines (115 loc) · 3.29 KB
/
logger.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
const assert = require('assert');
const sinon = require('sinon');
const Logger = require('../src/Logger');
describe('Logger', () => {
let log;
beforeEach(function() {
log = [];
});
const stub = instance => {
sinon.stub(instance, '_log').callsFake(message => {
log.push(message);
});
};
it('should log message on write', () => {
const l = new Logger.constructor({});
stub(l);
l.write('hello');
assert.equal(log[0], 'hello');
});
it('should track number of lines on persist false', () => {
const l = new Logger.constructor({});
stub(l);
const count = l.lines;
l.write('hello\nworld', false);
assert.equal(l.lines, count + 2);
});
it('should not track number of lines on persist true', () => {
const l = new Logger.constructor({});
stub(l);
const count = l.lines;
l.write('hello\nworld', true);
assert.equal(l.lines, count);
});
it('should respect log levels', () => {
const l = new Logger.constructor({logLevel: 2, color: false});
stub(l);
l.log('message');
l.persistent('message');
l.status('🚨', 'message');
l.logLevel = 1;
l.warn('message');
l.logLevel = 0;
l.error({message: 'message', stack: 'stack'});
assert.equal(log.length, 0);
l.logLevel = 1;
l.error({message: 'message', stack: 'stack'});
assert.equal(log.length, 1);
l.logLevel = 2;
l.warn('message');
assert.equal(log.length, 2);
l.logLevel = 3;
l.log('message');
l.persistent('message');
l.status('🚨', 'message');
assert.equal(log.length, 5);
});
it('should handle lack of color support with alternatives', () => {
const l = new Logger.constructor({color: false});
stub(l);
// clear is a no-op
l.lines = 4;
l.statusLine = 'hello';
l.clear();
assert.equal(l.lines, 4);
assert.equal(l.statusLine, 'hello');
// write-line calls log
const spy = sinon.spy(l, 'log');
l.writeLine(1, 'hello');
assert(spy.called);
});
it('should reset on clear', () => {
const l = new Logger.constructor({color: true, isTest: false});
stub(l);
l.lines = 10;
l.statusLine = 'hello';
l.clear();
assert.equal(l.lines, 0);
assert.equal(l.statusLine, null);
});
it('should log emoji and message via status', () => {
const l = new Logger.constructor({color: false});
stub(l);
l.status('🚨', 'hello');
assert(log[0].includes('🚨'));
assert(log[0].includes('hello'));
});
it('should use internal _log function for writes', () => {
const l = new Logger.constructor({color: false});
const sandbox = sinon.createSandbox(); // use sandbox to silence console.log
let spy;
try {
spy = sandbox.spy(l, '_log');
sandbox.stub(console, 'log');
l.write('hello world');
} finally {
l._log.restore();
sandbox.restore();
}
assert(spy.called);
});
it('should use stdout directly for writeLine', () => {
const l = new Logger.constructor({color: true});
const sandbox = sinon.createSandbox();
const log = [];
try {
sandbox.stub(process.stdout, 'write').callsFake(message => {
log.push(message);
});
l.writeLine(0, 'hello');
} finally {
sandbox.restore();
}
assert(log.includes('hello'));
});
});