3 var legacyLog = require('../');
4 var Log = legacyLog.Log;
6 // Helper for testing stdout
7 var hooker = require('hooker');
8 function stdoutEqual(test, callback, expected) {
10 // Hook process.stdout.write
11 hooker.hook(process.stdout, 'write', {
12 // This gets executed before the original process.stdout.write.
13 pre: function(result) {
14 // Concatenate uncolored result onto actual.
16 // Prevent the original process.stdout.write from executing.
17 return hooker.preempt();
20 // Execute the logging code to be tested.
22 // Restore process.stdout.write to its original value.
24 // Actually test the actually-logged stdout string to the expected value.
25 // test.equal(legacyLog.uncolor(actual), expected);
26 test.equal(actual, expected);
29 // Outright mute stdout.
30 function stdoutMute() {
31 hooker.hook(process.stdout, 'write', {
33 return hooker.preempt();
39 function stdoutUnmute() {
40 hooker.unhook(process.stdout, 'write');
43 // Helper function: repeat('a', 3) -> 'aaa', repeat('a', 3, '-') -> 'a-a-a'
44 function repeat(str, n, separator) {
46 for (var i = 1; i < n; i++) {
47 result += (separator || '') + str;
52 var fooBuffer = new Buffer('foo');
54 exports['Log instance'] = {
55 setUp: function(done) {
56 this.grunt = {fail: {errorcount: 0}};
59 'write': function(test) {
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');
70 'writeln': function(test) {
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');
81 'warn': function(test) {
83 var log = new Log({grunt: this.grunt});
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);
93 'error': function(test) {
95 var log = new Log({grunt: this.grunt});
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);
105 'ok': function(test) {
107 var log = new Log({grunt: this.grunt});
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');
116 'errorlns': function(test) {
118 var log = new Log({grunt: this.grunt});
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);
128 'oklns': function(test) {
132 stdoutEqual(test, function() {
133 log.oklns(repeat('foo', 30, ' '));
134 }, '>> '.green + repeat('foo', 19, ' ') +
135 '\n>> '.green + repeat('foo', 11, ' ') + '\n');
139 'success': function(test) {
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');
150 'fail': function(test) {
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');
161 'header': function(test) {
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');
173 'subhead': function(test) {
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');
185 'writetableln': function(test) {
189 stdoutEqual(test, function() {
190 log.writetableln([10], [repeat('foo', 10)]);
191 }, 'foofoofoof\noofoofoofo\nofoofoofoo\n');
195 'writelns': function(test) {
199 stdoutEqual(test, function() {
200 log.writelns(repeat('foo', 30, ' '));
201 }, repeat('foo', 20, ' ') + '\n' +
202 repeat('foo', 10, ' ') + '\n');
206 'writeflags': function(test) {
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');
222 'always': function(test) {
226 test.strictEqual(log.always, log);
227 test.strictEqual(log.verbose.always, log);
228 test.strictEqual(log.notverbose.always, log);
232 'or': function(test) {
236 test.strictEqual(log.verbose.or, log.notverbose);
237 test.strictEqual(log.notverbose.or, log.verbose);
241 'hasLogged': function(test) {
242 // Should only be true if output has been written!
245 test.equal(log.hasLogged, false);
246 test.equal(log.verbose.hasLogged, false);
247 test.equal(log.notverbose.hasLogged, false);
249 test.equal(log.hasLogged, true);
250 test.equal(log.verbose.hasLogged, true);
251 test.equal(log.notverbose.hasLogged, true);
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);
260 log.notverbose.write('');
261 test.equal(log.hasLogged, true);
262 test.equal(log.verbose.hasLogged, true);
263 test.equal(log.notverbose.hasLogged, true);
266 log = new Log({debug: true});
268 test.equal(log.hasLogged, true);
269 test.equal(log.verbose.hasLogged, true);
270 test.equal(log.notverbose.hasLogged, true);
273 // The following should be false since there's a verbose mismatch!
275 log.verbose.write('');
276 test.equal(log.hasLogged, false);
277 test.equal(log.verbose.hasLogged, false);
278 test.equal(log.notverbose.hasLogged, false);
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);
286 // The following should be false since there's a debug mismatch!
289 test.equal(log.hasLogged, false);
290 test.equal(log.verbose.hasLogged, false);
291 test.equal(log.notverbose.hasLogged, false);
295 'muted': function(test) {
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);
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);
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);
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);
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);
340 'verbose': function(test) {
345 // Test verbose methods to make sure they always return the verbose object.
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);
364 'notverbose': function(test) {
369 // Test notverbose methods to make sure they always return the notverbose object.
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);
388 'options.debug = true': function(test) {
390 var log = new Log({debug: true});
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');
399 'options.verbose = false': function(test) {
401 var log = new Log({verbose: false});
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');
413 'options.verbose = true': function(test) {
415 var log = new Log({verbose: true});
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');
427 'options.debug = false': function(test) {
429 var log = new Log({debug: false});
431 stdoutEqual(test, function() { log.debug('foo'); }, '');
435 'options.color = true': function(test) {
437 var log = new Log({color: true});
439 stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foo'.blue + 'bar'.underline);
443 'options.color = false': function(test) {
445 var log = new Log({color: false});
447 stdoutEqual(test, function() { log.write('foo'.blue + 'bar'.underline); }, 'foobar');
451 'perma-bind this when passing grunt in (backcompat)': function(test) {
453 var log = new Log({grunt: this.grunt});
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.');
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.');
490 exports['Helpers'] = {
491 'uncolor': function(test) {
494 test.ok(log.uncolor);
495 test.strictEqual(log.uncolor, legacyLog.uncolor);
498 'wordlist': function(test) {
501 test.ok(log.wordlist);
502 test.strictEqual(log.wordlist, legacyLog.wordlist);
505 'wraptext': function(test) {
508 test.ok(log.wraptext);
509 test.strictEqual(log.wraptext, legacyLog.wraptext);
512 'table': function(test) {
516 test.strictEqual(log.table, legacyLog.table);