migrated file appender tests to mocha

This commit is contained in:
Gareth Jones
2013-08-26 22:49:12 +10:00
parent 045b0dda2b
commit 3312724d7d
2 changed files with 203 additions and 181 deletions

View File

@@ -65,16 +65,12 @@ function fileAppender (file, layout, logSize, numBackups) {
}; };
} }
function configure(config, options) { function configure(config) {
var layout; var layout;
if (config.layout) { if (config.layout) {
layout = layouts.layout(config.layout.type, config.layout); layout = layouts.layout(config.layout.type, config.layout);
} }
if (options && options.cwd && !config.absolute) {
config.filename = path.join(options.cwd, config.filename);
}
return fileAppender(config.filename, layout, config.maxLogSize, config.backups); return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
} }

View File

@@ -1,12 +1,9 @@
"use strict"; "use strict";
var vows = require('vows') var fs = require('fs')
, fs = require('fs')
, path = require('path') , path = require('path')
, sandbox = require('sandboxed-module') , sandbox = require('sandboxed-module')
, log4js = require('../lib/log4js') , log4js = require('../lib/log4js')
, assert = require('assert'); , should = require('should');
log4js.clearAppenders();
function remove(filename) { function remove(filename) {
try { try {
@@ -16,30 +13,38 @@ function remove(filename) {
} }
} }
vows.describe('log4js fileAppender').addBatch({ describe('log4js fileAppender', function() {
'adding multiple fileAppenders': {
topic: function () { describe('adding multiple fileAppenders', function() {
var listenersCount = process.listeners('exit').length var initialCount, listenersCount;
, logger = log4js.getLogger('default-settings')
, count = 5, logfile; before(function() {
var logfile
, count = 5
, config = { appenders: {}, categories: { default: { level: "debug", appenders: ["file0"] } } };
initialCount = process.listeners('exit').length
while (count--) { while (count--) {
logfile = path.join(__dirname, '/fa-default-test' + count + '.log'); logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
log4js.addAppender(require('../lib/appenders/file').appender(logfile), 'default-settings'); config.appenders["file" + count] = { type: "file", filename: logfile };
} }
return listenersCount;
},
'does not add more than one `exit` listeners': function (initialCount) {
assert.ok(process.listeners('exit').length <= initialCount + 1);
}
},
'exit listener': { log4js.configure(config);
topic: function() {
listenersCount = process.listeners('exit').length;
});
it('does not add more than one `exit` listeners', function () {
listenersCount.should.be.below(initialCount + 2);
});
});
describe('exit listener', function() {
var openedFiles = [];
before(function() {
var exitListener var exitListener
, openedFiles = []
, fileAppender = sandbox.require( , fileAppender = sandbox.require(
'../lib/appenders/file', '../lib/appenders/file',
{ {
@@ -68,178 +73,198 @@ vows.describe('log4js fileAppender').addBatch({
for (var i=0; i < 5; i += 1) { for (var i=0; i < 5; i += 1) {
fileAppender.appender('test' + i, null, 100); fileAppender.appender('test' + i, null, 100);
} }
assert.isNotEmpty(openedFiles); openedFiles.should.not.be.empty;
exitListener(); exitListener();
return openedFiles; });
},
'should close all open files': function(openedFiles) { it('should close all open files', function() {
assert.isEmpty(openedFiles); openedFiles.should.be.empty;
} });
}, });
'with default fileAppender settings': { describe('with default fileAppender settings', function() {
topic: function() { var fileContents;
before(function(done) {
var that = this var that = this
, testFile = path.join(__dirname, '/fa-default-test.log') , testFile = path.join(__dirname, '/fa-default-test.log')
, logger = log4js.getLogger('default-settings'); , logger = log4js.getLogger('default-settings');
remove(testFile); remove(testFile);
log4js.clearAppenders(); log4js.configure({
log4js.addAppender(require('../lib/appenders/file').appender(testFile), 'default-settings'); appenders: {
"file": { type: "file", filename: testFile }
},
categories: {
default: { level: "debug", appenders: [ "file" ] }
}
});
logger.info("This should be in the file."); logger.info("This should be in the file.");
setTimeout(function() { setTimeout(function() {
fs.readFile(testFile, "utf8", that.callback); fs.readFile(testFile, "utf8", function(err, contents) {
if (!err) {
fileContents = contents;
}
done(err);
});
}, 100); }, 100);
}, });
'should write log messages to the file': function(err, fileContents) {
assert.include(fileContents, "This should be in the file.\n"); it('should write log messages to the file', function() {
}, fileContents.should.include("This should be in the file.\n");
'log messages should be in the basic layout format': function(err, fileContents) { });
assert.match(
fileContents, it('log messages should be in the basic layout format', function() {
fileContents.should.match(
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - / /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
); );
} });
}, });
'with a max file size and no backups': {
topic: function() { describe('with a max file size and no backups', function() {
var testFile = path.join(__dirname, '/fa-maxFileSize-test.log') var testFile = path.join(__dirname, '/fa-maxFileSize-test.log');
, logger = log4js.getLogger('max-file-size')
, that = this; before(function() {
var logger = log4js.getLogger('max-file-size');
remove(testFile); remove(testFile);
remove(testFile + '.1'); remove(testFile + '.1');
//log file of 100 bytes maximum, no backups //log file of 100 bytes maximum, no backups
log4js.clearAppenders(); log4js.configure({
log4js.addAppender( appenders: {
require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 100, 0), "file": { type: "file", filename: testFile, maxLogSize: 100, backups: 0 }
'max-file-size' },
); categories: {
default: { level: "debug", appenders: [ "file" ] }
}
});
logger.info("This is the first log message."); logger.info("This is the first log message.");
logger.info("This is an intermediate log message."); logger.info("This is an intermediate log message.");
logger.info("This is the second log message."); logger.info("This is the second log message.");
//wait for the file system to catch up });
setTimeout(function() {
fs.readFile(testFile, "utf8", that.callback); describe('log file', function() {
}, 100); it('should only contain the second message', function(done) {
}, //wait for the file system to catch up
'log file should only contain the second message': function(err, fileContents) { setTimeout(function() {
assert.include(fileContents, "This is the second log message.\n"); fs.readFile(testFile, "utf8", function(err, fileContents) {
assert.equal(fileContents.indexOf("This is the first log message."), -1); fileContents.should.include("This is the second log message.\n");
}, fileContents.should.not.include("This is the first log message.");
'the number of files': { done(err);
topic: function() { });
fs.readdir(__dirname, this.callback); }, 100);
}, });
'starting with the test file name should be two': function(err, files) { });
//there will always be one backup if you've specified a max log size
var logFiles = files.filter( describe('the number of files starting with the test file name', function() {
function(file) { return file.indexOf('fa-maxFileSize-test.log') > -1; } it('should be two', function(done) {
); fs.readdir(__dirname, function(err, files) {
assert.equal(logFiles.length, 2); //there will always be one backup if you've specified a max log size
} var logFiles = files.filter(
} function(file) { return file.indexOf('fa-maxFileSize-test.log') > -1; }
}, );
'with a max file size and 2 backups': { logFiles.should.have.length(2);
topic: function() { done(err);
var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log') });
, logger = log4js.getLogger('max-file-size-backups'); });
});
});
describe('with a max file size and 2 backups', function() {
var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log');
before(function() {
var logger = log4js.getLogger('max-file-size-backups');
remove(testFile); remove(testFile);
remove(testFile+'.1'); remove(testFile+'.1');
remove(testFile+'.2'); remove(testFile+'.2');
//log file of 50 bytes maximum, 2 backups //log file of 50 bytes maximum, 2 backups
log4js.clearAppenders(); log4js.configure({
log4js.addAppender( appenders: {
require('../lib/appenders/file').appender(testFile, log4js.layouts.basicLayout, 50, 2), "file": { type: "file", filename: testFile, maxLogSize: 50, backups: 2 }
'max-file-size-backups' },
); categories: {
default: { level: "debug", appenders: [ "file" ] }
}
});
logger.info("This is the first log message."); logger.info("This is the first log message.");
logger.info("This is the second log message."); logger.info("This is the second log message.");
logger.info("This is the third log message."); logger.info("This is the third log message.");
logger.info("This is the fourth log message."); logger.info("This is the fourth log message.");
var that = this; });
//give the system a chance to open the stream
setTimeout(function() { describe('the log files', function() {
fs.readdir(__dirname, function(err, files) { var logFiles;
if (files) {
that.callback(null, files.sort()); before(function(done) {
} else { setTimeout(function() {
that.callback(err, files); fs.readdir(__dirname, function(err, files) {
} if (files) {
}); logFiles = files.sort().filter(
}, 200); function(file) {
}, return file.indexOf('fa-maxFileSize-with-backups-test.log') > -1;
'the log files': { }
topic: function(files) { );
var logFiles = files.filter( done(null);
function(file) { return file.indexOf('fa-maxFileSize-with-backups-test.log') > -1; } } else {
); done(err);
return logFiles; }
}, });
'should be 3': function (files) { }, 200);
assert.equal(files.length, 3); });
},
'should be named in sequence': function (files) { it('should be 3', function () {
assert.deepEqual(files, [ logFiles.should.have.length(3);
});
it('should be named in sequence', function() {
logFiles.should.eql([
'fa-maxFileSize-with-backups-test.log', 'fa-maxFileSize-with-backups-test.log',
'fa-maxFileSize-with-backups-test.log.1', 'fa-maxFileSize-with-backups-test.log.1',
'fa-maxFileSize-with-backups-test.log.2' 'fa-maxFileSize-with-backups-test.log.2'
]); ]);
}, });
'and the contents of the first file': {
topic: function(logFiles) { describe('and the contents of the first file', function() {
fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback); it('should be the last log message', function(done) {
}, fs.readFile(path.join(__dirname, logFiles[0]), "utf8", function(err, contents) {
'should be the last log message': function(contents) { contents.should.include('This is the fourth log message.');
assert.include(contents, 'This is the fourth log message.'); done(err);
} });
}, });
'and the contents of the second file': { });
topic: function(logFiles) {
fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback); describe('and the contents of the second file', function() {
}, it('should be the third log message', function(done) {
'should be the third log message': function(contents) { fs.readFile(path.join(__dirname, logFiles[1]), "utf8", function(err, contents) {
assert.include(contents, 'This is the third log message.'); contents.should.include('This is the third log message.');
} done(err);
}, });
'and the contents of the third file': { });
topic: function(logFiles) { });
fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
}, describe('and the contents of the third file', function() {
'should be the second log message': function(contents) { it('should be the second log message', function(done) {
assert.include(contents, 'This is the second log message.'); fs.readFile(path.join(__dirname, logFiles[2]), "utf8", function(err, contents) {
} contents.should.include('This is the second log message.');
} done(err);
} });
} });
}).addBatch({ });
'configure' : { });
'with fileAppender': { });
topic: function() {
var log4js = require('../lib/log4js') describe('when underlying stream errors', function() {
, logger; var consoleArgs;
//this config file defines one file appender (to ./tmp-tests.log)
//and sets the log level for "tests" to WARN before(function() {
log4js.configure('./test/log4js.json'); var errorHandler
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('tmp-tests.log', 'utf8', this.callback);
},
'should load appender configuration from a json file': function(err, contents) {
assert.include(contents, 'this should be written to the file\n');
assert.equal(contents.indexOf('this should not be written to the file'), -1);
}
}
}
}).addBatch({
'when underlying stream errors': {
topic: function() {
var consoleArgs
, errorHandler
, fileAppender = sandbox.require( , fileAppender = sandbox.require(
'../lib/appenders/file', '../lib/appenders/file',
{ {
@@ -265,16 +290,17 @@ vows.describe('log4js fileAppender').addBatch({
} }
} }
); );
fileAppender.appender('test1.log', null, 100); fileAppender.configure({
filename: 'test1.log', maxLogSize: 100
});
errorHandler({ error: 'aargh' }); errorHandler({ error: 'aargh' });
return consoleArgs; });
},
'should log the error to console.error': function(consoleArgs) { it('should log the error to console.error', function() {
assert.isNotEmpty(consoleArgs); consoleArgs.should.not.be.empty;
assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened '); consoleArgs[0].should.eql('log4js.fileAppender - Writing to file %s, error happened ');
assert.equal(consoleArgs[1], 'test1.log'); consoleArgs[1].should.eql('test1.log');
assert.equal(consoleArgs[2].error, 'aargh'); consoleArgs[2].error.should.eql('aargh');
} });
} });
});
}).export(module);