improved coverage of file appenders

This commit is contained in:
Gareth Jones 2013-06-28 08:44:54 +10:00
parent 2b070e5470
commit c258470cda
2 changed files with 158 additions and 2 deletions

View File

@ -3,6 +3,7 @@ var vows = require('vows')
, assert = require('assert')
, path = require('path')
, fs = require('fs')
, sandbox = require('sandboxed-module')
, log4js = require('../lib/log4js');
function removeFile(filename) {
@ -41,6 +42,46 @@ vows.describe('../lib/appenders/dateFile').addBatch({
'should only add one `exit` listener': function (initialCount) {
assert.equal(process.listeners('exit').length, initialCount + 1);
},
},
'exit listener': {
topic: function() {
var exitListener
, openedFiles = []
, dateFileAppender = sandbox.require(
'../lib/appenders/dateFile',
{
globals: {
process: {
on: function(evt, listener) {
exitListener = listener;
}
}
},
requires: {
'../streams': {
DateRollingFileStream: function(filename) {
openedFiles.push(filename);
this.end = function() {
openedFiles.shift();
};
}
}
}
}
);
for (var i=0; i < 5; i += 1) {
dateFileAppender.appender('test' + i);
}
assert.isNotEmpty(openedFiles);
exitListener();
return openedFiles;
},
'should close all open files': function(openedFiles) {
assert.isEmpty(openedFiles);
}
},
@ -139,7 +180,39 @@ vows.describe('../lib/appenders/dateFile').addBatch({
'should not overwrite the file on open (bug found in issue #132)': function(contents) {
assert.include(contents, 'this is existing data');
}
},
'with cwd option': {
topic: function() {
var fileOpened,
appender = sandbox.require(
'../lib/appenders/dateFile',
{ requires:
{ '../streams':
{ DateRollingFileStream:
function(file) {
fileOpened = file;
return {
on: function() {},
end: function() {}
};
}
}
}
}
);
appender.configure(
{
filename: "whatever.log",
maxLogSize: 10
},
{ cwd: '/absolute/path/to' }
);
return fileOpened;
},
'should prepend options.cwd to config.filename': function(fileOpened) {
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
}
}
}
}).exportTo(module);

View File

@ -2,6 +2,7 @@
var vows = require('vows')
, fs = require('fs')
, path = require('path')
, sandbox = require('sandboxed-module')
, log4js = require('../lib/log4js')
, assert = require('assert');
@ -30,10 +31,51 @@ vows.describe('log4js fileAppender').addBatch({
return listenersCount;
},
'does not adds more than one `exit` listeners': function (initialCount) {
'does not add more than one `exit` listeners': function (initialCount) {
assert.ok(process.listeners('exit').length <= initialCount + 1);
}
},
'exit listener': {
topic: function() {
var exitListener
, openedFiles = []
, fileAppender = sandbox.require(
'../lib/appenders/file',
{
globals: {
process: {
on: function(evt, listener) {
exitListener = listener;
}
}
},
requires: {
'../streams': {
RollingFileStream: function(filename) {
openedFiles.push(filename);
this.end = function() {
openedFiles.shift();
};
this.on = function() {};
}
}
}
}
);
for (var i=0; i < 5; i += 1) {
fileAppender.appender('test' + i, null, 100);
}
assert.isNotEmpty(openedFiles);
exitListener();
return openedFiles;
},
'should close all open files': function(openedFiles) {
assert.isEmpty(openedFiles);
}
},
'with default fileAppender settings': {
topic: function() {
@ -193,5 +235,46 @@ vows.describe('log4js fileAppender').addBatch({
}
}
}
}).addBatch({
'when underlying stream errors': {
topic: function() {
var consoleArgs
, errorHandler
, fileAppender = sandbox.require(
'../lib/appenders/file',
{
globals: {
console: {
error: function() {
consoleArgs = Array.prototype.slice.call(arguments);
}
}
},
requires: {
'../streams': {
RollingFileStream: function(filename) {
this.end = function() {};
this.on = function(evt, cb) {
if (evt === 'error') {
errorHandler = cb;
}
};
}
}
}
}
);
fileAppender.appender('test1.log', null, 100);
errorHandler({ error: 'aargh' });
return consoleArgs;
},
'should log the error to console.error': function(consoleArgs) {
assert.isNotEmpty(consoleArgs);
assert.equal(consoleArgs[0], 'log4js.fileAppender - Writing to file %s, error happened ');
assert.equal(consoleArgs[1], 'test1.log');
assert.equal(consoleArgs[2].error, 'aargh');
}
}
}).export(module);