Backported the old streams. Nasty if statements abound.

This commit is contained in:
Gareth Jones
2013-04-07 14:32:39 +10:00
parent 36c5175a55
commit 9c5dff382e
14 changed files with 783 additions and 205 deletions

View File

@@ -1,8 +1,9 @@
var vows = require('vows'),
assert = require('assert'),
path = require('path'),
fs = require('fs'),
log4js = require('../lib/log4js');
assert = require('assert'),
path = require('path'),
fs = require('fs'),
sandbox = require('sandboxed-module'),
log4js = require('../lib/log4js');
function removeFile(filename) {
return function() {
@@ -95,4 +96,65 @@ vows.describe('../lib/appenders/dateFile').addBatch({
}
}
}).addBatch({
'with node version less than 0.10': {
topic: function() {
var oldStyleStreamCreated = false
, appender = sandbox.require(
'../lib/appenders/dateFile',
{
globals: {
process: {
version: "v0.8.1",
on: function() {}
}
},
requires: {
'../old-streams': {
BufferedWriteStream: function() {
oldStyleStreamCreated = true;
this.on = function() {};
},
DateRollingFileStream: function() {
this.on = function() {};
}
}
}
}
).appender('cheese.log', null, 1000, 1);
return oldStyleStreamCreated;
},
'should load the old-style streams': function(loaded) {
assert.isTrue(loaded);
}
},
'with node version greater than or equal to 0.10': {
topic: function() {
var oldStyleStreamCreated = false
, appender = sandbox.require(
'../lib/appenders/dateFile',
{
globals: {
process: {
version: "v0.10.1",
on: function() {}
}
},
requires: {
'../streams': {
DateRollingFileStream: function() {
this.on = function() {};
}
}
}
}
).appender('cheese.log', null, 1000, 1);
return oldStyleStreamCreated;
},
'should load the new streams': function(loaded) {
assert.isFalse(loaded);
}
}
}).exportTo(module);

View File

