1
1
'use strict' ;
2
2
3
- const should = require ( 'chai' ) . should ( ) ; // eslint-disable-line
3
+ require ( 'chai' ) . should ( ) ;
4
4
const rewire = require ( 'rewire' ) ;
5
5
const sinon = require ( 'sinon' ) ;
6
6
7
+ const noop = ( ) => { } ;
8
+ const fakeConsole = {
9
+ trace : noop ,
10
+ debug : noop ,
11
+ info : noop ,
12
+ warn : noop ,
13
+ error : noop
14
+ } ;
15
+ const fakeProcess = {
16
+ process : {
17
+ stderr : {
18
+ write : noop
19
+ } ,
20
+ stdout : {
21
+ write : noop
22
+ }
23
+ }
24
+ } ;
25
+
26
+ const logger = require ( '../lib/log' ) ;
27
+
7
28
describe ( 'hexo-log' , ( ) => {
8
- const logger = require ( '../lib/log' ) ;
9
- const loggerModule = rewire ( '../lib/log' ) ;
29
+ let loggerModule ;
30
+
31
+ beforeEach ( ( ) => {
32
+ sinon . restore ( ) ;
33
+ loggerModule = rewire ( '../lib/log' ) ;
34
+ } ) ;
10
35
11
36
it ( 'add alias for levels' , ( ) => {
12
37
const log = logger ( ) ;
@@ -18,70 +43,305 @@ describe('hexo-log', () => {
18
43
log . log . should . eql ( log . info ) ;
19
44
} ) ;
20
45
21
- it ( 'default name is hexo' , ( ) => {
22
- const log = logger ( ) ;
46
+ it ( 'trace - should call console.trace' , ( ) => {
47
+ const spy = sinon . spy ( ) ;
48
+ loggerModule . __set__ ( 'console.trace' , spy ) ;
49
+
50
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
51
+ const log = loggerModule ( { debug : true } ) ;
52
+ log . trace ( 'test' ) ;
53
+ } ) ;
23
54
24
- log . fields . name . should . eql ( 'hexo' ) ;
55
+ spy . called . should . be . true ;
25
56
} ) ;
26
57
27
- it ( 'options.name' , ( ) => {
28
- const log = logger ( { name : 'foo' } ) ;
58
+ it ( 'trace - with stderr and no stdout' , ( ) => {
59
+ const stdoutSpy = sinon . spy ( ) ;
60
+ const stderrSpy = sinon . spy ( ) ;
29
61
30
- log . fields . name . should . eql ( 'foo' ) ;
31
- } ) ;
62
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
32
63
33
- it ( 'level should be trace if options.debug is true' , ( ) => {
34
- const log = logger ( { debug : true } ) ;
64
+ loggerModule . __with__ ( {
65
+ process : {
66
+ stderr : {
67
+ write : stderrSpy
68
+ } ,
69
+ stdout : {
70
+ write : stdoutSpy
71
+ }
72
+ }
73
+ } ) ( ( ) => {
74
+ const log = loggerModule ( { debug : true } ) ;
75
+ log . trace ( 'test' ) ;
76
+ } ) ;
35
77
36
- log . streams [ 0 ] . level . should . eql ( 10 ) ;
78
+ stderrSpy . called . should . be . true ;
79
+ stdoutSpy . called . should . be . false ;
37
80
} ) ;
38
81
39
- it ( 'should add file stream if options.debug is true' , ( ) => {
40
- const log = logger ( { debug : true } ) ;
82
+ it ( 'debug - should call console.debug' , ( ) => {
83
+ const spy = sinon . spy ( ) ;
84
+ loggerModule . __set__ ( 'console.debug' , spy ) ;
85
+
86
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
87
+ const log = loggerModule ( { debug : true } ) ;
88
+ log . debug ( 'test' ) ;
89
+ } ) ;
41
90
42
- log . streams [ 1 ] . path . should . eql ( 'debug.log' ) ;
91
+ spy . called . should . be . true ;
43
92
} ) ;
44
93
45
- it ( 'should remove console stream if options.silent is true' , ( ) => {
46
- const log = logger ( { silent : true } ) ;
94
+ it ( 'debug - with stdout and no stderr' , ( ) => {
95
+ const stdoutSpy = sinon . spy ( ) ;
96
+ const stderrSpy = sinon . spy ( ) ;
47
97
48
- log . streams . length . should . eql ( 0 ) ;
98
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
99
+
100
+ loggerModule . __with__ ( {
101
+ process : {
102
+ stderr : {
103
+ write : stderrSpy
104
+ } ,
105
+ stdout : {
106
+ write : stdoutSpy
107
+ }
108
+ }
109
+ } ) ( ( ) => {
110
+ const log = loggerModule ( { debug : true } ) ;
111
+ log . debug ( 'test' ) ;
112
+ } ) ;
113
+
114
+ stderrSpy . called . should . be . false ;
115
+ stdoutSpy . called . should . be . true ;
49
116
} ) ;
50
117
51
- it ( 'should display time if options.debug is true ' , ( ) => {
118
+ it ( 'info - should call console.info ' , ( ) => {
52
119
const spy = sinon . spy ( ) ;
53
- const now = new Date ( ) ;
120
+ loggerModule . __set__ ( 'console.info' , spy ) ;
121
+
122
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
123
+ const log = loggerModule ( { debug : true } ) ;
124
+ log . info ( 'test' ) ;
125
+ } ) ;
126
+
127
+ spy . called . should . be . true ;
128
+ } ) ;
129
+
130
+ it ( 'info - with stdout and no stderr' , ( ) => {
131
+ const stdoutSpy = sinon . spy ( ) ;
132
+ const stderrSpy = sinon . spy ( ) ;
133
+
134
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
54
135
55
136
loggerModule . __with__ ( {
56
137
process : {
138
+ stderr : {
139
+ write : stderrSpy
140
+ } ,
57
141
stdout : {
58
- write : spy
142
+ write : stdoutSpy
59
143
}
60
144
}
61
145
} ) ( ( ) => {
62
- sinon . useFakeTimers ( now . valueOf ( ) ) ;
63
146
const log = loggerModule ( { debug : true } ) ;
64
147
log . info ( 'test' ) ;
65
- sinon . restore ( ) ;
66
148
} ) ;
67
149
68
- spy . args [ 0 ] [ 0 ] . should . contain ( now . toISOString ( ) . substring ( 11 , 23 ) ) ;
150
+ stderrSpy . called . should . be . false ;
151
+ stdoutSpy . called . should . be . true ;
152
+ } ) ;
153
+
154
+ it ( 'warn - should call console.warn' , ( ) => {
155
+ const spy = sinon . spy ( ) ;
156
+ loggerModule . __set__ ( 'console.warn' , spy ) ;
157
+
158
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
159
+ const log = loggerModule ( { debug : true } ) ;
160
+ log . warn ( 'test' ) ;
161
+ } ) ;
162
+
163
+ spy . called . should . be . true ;
164
+ } ) ;
165
+
166
+ it ( 'warn - with stderr and no stdout' , ( ) => {
167
+ const stdoutSpy = sinon . spy ( ) ;
168
+ const stderrSpy = sinon . spy ( ) ;
169
+
170
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
171
+
172
+ loggerModule . __with__ ( {
173
+ process : {
174
+ stderr : {
175
+ write : stderrSpy
176
+ } ,
177
+ stdout : {
178
+ write : stdoutSpy
179
+ }
180
+ }
181
+ } ) ( ( ) => {
182
+ const log = loggerModule ( { debug : true } ) ;
183
+ log . warn ( 'test' ) ;
184
+ } ) ;
185
+
186
+ stderrSpy . called . should . be . true ;
187
+ stdoutSpy . called . should . be . false ;
188
+ } ) ;
189
+
190
+ it ( 'error - should call console.error' , ( ) => {
191
+ const spy = sinon . spy ( ) ;
192
+ loggerModule . __set__ ( 'console.error' , spy ) ;
193
+
194
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
195
+ const log = loggerModule ( { debug : true } ) ;
196
+ log . error ( 'test' ) ;
197
+ } ) ;
198
+
199
+ spy . called . should . be . true ;
69
200
} ) ;
70
201
71
- it ( 'should print error to process.stderr stream' , ( ) => {
202
+ it ( 'error - with stderr and no stdout' , ( ) => {
203
+ const stdoutSpy = sinon . spy ( ) ;
204
+ const stderrSpy = sinon . spy ( ) ;
205
+
206
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
207
+
208
+ loggerModule . __with__ ( {
209
+ process : {
210
+ stderr : {
211
+ write : stderrSpy
212
+ } ,
213
+ stdout : {
214
+ write : stdoutSpy
215
+ }
216
+ }
217
+ } ) ( ( ) => {
218
+ const log = loggerModule ( { debug : true } ) ;
219
+ log . error ( 'test' ) ;
220
+ } ) ;
221
+
222
+ stderrSpy . called . should . be . true ;
223
+ stdoutSpy . called . should . be . false ;
224
+ } ) ;
225
+
226
+ it ( 'fatal - should call console.error' , ( ) => {
72
227
const spy = sinon . spy ( ) ;
228
+ loggerModule . __set__ ( 'console.error' , spy ) ;
229
+
230
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
231
+ const log = loggerModule ( { debug : true } ) ;
232
+ log . fatal ( 'test' ) ;
233
+ } ) ;
234
+
235
+ spy . called . should . be . true ;
236
+ } ) ;
237
+
238
+ it ( 'fatal - with stderr and no stdout' , ( ) => {
239
+ const stdoutSpy = sinon . spy ( ) ;
240
+ const stderrSpy = sinon . spy ( ) ;
241
+
242
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
73
243
74
244
loggerModule . __with__ ( {
75
245
process : {
76
246
stderr : {
247
+ write : stderrSpy
248
+ } ,
249
+ stdout : {
250
+ write : stdoutSpy
251
+ }
252
+ }
253
+ } ) ( ( ) => {
254
+ const log = loggerModule ( { debug : true } ) ;
255
+ log . fatal ( 'test' ) ;
256
+ } ) ;
257
+
258
+ stderrSpy . called . should . be . true ;
259
+ stdoutSpy . called . should . be . false ;
260
+ } ) ;
261
+
262
+ it ( 'options.debug - should display time' , ( ) => {
263
+ const spy = sinon . spy ( ) ;
264
+ const now = new Date ( ) ;
265
+
266
+ loggerModule . __set__ ( 'console' , fakeConsole ) ;
267
+
268
+ loggerModule . __with__ ( {
269
+ process : {
270
+ stdout : {
77
271
write : spy
78
272
}
79
273
}
80
274
} ) ( ( ) => {
81
- const log = loggerModule ( ) ;
275
+ sinon . useFakeTimers ( now . valueOf ( ) ) ;
276
+ const log = loggerModule ( { debug : true } ) ;
277
+ log . info ( 'test' ) ;
278
+ sinon . restore ( ) ;
279
+ } ) ;
280
+
281
+ spy . args [ 0 ] [ 0 ] . should . contain ( now . toISOString ( ) . substring ( 11 , 23 ) ) ;
282
+ } ) ;
283
+
284
+ it ( 'options.silent - should not display anything' , ( ) => {
285
+ const consoleTraceSpy = sinon . spy ( ) ;
286
+ const consoleDebugSpy = sinon . spy ( ) ;
287
+ const consoleInfoSpy = sinon . spy ( ) ;
288
+ const consoleWarnSpy = sinon . spy ( ) ;
289
+ const consoleErrorSpy = sinon . spy ( ) ;
290
+
291
+ loggerModule . __set__ ( 'console.trace' , consoleTraceSpy ) ;
292
+ loggerModule . __set__ ( 'console.debug' , consoleDebugSpy ) ;
293
+ loggerModule . __set__ ( 'console.info' , consoleInfoSpy ) ;
294
+ loggerModule . __set__ ( 'console.warn' , consoleWarnSpy ) ;
295
+ loggerModule . __set__ ( 'console.error' , consoleErrorSpy ) ;
296
+
297
+ loggerModule . __with__ ( fakeProcess ) ( ( ) => {
298
+ const log = loggerModule ( { silent : true } ) ;
299
+ log . trace ( 'test' ) ;
300
+ log . debug ( 'test' ) ;
301
+ log . info ( 'test' ) ;
302
+ log . warn ( 'test' ) ;
82
303
log . error ( 'test' ) ;
304
+ log . fatal ( 'test' ) ;
83
305
} ) ;
84
306
85
- spy . calledOnce . should . be . true ;
307
+ consoleTraceSpy . called . should . be . false ;
308
+ consoleDebugSpy . called . should . be . false ;
309
+ consoleInfoSpy . called . should . be . false ;
310
+ consoleWarnSpy . calledOnce . should . be . false ;
311
+ consoleErrorSpy . calledTwice . should . be . false ;
312
+ } ) ;
313
+ } ) ;
314
+
315
+ describe ( 'hexo-log example' , ( ) => {
316
+ const log = logger ( { debug : true } ) ;
317
+ const log2 = logger ( ) ;
318
+ it ( 'log.trace()' , ( ) => {
319
+ log . trace ( 'Hello, World!' ) ;
320
+ log2 . trace ( 'Hello, World!' ) ;
321
+ } ) ;
322
+
323
+ it ( 'log.debug()' , ( ) => {
324
+ log . debug ( 'Hello, World!' ) ;
325
+ log2 . debug ( 'Hello, World!' ) ;
326
+ } ) ;
327
+
328
+ it ( 'log.info()' , ( ) => {
329
+ log . info ( 'Hello, World!' ) ;
330
+ log2 . info ( 'Hello, World!' ) ;
331
+ } ) ;
332
+
333
+ it ( 'log.warn()' , ( ) => {
334
+ log . warn ( 'Hello, World!' ) ;
335
+ log2 . warn ( 'Hello, World!' ) ;
336
+ } ) ;
337
+
338
+ it ( 'log.error()' , ( ) => {
339
+ log . error ( 'Hello, World!' ) ;
340
+ log2 . error ( 'Hello, World!' ) ;
341
+ } ) ;
342
+
343
+ it ( 'log.fatal()' , ( ) => {
344
+ log . fatal ( 'Hello, World!' ) ;
345
+ log2 . fatal ( 'Hello, World!' ) ;
86
346
} ) ;
87
347
} ) ;
0 commit comments