1 | 'use strict';
|
2 |
|
3 | var legacyLog = require('../');
|
4 | var Log = legacyLog.Log;
|
5 |
|
6 |
|
7 | var hooker = require('hooker');
|
8 | function stdoutEqual(test, callback, expected) {
|
9 | var actual = '';
|
10 |
|
11 | hooker.hook(process.stdout, 'write', {
|
12 |
|
13 | pre: function(result) {
|
14 |
|
15 | actual += result;
|
16 |
|
17 | return hooker.preempt();
|
18 | },
|
19 | });
|
20 |
|
21 | callback();
|
22 |
|
23 | stdoutUnmute();
|
24 |
|
25 |
|
26 | test.equal(actual, expected);
|
27 | }
|
28 |
|
29 |
|
30 | function stdoutMute() {
|
31 | hooker.hook(process.stdout, 'write', {
|
32 | pre: function() {
|
33 | return hooker.preempt();
|
34 | },
|
35 | });
|
36 | }
|
37 |
|
38 |
|
39 | function stdoutUnmute() {
|
40 | hooker.unhook(process.stdout, 'write');
|
41 | }
|
42 |
|
43 |
|
44 | function repeat(str, n, separator) {
|
45 | var result = str;
|
46 | for (var i = 1; i < n; i++) {
|
47 | result += (separator || '') + str;
|
48 | }
|
49 | return result;
|
50 | }
|
51 |
|
52 | var fooBuffer = new Buffer('foo');
|
53 |
|
54 | exports['Log instance'] = {
|
55 | setUp: function(done) {
|
56 | this.grunt = {fail: {errorcount: 0}};
|
57 | done();
|
58 | },
|
59 | 'write': function(test) {
|
60 | test.expect(4);
|
61 | var log = new Log();
|
62 |
|
63 | stdoutEqual(test, function() { log.write(''); }, '');
|
64 | stdoutEqual(test, function() { log.write('foo'); }, 'foo');
|
65 | stdoutEqual(test, function() { log.write('%s', 'foo'); }, 'foo');
|
66 | stdoutEqual(test, function() { log.write(fooBuffer); }, 'foo');
|
67 |
|
68 | test.done();
|
69 | },
|
70 | 'writeln': function(test) {
|
71 | test.expect(4);
|
72 | var log = new Log();
|
73 |
|
74 | stdoutEqual(test, function() { log.writeln(); }, '\n');
|
75 | stdoutEqual(test, function() { log.writeln('foo'); }, 'foo\n');
|
76 | stdoutEqual(test, function() { log.writeln('%s', 'foo'); }, 'foo\n');
|
77 | stdoutEqual(test, function() { log.writeln(fooBuffer); }, 'foo\n');
|
78 |
|
79 | test.done();
|
80 | },
|
81 | 'warn': function(test) {
|
82 | test.expect(5);
|
83 | var log = new Log({grunt: this.grunt});
|
84 |
|
85 | stdoutEqual(test, function() { log.warn(); }, 'ERROR'.red + '\n');
|
86 | stdoutEqual(test, function() { log.warn('foo'); }, '>> '.red + 'foo\n');
|
87 | stdoutEqual(test, function() { log.warn('%s', 'foo'); }, '>> '.red + 'foo\n');
|
88 | stdoutEqual(test, function() { log.warn(fooBuffer); }, '>> '.red + 'foo\n');
|
89 | test.equal(this.grunt.fail.errorcount, 0);
|
90 |
|
91 | test.done();
|
92 | },
|
93 | 'error': function(test) {
|
94 | test.expect(5);
|
95 | var log = new Log({grunt: this.grunt});
|
96 |
|
97 | stdoutEqual(test, function() { log.error(); }, 'ERROR'.red + '\n');
|
98 | stdoutEqual(test, function() { log.error('foo'); }, '>> '.red + 'foo\n');
|
99 | stdoutEqual(test, function() { log.error('%s', 'foo'); }, '>> '.red + 'foo\n');
|
100 | stdoutEqual(test, function() { log.error(fooBuffer); }, '>> '.red + 'foo\n');
|
101 | test.equal(this.grunt.fail.errorcount, 4);
|
102 |
|
103 | test.done();
|
104 | },
|
105 | 'ok': function(test) {
|
106 | test.expect(4);
|
107 | var log = new Log({grunt: this.grunt});
|
108 |
|
109 | stdoutEqual(test, function() { log.ok(); }, 'OK'.green + '\n');
|
110 | stdoutEqual(test, function() { log.ok('foo'); }, '>> '.green + 'foo\n');
|
111 | stdoutEqual(test, function() { log.ok('%s', 'foo'); }, '>> '.green + 'foo\n');
|
112 | stdoutEqual(test, function() { log.ok(fooBuffer); }, '>> '.green + 'foo\n');
|
113 |
|
114 | test.done();
|
115 | },
|
116 | 'errorlns': function(test) {
|
117 | test.expect(2);
|
118 | var log = new Log({grunt: this.grunt});
|
119 |
|
120 | stdoutEqual(test, function() {
|
121 | log.errorlns(repeat('foo', 30, ' '));
|
122 | }, '>> '.red + repeat('foo', 19, ' ') +
|
123 | '\n>> '.red + repeat('foo', 11, ' ') + '\n');
|
124 | test.equal(this.grunt.fail.errorcount, 1);
|
125 |
|
126 | test.done();
|
127 | },
|
128 | 'oklns': function(test) {
|
129 | test.expect(1);
|
130 | var log = new Log();
|
131 |
|
132 | stdoutEqual(test, function() {
|
133 | log.oklns(repeat('foo', 30, ' '));
|
134 | }, '>> '.green + repeat('foo', 19, ' ') +
|
135 | '\n>> '.green + repeat('foo', 11, ' ') + '\n');
|
136 |
|
137 | test.done();
|
138 | },
|
139 | 'success': function(test) {
|
140 | test.expect(4);
|
141 | var log = new Log();
|
142 |
|
143 | stdoutEqual(test, function() { log.success(); }, ''.green + '\n');
|
144 | stdoutEqual(test, function() { log.success('foo'); }, 'foo'.green + '\n');
|
145 | stdoutEqual(test, function() { log.success('%s', 'foo'); }, 'foo'.green + '\n');
|
146 | stdoutEqual(test, function() { log.success(fooBuffer); }, 'foo'.green + '\n');
|
147 |
|
148 | test.done();
|
149 | },
|
150 | 'fail': function(test) {
|
151 | test.expect(4);
|
152 | var log = new Log();
|
153 |
|
154 | stdoutEqual(test, function() { log.fail(); }, ''.red + '\n');
|
155 | stdoutEqual(test, function() { log.fail('foo'); }, 'foo'.red + '\n');
|
156 | stdoutEqual(test, function() { log.fail('%s', 'foo'); }, 'foo'.red + '\n');
|
157 | stdoutEqual(test, function() { log.fail(fooBuffer); }, 'foo'.red + '\n');
|
158 |
|
159 | test.done();
|
160 | },
|
161 | 'header': function(test) {
|
162 | test.expect(5);
|
163 | var log = new Log();
|
164 |
|
165 | stdoutEqual(test, function() { log.header(); }, ''.underline + '\n');
|
166 | stdoutEqual(test, function() { log.header(); }, '\n' + ''.underline + '\n');
|
167 | stdoutEqual(test, function() { log.header('foo'); }, '\n' + 'foo'.underline + '\n');
|
168 | stdoutEqual(test, function() { log.header('%s', 'foo'); }, '\n' + 'foo'.underline + '\n');
|
169 | stdoutEqual(test, function() { log.header(fooBuffer); }, '\n' + 'foo'.underline + '\n');
|
170 |
|
171 | test.done();
|
172 | },
|
173 | 'subhead': function(test) {
|
174 | test.expect(5);
|
175 | var log = new Log();
|
176 |
|
177 | stdoutEqual(test, function() { log.subhead(); }, ''.bold + '\n');
|
178 | stdoutEqual(test, function() { log.subhead(); }, '\n' + ''.bold + '\n');
|
179 | stdoutEqual(test, function() { log.subhead('foo'); }, '\n' + 'foo'.bold + '\n');
|
180 | stdoutEqual(test, function() { log.subhead('%s', 'foo'); }, '\n' + 'foo'.bold + '\n');
|
181 | stdoutEqual(test, function() { log.subhead(fooBuffer); }, '\n' + 'foo'.bold + '\n');
|
182 |
|
183 | test.done();
|
184 | },
|
185 | 'writetableln': function(test) {
|
186 | test.expect(1);
|
187 | var log = new Log();
|
188 |
|
189 | stdoutEqual(test, function() {
|
190 | log.writetableln([10], [repeat('foo', 10)]);
|
191 | }, 'foofoofoof\noofoofoofo\nofoofoofoo\n');
|
192 |
|
193 | test.done();
|
194 | },
|
195 | 'writelns': function(test) {
|
196 | test.expect(1);
|
197 | var log = new Log();
|
198 |
|
199 | stdoutEqual(test, function() {
|
200 | log.writelns(repeat('foo', 30, ' '));
|
201 | }, repeat('foo', 20, ' ') + '\n' +
|
202 | repeat('foo', 10, ' ') + '\n');
|
203 |
|
204 | test.done();
|
205 | },
|
206 | 'writeflags': function(test) {
|
207 | test.expect(3);
|
208 | var log = new Log();
|
209 |
|
210 | stdoutEqual(test, function() {
|
211 | log.writeflags(['a', 'b']);
|
212 | }, 'Flags: ' + 'a'.cyan + ', ' + 'b'.cyan + '\n');
|
213 | stdoutEqual(test, function() {
|
214 | log.writeflags(['a', 'b'], 'Prefix');
|
215 | }, 'Prefix: ' + 'a'.cyan + ', ' + 'b'.cyan + '\n');
|
216 | stdoutEqual(test, function() {
|
217 | log.writeflags({a: true, b: false, c: 0, d: null}, 'Prefix');
|
218 | }, 'Prefix: ' + 'a'.cyan + ', ' + 'b=false'.cyan + ', ' + 'c=0'.cyan + ', ' + 'd=null'.cyan + '\n');
|
219 |
|
220 | test.done();
|
221 | },
|
222 | 'always': function(test) {
|
223 | test.expect(3);
|
224 | var log = new Log();
|
225 |
|
226 | test.strictEqual(log.always, log);
|
227 | test.strictEqual(log.verbose.always, log);
|
228 | test.strictEqual(log.notverbose.always, log);
|
229 |
|
230 | test.done();
|
231 | },
|
232 | 'or': function(test) {
|
233 | test.expect(2);
|
234 | var log = new Log();
|
235 |
|
236 | test.strictEqual(log.verbose.or, log.notverbose);
|
237 | test.strictEqual(log.notverbose.or, log.verbose);
|
238 |
|
239 | test.done();
|
240 | },
|
241 | 'hasLogged': function(test) {
|
242 |
|
243 | test.expect(24);
|
244 | var log = new Log();
|
245 | test.equal(log.hasLogged, false);
|
246 | test.equal(log.verbose.hasLogged, false);
|
247 | test.equal(log.notverbose.hasLogged, false);
|
248 | log.write('');
|
249 | test.equal(log.hasLogged, true);
|
250 | test.equal(log.verbose.hasLogged, true);
|
251 | test.equal(log.notverbose.hasLogged, true);
|
252 |
|
253 | log = new Log({verbose: true});
|
254 | log.verbose.write('');
|
255 | test.equal(log.hasLogged, true);
|
256 | test.equal(log.verbose.hasLogged, true);
|
257 | test.equal(log.notverbose.hasLogged, true);
|
258 |
|
259 | log = new Log();
|
260 | log.notverbose.write('');
|
261 | test.equal(log.hasLogged, true);
|
262 | test.equal(log.verbose.hasLogged, true);
|
263 | test.equal(log.notverbose.hasLogged, true);
|
264 |
|
265 | stdoutMute();
|
266 | log = new Log({debug: true});
|
267 | log.debug('');
|
268 | test.equal(log.hasLogged, true);
|
269 | test.equal(log.verbose.hasLogged, true);
|
270 | test.equal(log.notverbose.hasLogged, true);
|
271 | stdoutUnmute();
|
272 |
|
273 |
|
274 | log = new Log();
|
275 | log.verbose.write('');
|
276 | test.equal(log.hasLogged, false);
|
277 | test.equal(log.verbose.hasLogged, false);
|
278 | test.equal(log.notverbose.hasLogged, false);
|
279 |
|
280 | log = new Log({verbose: true});
|
281 | log.notverbose.write('');
|
282 | test.equal(log.hasLogged, false);
|
283 | test.equal(log.verbose.hasLogged, false);
|
284 | test.equal(log.notverbose.hasLogged, false);
|
285 |
|
286 |
|
287 | log = new Log();
|
288 | log.debug('');
|
289 | test.equal(log.hasLogged, false);
|
290 | test.equal(log.verbose.hasLogged, false);
|
291 | test.equal(log.notverbose.hasLogged, false);
|
292 |
|
293 | test.done();
|
294 | },
|
295 | 'muted': function(test) {
|
296 | test.expect(30);
|
297 | var log = new Log();
|
298 |
|
299 | test.equal(log.muted, false);
|
300 | test.equal(log.verbose.muted, false);
|
301 | test.equal(log.notverbose.muted, false);
|
302 | test.equal(log.options.muted, false);
|
303 | test.equal(log.verbose.options.muted, false);
|
304 | test.equal(log.notverbose.options.muted, false);
|
305 |
|
306 | log.muted = true;
|
307 | test.equal(log.muted, true);
|
308 | test.equal(log.verbose.muted, true);
|
309 | test.equal(log.notverbose.muted, true);
|
310 | test.equal(log.options.muted, true);
|
311 | test.equal(log.verbose.options.muted, true);
|
312 | test.equal(log.notverbose.options.muted, true);
|
313 |
|
314 | log.muted = false;
|
315 | test.equal(log.muted, false);
|
316 | test.equal(log.verbose.muted, false);
|
317 | test.equal(log.notverbose.muted, false);
|
318 | test.equal(log.options.muted, false);
|
319 | test.equal(log.verbose.options.muted, false);
|
320 | test.equal(log.notverbose.options.muted, false);
|
321 |
|
322 | log.options.muted = true;
|
323 | test.equal(log.muted, true);
|
324 | test.equal(log.verbose.muted, true);
|
325 | test.equal(log.notverbose.muted, true);
|
326 | test.equal(log.options.muted, true);
|
327 | test.equal(log.verbose.options.muted, true);
|
328 | test.equal(log.notverbose.options.muted, true);
|
329 |
|
330 | log.options.muted = false;
|
331 | test.equal(log.muted, false);
|
332 | test.equal(log.verbose.muted, false);
|
333 | test.equal(log.notverbose.muted, false);
|
334 | test.equal(log.options.muted, false);
|
335 | test.equal(log.verbose.options.muted, false);
|
336 | test.equal(log.notverbose.options.muted, false);
|
337 |
|
338 | test.done();
|
339 | },
|
340 | 'verbose': function(test) {
|
341 | test.expect(15);
|
342 | var log = new Log();
|
343 | log.muted = true;
|
344 |
|
345 |
|
346 | test.strictEqual(log.verbose.write(''), log.verbose);
|
347 | test.strictEqual(log.verbose.writeln(''), log.verbose);
|
348 | test.strictEqual(log.verbose.warn(''), log.verbose);
|
349 | test.strictEqual(log.verbose.error(''), log.verbose);
|
350 | test.strictEqual(log.verbose.ok(''), log.verbose);
|
351 | test.strictEqual(log.verbose.errorlns(''), log.verbose);
|
352 | test.strictEqual(log.verbose.oklns(''), log.verbose);
|
353 | test.strictEqual(log.verbose.success(''), log.verbose);
|
354 | test.strictEqual(log.verbose.fail(''), log.verbose);
|
355 | test.strictEqual(log.verbose.header(''), log.verbose);
|
356 | test.strictEqual(log.verbose.subhead(''), log.verbose);
|
357 | test.strictEqual(log.verbose.debug(''), log.verbose);
|
358 | test.strictEqual(log.verbose.writetableln([]), log.verbose);
|
359 | test.strictEqual(log.verbose.writelns(''), log.verbose);
|
360 | test.strictEqual(log.verbose.writeflags([]), log.verbose);
|
361 |
|
362 | test.done();
|
363 | },
|
364 | 'notverbose': function(test) {
|
365 | test.expect(15);
|
366 | var log = new Log();
|
367 | log.muted = true;
|
368 |
|
369 |
|
370 | test.strictEqual(log.notverbose.write(''), log.notverbose);
|
371 | test.strictEqual(log.notverbose.writeln(''), log.notverbose);
|
372 | test.strictEqual(log.notverbose.warn(''), log.notverbose);
|
373 | test.strictEqual(log.notverbose.error(''), log.notverbose);
|
374 | test.strictEqual(log.notverbose.ok(''), log.notverbose);
|
375 | test.strictEqual(log.notverbose.errorlns(''), log.notverbose);
|
376 | test.strictEqual(log.notverbose.oklns(''), log.notverbose);
|
377 | test.strictEqual(log.notverbose.success(''), log.notverbose);
|
378 | test.strictEqual(log.notverbose.fail(''), log.notverbose);
|
379 | test.strictEqual(log.notverbose.header(''), log.notverbose);
|
380 | test.strictEqual(log.notverbose.subhead(''), log.notverbose);
|
381 | test.strictEqual(log.notverbose.debug(''), log.notverbose);
|
382 | test.strictEqual(log.notverbose.writetableln([]), log.notverbose);
|
383 | test.strictEqual(log.notverbose.writelns(''), log.notverbose);
|
384 | test.strictEqual(log.notverbose.writeflags([]), log.notverbose);
|
385 |
|
386 | test.done();
|
387 | },
|
388 | 'options.debug = true': function(test) {
|
389 | test.expect(4);
|
390 | var log = new Log({debug: true});
|
391 |
|
392 | stdoutEqual(test, function() { log.debug(); }, '[D] ' + ''.magenta + '\n');
|
393 | stdoutEqual(test, function() { log.debug('foo'); }, '[D] ' + 'foo'.magenta + '\n');
|
394 | stdoutEqual(test, function() { log.debug('%s', 'foo'); }, '[D] ' + 'foo'.magenta + '\n');
|
395 | stdoutEqual(test, function() { log.debug(fooBuffer); }, '[D] ' + 'foo'.magenta + '\n');
|
396 |
|
397 | test.done();
|
398 | },
|
399 | 'options.verbose = false': function(test) {
|
400 | test.expect(7);
|
401 | var log = new Log({verbose: false});
|
402 |
|
403 | stdoutEqual(test, function() { log.notverbose.write('foo'); }, 'foo');
|
404 | stdoutEqual(test, function() { log.notverbose.write('%s', 'foo'); }, 'foo');
|
405 | stdoutEqual(test, function() { log.notverbose.write(fooBuffer); }, 'foo');
|
406 | stdoutEqual(test, function() { log.verbose.write('foo'); }, '');
|
407 | stdoutEqual(test, function() { log.verbose.write('%s', 'foo'); }, '');
|
408 | stdoutEqual(test, function() { log.verbose.write(fooBuffer); }, '');
|
409 | stdoutEqual(test, function() { log.verbose.write('a').or.write('b'); }, 'b');
|
410 |
|
411 | test.done();
|
412 | },
|
413 | 'options.verbose = true': function(test) {
|
414 | test.expect(7);
|
415 | var log = new Log({verbose: true});
|
416 |
|
417 | stdoutEqual(test, function() { log.verbose.write('foo'); }, 'foo');
|
418 | stdoutEqual(test, function() { log.verbose.write('%s', 'foo'); }, 'foo');
|
419 | stdoutEqual(test, function() { log.verbose.write(fooBuffer); }, 'foo');
|
420 | stdoutEqual(test, function() { log.notverbose.write('foo'); }, '');
|
421 | stdoutEqual(test, function() { log.notverbose.write('%s', 'foo'); }, '');
|
422 | stdoutEqual(test, function() { log.notverbose.write(fooBuffer); }, '');
|
423 | stdoutEqual(test, function() { log.notverbose.write('a').or.write('b'); }, 'b');
|
424 |
|
425 | test.done();
|
426 | },
|
427 | 'options.debug = false': function(test) {
|
428 | test.expect(1);
|
429 | var log = new Log({debug: false});
|
430 |
|
431 | stdoutEqual(test, function() { log.debug('foo'); }, '');
|
432 |
|
433 | test.done();
|
434 | },
|
435 | 'options.color = true': function(test) {
|
436 | test.expect(1);
|
437 | var log = new Log({color: true});
|
438 |
|
439 | stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foo'.blue + 'bar'.underline);
|
440 |
|
441 | test.done();
|
442 | },
|
443 | 'options.color = false': function(test) {
|
444 | test.expect(1);
|
445 | var log = new Log({color: false});
|
446 |
|
447 | stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foobar');
|
448 |
|
449 | test.done();
|
450 | },
|
451 | 'perma-bind this when passing grunt in (backcompat)': function(test) {
|
452 | test.expect(43);
|
453 | var log = new Log({grunt: this.grunt});
|
454 | stdoutMute();
|
455 | [
|
456 | 'write',
|
457 | 'writeln',
|
458 | 'warn',
|
459 | 'error',
|
460 | 'ok',
|
461 | 'errorlns',
|
462 | 'oklns',
|
463 | 'success',
|
464 | 'fail',
|
465 | 'header',
|
466 | 'subhead',
|
467 | 'debug',
|
468 | ].forEach(function(method) {
|
469 | var fn = log[method];
|
470 | var verboseFn = log.verbose[method];
|
471 | var notVerboseFn = log.notverbose[method];
|
472 | test.equal(fn(), log, 'Should return log if invoked in a way where this is not log.');
|
473 | test.equal(verboseFn(), log.verbose, 'Should return log.verbose if invoked in a way where this is not log.');
|
474 | test.equal(notVerboseFn(), log.notverbose, 'Should return log.notverbose if invoked in a way where this is not log.');
|
475 | });
|
476 |
|
477 | test.doesNotThrow(function() { var fn = log.writetableln; fn([]); }, 'Should not throw if invoked in a way where this is not log.');
|
478 | test.doesNotThrow(function() { var fn = log.writelns; fn([]); }, 'Should not throw if invoked in a way where this is not log.');
|
479 | test.doesNotThrow(function() { var fn = log.writeflags; fn([]); }, 'Should not throw if invoked in a way where this is not log.');
|
480 | test.doesNotThrow(function() { var fn = log.wordlist; fn([]); }, 'Should not throw if invoked in a way where this is not log.');
|
481 | test.doesNotThrow(function() { var fn = log.uncolor; fn(''); }, 'Should not throw if invoked in a way where this is not log.');
|
482 | test.doesNotThrow(function() { var fn = log.wraptext; fn(1,''); }, 'Should not throw if invoked in a way where this is not log.');
|
483 | test.doesNotThrow(function() { var fn = log.table; fn([],''); }, 'Should not throw if invoked in a way where this is not log.');
|
484 | stdoutUnmute();
|
485 |
|
486 | test.done();
|
487 | },
|
488 | };
|
489 |
|
490 | exports['Helpers'] = {
|
491 | 'uncolor': function(test) {
|
492 | test.expect(2);
|
493 | var log = new Log();
|
494 | test.ok(log.uncolor);
|
495 | test.strictEqual(log.uncolor, legacyLog.uncolor);
|
496 | test.done();
|
497 | },
|
498 | 'wordlist': function(test) {
|
499 | test.expect(2);
|
500 | var log = new Log();
|
501 | test.ok(log.wordlist);
|
502 | test.strictEqual(log.wordlist, legacyLog.wordlist);
|
503 | test.done();
|
504 | },
|
505 | 'wraptext': function(test) {
|
506 | test.expect(2);
|
507 | var log = new Log();
|
508 | test.ok(log.wraptext);
|
509 | test.strictEqual(log.wraptext, legacyLog.wraptext);
|
510 | test.done();
|
511 | },
|
512 | 'table': function(test) {
|
513 | test.expect(2);
|
514 | var log = new Log();
|
515 | test.ok(log.table);
|
516 | test.strictEqual(log.table, legacyLog.table);
|
517 | test.done();
|
518 | },
|
519 | };
|