From b356dec3187d78412653da24f2c9be9bb5c3b2cc Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 25 May 2013 14:00:06 +1000 Subject: [PATCH] Getting my lint on (via bob) --- .bob.json | 12 + .gitignore | 1 + .jshintrc | 15 + lib/appenders/console.js | 23 +- test/streams/DateRollingFileStream-test.js | 334 ++++++++++++--------- test/streams/rollingFileStream-test.js | 225 ++++++++------ 6 files changed, 353 insertions(+), 257 deletions(-) create mode 100644 .bob.json create mode 100644 .jshintrc diff --git a/.bob.json b/.bob.json new file mode 100644 index 0000000..c8c1e02 --- /dev/null +++ b/.bob.json @@ -0,0 +1,12 @@ +{ + "build": "clean lint coverage test", + "lint": { + "type": "jshint" + }, + "coverage": { + "type": "vows" + }, + "test": { + "type": "vows" + } +} diff --git a/.gitignore b/.gitignore index c9a0c8b..707a241 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build node_modules +.bob/ diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..fe10cba --- /dev/null +++ b/.jshintrc @@ -0,0 +1,15 @@ +{ + "node": true, + "laxcomma": true, + "indent": 2, + "globalstrict": true, + "maxparams": 5, + "maxdepth": 3, + "maxstatements": 20, + "maxcomplexity": 5, + "maxlen": 100, + "globals": { + "describe": true, + "it": true + } +} diff --git a/lib/appenders/console.js b/lib/appenders/console.js index aacdd73..cb7f42a 100644 --- a/lib/appenders/console.js +++ b/lib/appenders/console.js @@ -1,19 +1,20 @@ -var layouts = require('../layouts'), - consoleLog = console.log; +"use strict"; +var layouts = require('../layouts') +, consoleLog = console.log; function consoleAppender (layout) { - layout = layout || layouts.colouredLayout; - return function(loggingEvent) { - consoleLog(layout(loggingEvent)); - }; + layout = layout || layouts.colouredLayout; + return function(loggingEvent) { + consoleLog(layout(loggingEvent)); + }; } function configure(config) { - var layout; - if (config.layout) { - layout = layouts.layout(config.layout.type, config.layout); - } - return consoleAppender(layout); + var layout; + if (config.layout) { + layout = layouts.layout(config.layout.type, config.layout); + } + return consoleAppender(layout); } exports.appender = consoleAppender; diff --git a/test/streams/DateRollingFileStream-test.js b/test/streams/DateRollingFileStream-test.js index a314170..33f014b 100644 --- a/test/streams/DateRollingFileStream-test.js +++ b/test/streams/DateRollingFileStream-test.js @@ -1,3 +1,4 @@ +"use strict"; var vows = require('vows') , assert = require('assert') , fs = require('fs') @@ -11,177 +12,216 @@ if (semver.satisfies(process.version, '>=0.10.0')) { } else { streams = require('readable-stream'); } -DateRollingFileStream = require('../../lib/streams').DateRollingFileStream +DateRollingFileStream = require('../../lib/streams').DateRollingFileStream; function cleanUp(filename) { - return function() { - fs.unlink(filename); - }; + return function() { + fs.unlink(filename); + }; } function now() { - return testTime.getTime(); + return testTime.getTime(); } vows.describe('DateRollingFileStream').addBatch({ - 'arguments': { - topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-1', 'yyyy-mm-dd.hh'), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'), - - 'should take a filename and a pattern and return a WritableStream': function(stream) { - assert.equal(stream.filename, __dirname + '/test-date-rolling-file-stream-1'); - assert.equal(stream.pattern, 'yyyy-mm-dd.hh'); - assert.instanceOf(stream, streams.Writable); - }, - 'with default settings for the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, 420); - assert.equal(stream.theStream.flags, 'a'); - //encoding is not available on the underlying stream - //assert.equal(stream.encoding, 'utf8'); - } + 'arguments': { + topic: new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-1', + 'yyyy-mm-dd.hh' + ), + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'), + + 'should take a filename and a pattern and return a WritableStream': function(stream) { + assert.equal(stream.filename, __dirname + '/test-date-rolling-file-stream-1'); + assert.equal(stream.pattern, 'yyyy-mm-dd.hh'); + assert.instanceOf(stream, streams.Writable); }, - - 'default arguments': { - topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-2'), - - 'pattern should be .yyyy-MM-dd': function(stream) { - assert.equal(stream.pattern, '.yyyy-MM-dd'); - } + 'with default settings for the underlying stream': function(stream) { + assert.equal(stream.theStream.mode, 420); + assert.equal(stream.theStream.flags, 'a'); + //encoding is not available on the underlying stream + //assert.equal(stream.encoding, 'utf8'); + } + }, + + 'default arguments': { + topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'), + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-2'), + + 'pattern should be .yyyy-MM-dd': function(stream) { + assert.equal(stream.pattern, '.yyyy-MM-dd'); + } + }, + + 'with stream arguments': { + topic: new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-3', + 'yyyy-MM-dd', + { mode: parseInt('0666', 8) } + ), + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'), + + 'should pass them to the underlying stream': function(stream) { + assert.equal(stream.theStream.mode, parseInt('0666', 8)); + } + }, + + 'with stream arguments but no pattern': { + topic: new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-4', + { mode: parseInt('0666', 8) } + ), + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'), + + 'should pass them to the underlying stream': function(stream) { + assert.equal(stream.theStream.mode, parseInt('0666', 8)); }, - - 'with stream arguments': { - topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-3', 'yyyy-MM-dd', { mode: 0666 }), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'), - - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, 0666); - } + 'should use default pattern': function(stream) { + assert.equal(stream.pattern, '.yyyy-MM-dd'); + } + }, + + 'with a pattern of .yyyy-MM-dd': { + topic: function() { + var that = this, + stream = new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd', + null, + now + ); + stream.write("First message\n", 'utf8', function() { + that.callback(null, stream); + }); + }, + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'), + + 'should create a file with the base name': { + topic: function(stream) { + fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback); + }, + 'file should contain first message': function(result) { + assert.equal(result.toString(), "First message\n"); + } }, - 'with stream arguments but no pattern': { - topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-4', { mode: 0666 }), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'), + 'when the day changes': { + topic: function(stream) { + testTime = new Date(2012, 8, 13, 0, 10, 12); + stream.write("Second message\n", 'utf8', this.callback); + }, + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5.2012-09-12'), - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, 0666); + + 'the number of files': { + topic: function() { + fs.readdir(__dirname, this.callback); }, - 'should use default pattern': function(stream) { - assert.equal(stream.pattern, '.yyyy-MM-dd'); + 'should be two': function(files) { + assert.equal( + files.filter( + function(file) { + return file.indexOf('test-date-rolling-file-stream-5') > -1; + } + ).length, + 2 + ); } - }, - - 'with a pattern of .yyyy-MM-dd': { + }, + + 'the file without a date': { topic: function() { - var that = this, - stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd', null, now); - stream.write("First message\n", 'utf8', function() { - that.callback(null, stream); - }); + fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback); }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'), - - 'should create a file with the base name': { - topic: function(stream) { - fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback); - }, - 'file should contain first message': function(result) { - assert.equal(result.toString(), "First message\n"); - } + 'should contain the second message': function(contents) { + assert.equal(contents.toString(), "Second message\n"); + } + }, + + 'the file with the date': { + topic: function() { + fs.readFile(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', this.callback); }, - - 'when the day changes': { - topic: function(stream) { - testTime = new Date(2012, 8, 13, 0, 10, 12); - stream.write("Second message\n", 'utf8', this.callback); - }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5.2012-09-12'), - - - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be two': function(files) { - assert.equal(files.filter(function(file) { return file.indexOf('test-date-rolling-file-stream-5') > -1; }).length, 2); - } - }, - - 'the file without a date': { - topic: function() { - fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback); - }, - 'should contain the second message': function(contents) { - assert.equal(contents.toString(), "Second message\n"); - } - }, - - 'the file with the date': { - topic: function() { - fs.readFile(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', this.callback); - }, - 'should contain the first message': function(contents) { - assert.equal(contents.toString(), "First message\n"); - } - } + 'should contain the first message': function(contents) { + assert.equal(contents.toString(), "First message\n"); } + } + } + }, + + 'with alwaysIncludePattern': { + topic: function() { + var that = this, + testTime = new Date(2012, 8, 12, 0, 10, 12), + stream = new DateRollingFileStream( + __dirname + '/test-date-rolling-file-stream-pattern', + '.yyyy-MM-dd', + {alwaysIncludePattern: true}, + now + ); + stream.write("First message\n", 'utf8', function() { + that.callback(null, stream); + }); }, - - 'with alwaysIncludePattern': { + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'), + + 'should create a file with the pattern set': { + topic: function(stream) { + fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback); + }, + 'file should contain first message': function(result) { + assert.equal(result.toString(), "First message\n"); + } + }, + + 'when the day changes': { + topic: function(stream) { + testTime = new Date(2012, 8, 13, 0, 10, 12); + stream.write("Second message\n", 'utf8', this.callback); + }, + teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'), + + + 'the number of files': { topic: function() { - var that = this, - testTime = new Date(2012, 8, 12, 0, 10, 12), - stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-pattern', '.yyyy-MM-dd', {alwaysIncludePattern: true}, now); - stream.write("First message\n", 'utf8', function() { - that.callback(null, stream); - }); + fs.readdir(__dirname, this.callback); }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'), - - 'should create a file with the pattern set': { - topic: function(stream) { - fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback); - }, - 'file should contain first message': function(result) { - assert.equal(result.toString(), "First message\n"); - } + 'should be two': function(files) { + assert.equal( + files.filter( + function(file) { + return file.indexOf('test-date-rolling-file-stream-pattern') > -1; + } + ).length, + 2 + ); + } + }, + + 'the file with the later date': { + topic: function() { + fs.readFile( + __dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', + this.callback + ); }, - - 'when the day changes': { - topic: function(stream) { - testTime = new Date(2012, 8, 13, 0, 10, 12); - stream.write("Second message\n", 'utf8', this.callback); - }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'), - - - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be two': function(files) { - assert.equal(files.filter(function(file) { return file.indexOf('test-date-rolling-file-stream-pattern') > -1; }).length, 2); - } - }, - - 'the file with the later date': { - topic: function() { - fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', this.callback); - }, - 'should contain the second message': function(contents) { - assert.equal(contents.toString(), "Second message\n"); - } - }, - - 'the file with the date': { - topic: function() { - fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback); - }, - 'should contain the first message': function(contents) { - assert.equal(contents.toString(), "First message\n"); - } - } + 'should contain the second message': function(contents) { + assert.equal(contents.toString(), "Second message\n"); + } + }, + + 'the file with the date': { + topic: function() { + fs.readFile( + __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', + this.callback + ); + }, + 'should contain the first message': function(contents) { + assert.equal(contents.toString(), "First message\n"); } + } } + } }).exportTo(module); diff --git a/test/streams/rollingFileStream-test.js b/test/streams/rollingFileStream-test.js index 73de936..317719c 100644 --- a/test/streams/rollingFileStream-test.js +++ b/test/streams/rollingFileStream-test.js @@ -1,3 +1,4 @@ +"use strict"; var vows = require('vows') , async = require('async') , assert = require('assert') @@ -15,120 +16,146 @@ if (semver.satisfies(process.version, '>=0.10.0')) { RollingFileStream = require('../../lib/streams').RollingFileStream; function remove(filename) { - try { - fs.unlinkSync(filename); - } catch (e) { - //doesn't really matter if it failed - } + try { + fs.unlinkSync(filename); + } catch (e) { + //doesn't really matter if it failed + } } vows.describe('RollingFileStream').addBatch({ - 'arguments': { - topic: function() { - remove(__dirname + "/test-rolling-file-stream"); - return new RollingFileStream("test-rolling-file-stream", 1024, 5); - }, - 'should take a filename, file size in bytes, number of backups as arguments and return a Writable': function(stream) { - assert.instanceOf(stream, streams.Writable); - assert.equal(stream.filename, "test-rolling-file-stream"); - assert.equal(stream.size, 1024); - assert.equal(stream.backups, 5); - }, - 'with default settings for the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, 420); - assert.equal(stream.theStream.flags, 'a'); - //encoding isn't a property on the underlying stream - //assert.equal(stream.theStream.encoding, 'utf8'); - } + 'arguments': { + topic: function() { + remove(__dirname + "/test-rolling-file-stream"); + return new RollingFileStream("test-rolling-file-stream", 1024, 5); }, - 'with stream arguments': { - topic: function() { - remove(__dirname + '/test-rolling-file-stream'); - return new RollingFileStream('test-rolling-file-stream', 1024, 5, { mode: 0666 }); - }, - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, 0666); - } + 'should take a filename, file size (bytes), no. backups, return Writable': function(stream) { + assert.instanceOf(stream, streams.Writable); + assert.equal(stream.filename, "test-rolling-file-stream"); + assert.equal(stream.size, 1024); + assert.equal(stream.backups, 5); }, - 'without size': { - topic: function() { - try { - new RollingFileStream(__dirname + "/test-rolling-file-stream"); - } catch (e) { - return e; - } - }, - 'should throw an error': function(err) { - assert.instanceOf(err, Error); - } + 'with default settings for the underlying stream': function(stream) { + assert.equal(stream.theStream.mode, 420); + assert.equal(stream.theStream.flags, 'a'); + //encoding isn't a property on the underlying stream + //assert.equal(stream.theStream.encoding, 'utf8'); + } + }, + 'with stream arguments': { + topic: function() { + remove(__dirname + '/test-rolling-file-stream'); + return new RollingFileStream( + 'test-rolling-file-stream', + 1024, + 5, + { mode: parseInt('0666', 8) } + ); }, - 'without number of backups': { - topic: function() { - remove('test-rolling-file-stream'); - return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024); - }, - 'should default to 1 backup': function(stream) { - assert.equal(stream.backups, 1); - } + 'should pass them to the underlying stream': function(stream) { + assert.equal(stream.theStream.mode, parseInt('0666', 8)); + } + }, + 'without size': { + topic: function() { + try { + new RollingFileStream(__dirname + "/test-rolling-file-stream"); + } catch (e) { + return e; + } + }, + 'should throw an error': function(err) { + assert.instanceOf(err, Error); + } + }, + 'without number of backups': { + topic: function() { + remove('test-rolling-file-stream'); + return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024); }, - 'writing less than the file size': { + 'should default to 1 backup': function(stream) { + assert.equal(stream.backups, 1); + } + }, + 'writing less than the file size': { + topic: function() { + remove(__dirname + "/test-rolling-file-stream-write-less"); + var that = this + , stream = new RollingFileStream( + __dirname + "/test-rolling-file-stream-write-less", + 100 + ); + stream.write("cheese", "utf8", function() { + stream.end(); + fs.readFile(__dirname + "/test-rolling-file-stream-write-less", "utf8", that.callback); + }); + }, + 'should write to the file': function(contents) { + assert.equal(contents, "cheese"); + }, + 'the number of files': { topic: function() { - remove(__dirname + "/test-rolling-file-stream-write-less"); - var that = this, stream = new RollingFileStream(__dirname + "/test-rolling-file-stream-write-less", 100); - stream.write("cheese", "utf8", function() { - stream.end(); - fs.readFile(__dirname + "/test-rolling-file-stream-write-less", "utf8", that.callback); - }); + fs.readdir(__dirname, this.callback); }, - 'should write to the file': function(contents) { - assert.equal(contents, "cheese"); - }, - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be one': function(files) { - assert.equal(files.filter(function(file) { return file.indexOf('test-rolling-file-stream-write-less') > -1; }).length, 1); - } + 'should be one': function(files) { + assert.equal( + files.filter( + function(file) { + return file.indexOf('test-rolling-file-stream-write-less') > -1; + } + ).length, + 1 + ); } + } + }, + 'writing more than the file size': { + topic: function() { + remove(__dirname + "/test-rolling-file-stream-write-more"); + remove(__dirname + "/test-rolling-file-stream-write-more.1"); + var that = this + , stream = new RollingFileStream( + __dirname + "/test-rolling-file-stream-write-more", + 45 + ); + async.forEach( + [0, 1, 2, 3, 4, 5, 6], + function(i, cb) { + stream.write(i +".cheese\n", "utf8", cb); + }, + function() { + stream.end(); + that.callback(); + } + ); }, - 'writing more than the file size': { + 'the number of files': { topic: function() { - remove(__dirname + "/test-rolling-file-stream-write-more"); - remove(__dirname + "/test-rolling-file-stream-write-more.1"); - var that = this, stream = new RollingFileStream(__dirname + "/test-rolling-file-stream-write-more", 45); - async.forEach([0, 1, 2, 3, 4, 5, 6], function(i, cb) { - stream.write(i +".cheese\n", "utf8", cb); - }, function() { - stream.end(); - that.callback(); - }); + fs.readdir(__dirname, this.callback); }, - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be two': function(files) { - assert.equal(files.filter( - function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; } - ).length, 2); - } + 'should be two': function(files) { + assert.equal(files.filter( + function(file) { + return file.indexOf('test-rolling-file-stream-write-more') > -1; + } + ).length, 2); + } + }, + 'the first file': { + topic: function() { + fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback); }, - 'the first file': { - topic: function() { - fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback); - }, - 'should contain the last two log messages': function(contents) { - assert.equal(contents, '5.cheese\n6.cheese\n'); - } + 'should contain the last two log messages': function(contents) { + assert.equal(contents, '5.cheese\n6.cheese\n'); + } + }, + 'the second file': { + topic: function() { + fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback); }, - 'the second file': { - topic: function() { - fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback); - }, - 'should contain the first five log messages': function(contents) { - assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n'); - } + 'should contain the first five log messages': function(contents) { + assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n'); } } + } }).exportTo(module);