@@ -2,6 +2,8 @@ var vows = require('vows')
, fs = require('fs')
, path = require('path')
, log4js = require('../lib/log4js')
, sandbox = require('sandboxed-module')
, semver = require('semver')
, assert = require('assert');
log4js.clearAppenders();
@@ -17,9 +19,10 @@ function remove(filename) {
vows.describe('log4js fileAppender').addBatch({
'adding multiple fileAppenders': {
topic: function () {
var listenersCount = process.listeners('exit').length
, logger = log4js.getLogger('default-settings')
, count = 5, logfile;
var listenersCount = process.listeners('exit').length
, logger = log4js.getLogger('default-settings')
, count = 5
, logfile;
while (count--) {
logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
@@ -109,12 +112,12 @@ vows.describe('log4js fileAppender').addBatch({
//give the system a chance to open the stream
setTimeout(function() {
fs.readdir(__dirname, function(err, files) {
if (files) {
that.callback(null, files.sort());
} else {
that.callback(err, files);
}
});
if (files) {
that.callback(null, files.sort());
} else {
that.callback(err, files);
}
});
}, 200);
},
'the log files': {
@@ -133,7 +136,14 @@ vows.describe('log4js fileAppender').addBatch({
fs.readFile(path.join(__dirname, logFiles[0]), "utf8", this.callback);
},
'should be the last log message': function(contents) {
//there's a difference in behaviour between
//old-style streams and new ones (the new ones are
//correct)
if (semver.satisfies(process.version, ">=0.10.0")) {
assert.include(contents, 'This is the fourth log message.');
} else {
assert.isEmpty(contents);
}
}
},
'and the contents of the second file': {
@@ -141,7 +151,14 @@ vows.describe('log4js fileAppender').addBatch({
fs.readFile(path.join(__dirname, logFiles[1]), "utf8", this.callback);
},
'should be the third log message': function(contents) {
//there's a difference in behaviour between
//old-style streams and new ones (the new ones are
//correct)
if (semver.satisfies(process.version, ">=0.10.0")) {
assert.include(contents, 'This is the third log message.');
} else {
assert.include(contents, 'This is the fourth log message.');
}
}
},
'and the contents of the third file': {
@@ -149,31 +166,98 @@ vows.describe('log4js fileAppender').addBatch({
fs.readFile(path.join(__dirname, logFiles[2]), "utf8", this.callback);
},
'should be the second log message': function(contents) {
//there's a difference in behaviour between
//old-style streams and new ones (the new ones are
//correct)
if (semver.satisfies(process.version, ">=0.10.0")) {
assert.include(contents, 'This is the second log message.');
} else {
assert.include(contents, 'This is the third log message.');
}
}
}
}
}
}).addBatch({
'configure' : {
'with fileAppender': {
'configure' : {
'with fileAppender': {
topic: function() {
var log4js = require('../lib/log4js')
, logger;
//this config file defines one file appender (to ./tmp-tests.log)
//and sets the log level for "tests" to WARN
log4js.configure('test/log4js.json');
logger = log4js.getLogger('tests');
logger.info('this should not be written to the file');
logger.warn('this should be written to the file');
var log4js = require('../lib/log4js')
, logger;
//this config file defines one file appender (to ./tmp-tests.log)
//and sets the log level for "tests" to WARN
log4js.configure('test/log4js.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('tmp-tests.log', 'utf8', this.callback);
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);
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({
'with node version less than 0.10': {
topic: function() {
var oldStyleStreamCreated = false
, appender = sandbox.require(
'../lib/appenders/file',
{
globals: {
process: {
version: "v0.8.1",
on: function() {}
}
},
requires: {
'../old-streams': {
BufferedWriteStream: function() {
oldStyleStreamCreated = true;
this.on = function() {};
},
RollingFileStream: function() {
this.on = function() {};
}
}
}
}
).appender('cheese.log', null, 1000, 1);
return oldStyleStreamCreated;
},
'should load the old-style streams': function(loaded) {
assert.isTrue(loaded);
}
},
'with node version greater than or equal to 0.10': {
topic: function() {
var oldStyleStreamCreated = false
, appender = sandbox.require(
'../lib/appenders/file',
{
globals: {
process: {
version: "v0.10.1",
on: function() {}
}
},
requires: {
'../streams': {
RollingFileStream: function() {
this.on = function() {};
}
}
}
}
).appender('cheese.log', null, 1000, 1);
return oldStyleStreamCreated;
},
'should load the new streams': function(loaded) {
assert.isFalse(loaded);
}
}
}).export(module);

View File

@@ -56,6 +56,12 @@ vows.describe('log4js-abspath').addBatch({
};
}
}
},
globals: {
process: {
version: "v0.10.1",
on: function() {}
}
}
}
);
@@ -66,4 +72,4 @@ vows.describe('log4js-abspath').addBatch({
assert.equal(fileOpened, "/absolute/path/to/whatever.log");
}
},
}).export(module);
}).export(module);

View File

@@ -2,7 +2,8 @@ var vows = require('vows')
, assert = require('assert')
, streams = require('stream')
, fs = require('fs')
, DateRollingFileStream = require('../../lib/streams').DateRollingFileStream
, semver = require('semver')
, DateRollingFileStream
, testTime = new Date(2012, 8, 12, 10, 37, 11);
function cleanUp(filename) {
@@ -15,108 +16,112 @@ function now() {
return testTime.getTime();
}
vows.describe('DateRollingFileStream').addBatch({
if (semver.satisfies(process.version, '>=0.10.0')) {
DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
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');
}
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');
}
},
'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');
}
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: 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);
}
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);
}
},
'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'),
'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');
}
topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-4', { mode: 0666 }),
teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'),
'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);
});
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);
},
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");
}
},
'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");
}
}
'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-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");
}
}
}
}
}).exportTo(module);
}).exportTo(module);
}

View File

@@ -4,7 +4,9 @@ var vows = require('vows')
, events = require('events')
, fs = require('fs')
, streams = require('stream')
, RollingFileStream = require('../../lib/streams').RollingFileStream;
, semver = require('semver')
, RollingFileStream;
function remove(filename) {
try {
@@ -14,54 +16,57 @@ function remove(filename) {
}
}
vows.describe('RollingFileStream').addBatch({
if (semver.satisfies(process.version, '>=0.10.0')) {
RollingFileStream = require('../../lib/streams').RollingFileStream;
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');
}
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');
}
},
'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);
}
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);
}
},
'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);
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);
},
'should default to 1 backup': function(stream) {
assert.equal(stream.backups, 1);
}
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);
}
},
'writing less than the file size': {
topic: function() {
@@ -70,7 +75,7 @@ vows.describe('RollingFileStream').addBatch({
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");
@@ -89,12 +94,12 @@ vows.describe('RollingFileStream').addBatch({
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);
async.forEach([0, 1, 2, 3, 4, 5, 6], function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
}, function() {
stream.end();
that.callback();
});
stream.end();
that.callback();
});
},
'the number of files': {
topic: function() {
@@ -102,8 +107,8 @@ vows.describe('RollingFileStream').addBatch({
},
'should be two': function(files) {
assert.equal(files.filter(
function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }
).length, 2);
function(file) { return file.indexOf('test-rolling-file-stream-write-more') > -1; }
).length, 2);
}
},
'the first file': {
@@ -123,4 +128,5 @@ vows.describe('RollingFileStream').addBatch({
}
}
}
}).exportTo(module);
}).exportTo(module);
}