From 936ad4da8e8c51be12fd050b01e8b3721d6f576d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 5 May 2013 13:44:01 +1000 Subject: [PATCH] fixed tests broken by alwaysIncludePattern --- lib/appenders/dateFile.js | 4 +- lib/streams/DateRollingFileStream.js | 37 +++++---- test/dateFileAppender-test.js | 93 ++++++++++------------ test/streams/DateRollingFileStream-test.js | 4 +- 4 files changed, 69 insertions(+), 69 deletions(-) diff --git a/lib/appenders/dateFile.js b/lib/appenders/dateFile.js index ec75f90..317c4e8 100644 --- a/lib/appenders/dateFile.js +++ b/lib/appenders/dateFile.js @@ -22,7 +22,7 @@ process.on('exit', function() { function appender(filename, pattern, alwaysIncludePattern, layout) { layout = layout || layouts.basicLayout; - var logFile = new streams.DateRollingFileStream(filename, pattern, alwaysIncludePattern); + var logFile = new streams.DateRollingFileStream(filename, pattern, { alwaysIncludePattern: alwaysIncludePattern }); openFiles.push(logFile); return function(logEvent) { @@ -35,7 +35,7 @@ function configure(config, options) { var layout; if (config.layout) { - layout = layouts.layout(config.layout.type, config.layout); + layout = layouts.layout(config.layout.type, config.layout); } if (!config.alwaysIncludePattern) { diff --git a/lib/streams/DateRollingFileStream.js b/lib/streams/DateRollingFileStream.js index 01755f6..0fc9394 100644 --- a/lib/streams/DateRollingFileStream.js +++ b/lib/streams/DateRollingFileStream.js @@ -13,25 +13,30 @@ if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) { debug = function() { }; } -function DateRollingFileStream(filename, pattern, alwaysIncludePattern, options, now) { - debug("Now is " + now); - if (pattern && typeof(pattern) === 'object') { - now = options; - options = pattern; - pattern = null; - } - this.pattern = pattern || '.yyyy-MM-dd'; - this.now = now || Date.now; - this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now())); +function DateRollingFileStream(filename, pattern, options, now) { + debug("Now is " + now); + if (pattern && typeof(pattern) === 'object') { + now = options; + options = pattern; + pattern = null; + } + this.pattern = pattern || '.yyyy-MM-dd'; + this.now = now || Date.now; + this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now())); + this.baseFilename = filename; - this.alwaysIncludePattern = alwaysIncludePattern; - if (this.alwaysIncludePattern) { - this.baseFilename = filename; - filename = filename + this.lastTimeWeWroteSomething; + if (options) { + if (options.alwaysIncludePattern) { + filename = filename + this.lastTimeWeWroteSomething; + } + delete options.alwaysIncludePattern; + if (options === {}) { + options = null; } - debug("this.now is " + this.now + ", now is " + now); + } + debug("this.now is " + this.now + ", now is " + now); - DateRollingFileStream.super_.call(this, filename, options); + DateRollingFileStream.super_.call(this, filename, options); } util.inherits(DateRollingFileStream, BaseRollingFileStream); diff --git a/test/dateFileAppender-test.js b/test/dateFileAppender-test.js index 50a0458..6b8b047 100644 --- a/test/dateFileAppender-test.js +++ b/test/dateFileAppender-test.js @@ -72,63 +72,58 @@ vows.describe('../lib/appenders/dateFile').addBatch({ } }).addBatch({ - 'configure': { - 'with dateFileAppender': { + 'configure': { + 'with dateFileAppender': { topic: function() { - var log4js = require('../lib/log4js') - , logger; - //this config file defines one file appender (to ./date-file-test.log) - //and sets the log level for "tests" to WARN - log4js.configure('test/with-dateFile.json'); - logger = log4js.getLogger('tests'); - logger.info('this should not be written to the file'); - logger.warn('this should be written to the file'); - - fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback); + var log4js = require('../lib/log4js') + , logger; + //this config file defines one file appender (to ./date-file-test.log) + //and sets the log level for "tests" to WARN + log4js.configure('test/with-dateFile.json'); + logger = log4js.getLogger('tests'); + logger.info('this should not be written to the file'); + logger.warn('this should be written to the file'); + + fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback); }, - teardown: removeFile('date-file-test.log'), - + teardown: removeFile('date-file-test.log'), + 'should load appender configuration from a json file': function(err, contents) { - assert.include(contents, 'this should be written to the file' + require('os').EOL); - assert.equal(contents.indexOf('this should not be written to the file'), -1); + assert.include(contents, 'this should be written to the file' + require('os').EOL); + assert.equal(contents.indexOf('this should not be written to the file'), -1); } - }, - 'with options.alwaysIncludePattern': { - topic: function() { - var log4js = require('../lib/log4js') + }, + 'with options.alwaysIncludePattern': { + topic: function() { + var log4js = require('../lib/log4js') , format = require('../lib/date_format') , logger , options = { - "appenders": [ - { - "category": "tests", - "type": "dateFile", - "filename": "test/date-file-test", - "pattern": "-from-MM-dd.log", - "alwaysIncludePattern": true, - "layout": { - "type": "messagePassThrough" - } + "appenders": [ + { + "category": "tests", + "type": "dateFile", + "filename": "test/date-file-test", + "pattern": "-from-MM-dd.log", + "alwaysIncludePattern": true, + "layout": { + "type": "messagePassThrough" + } + } + ] } - ] - } , thisTime = format.asString(options.appenders[0].pattern, new Date()); - log4js.clearAppenders(); - log4js.configure(options); - logger = log4js.getLogger('tests'); - logger.warn('this should be written to the file'); - return thisTime; - }, - teardown: function(topic) { - removeFile('date-file-test' + topic); - }, - 'should create file with the correct pattern': function(topic) { - assert.equal(fs.existsSync(path.join(__dirname, 'date-file-test' + topic)), true); - }, - 'should not create file with the base filename': function(topic) { - assert.equal(fs.existsSync(path.join(__dirname, 'date-file-test')), false); + log4js.clearAppenders(); + log4js.configure(options); + logger = log4js.getLogger('tests'); + logger.warn('this should be written to the file with the appended date'); + this.teardown = removeFile('date-file-test' + thisTime); + fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', this.callback); + }, + 'should create file with the correct pattern': function(contents) { + assert.include(contents, 'this should be written to the file with the appended date'); + } } - } - - } + + } }).exportTo(module); diff --git a/test/streams/DateRollingFileStream-test.js b/test/streams/DateRollingFileStream-test.js index 0d97cf1..69e9ba8 100644 --- a/test/streams/DateRollingFileStream-test.js +++ b/test/streams/DateRollingFileStream-test.js @@ -76,8 +76,8 @@ vows.describe('DateRollingFileStream').addBatch({ 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); - }); + that.callback(null, stream); + }); }, teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'),