extracted streams, date-format into separate modules

This commit is contained in:
Gareth Jones
2013-08-29 22:15:50 +10:00
parent 5e144e4004
commit 72c4fb48db
15 changed files with 10 additions and 988 deletions

View File

@@ -58,7 +58,7 @@ describe('../lib/appenders/dateFile', function() {
}
},
requires: {
'../streams': {
'streamroller': {
DateRollingFileStream: function(filename) {
openedFiles.push(filename);
@@ -164,7 +164,7 @@ describe('../lib/appenders/dateFile', function() {
before(function(done) {
var log4js = require('../lib/log4js')
, format = require('../lib/date_format')
, format = require('date-format')
, logger
, options = {
"appenders": {

View File

@@ -1,42 +0,0 @@
"use strict";
var should = require('should')
, dateFormat = require('../lib/date_format');
describe('date_format', function() {
var date = new Date(2010, 0, 11, 14, 31, 30, 5);
it('should format a date as string using a pattern', function() {
dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql("11 01 2010 14:31:30.005");
});
it('should default to the ISO8601 format', function() {
dateFormat.asString(date).should.eql('2010-01-11 14:31:30.005');
});
it('should provide a ISO8601 with timezone offset format', function() {
date.getTimezoneOffset = function() { return -660; };
dateFormat.asString(
dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
date
).should.eql(
"2010-01-11T14:31:30+1100"
);
date.getTimezoneOffset = function() { return 120; };
dateFormat.asString(
dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT,
date
).should.eql(
"2010-01-11T14:31:30-0200"
);
});
it('should provide a just-the-time format', function() {
dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
});
it('should provide a custom format', function() {
date.getTimezoneOffset = function() { return 120; };
dateFormat.asString("O.SSS.ss.mm.hh.dd.MM.yy", date).should.eql('-0200.005.30.31.14.11.01.10');
});
});

View File

@@ -62,7 +62,7 @@ describe('log4js fileAppender', function() {
}
},
requires: {
'../streams': {
'streamroller': {
RollingFileStream: function(filename) {
openedFiles.push(filename);
@@ -304,7 +304,7 @@ describe('log4js fileAppender', function() {
}
},
requires: {
'../streams': {
'streamroller': {
RollingFileStream: function(filename) {
this.end = function() {};

View File

@@ -1,86 +0,0 @@
"use strict";
var should = require('should')
, fs = require('fs')
, sandbox = require('sandboxed-module');
describe('../../lib/streams/BaseRollingFileStream', function() {
describe('when node version < 0.10.0', function() {
it('should use readable-stream to maintain compatibility', function() {
var streamLib = sandbox.load(
'../../lib/streams/BaseRollingFileStream',
{
globals: {
process: {
version: '0.8.11'
}
},
requires: {
'readable-stream': {
Writable: function() {}
}
}
}
);
streamLib.required.should.have.property('readable-stream');
streamLib.required.should.not.have.property('stream');
});
});
describe('when node version > 0.10.0', function() {
it('should use the core stream module', function() {
var streamLib = sandbox.load(
'../../lib/streams/BaseRollingFileStream',
{
globals: {
process: {
version: '0.10.1'
}
},
requires: {
'stream': {
Writable: function() {}
}
}
}
);
streamLib.required.should.have.property('stream');
streamLib.required.should.not.have.property('readable-stream');
});
});
describe('when no filename is passed', function() {
it('should throw an error', function() {
var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream');
(function() {
new BaseRollingFileStream();
}).should.throw();
});
});
describe('default behaviour', function() {
var stream;
before(function() {
var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream');
stream = new BaseRollingFileStream('basetest.log');
});
after(function(done) {
fs.unlink('basetest.log', done);
});
it('should not want to roll', function() {
stream.shouldRoll().should.eql(false);
});
it('should not roll', function() {
var cbCalled = false;
//just calls the callback straight away, no async calls
stream.roll('basetest.log', function() { cbCalled = true; });
cbCalled.should.eql(true);
});
});
});

View File

@@ -1,259 +0,0 @@
"use strict";
var should = require('should')
, fs = require('fs')
, semver = require('semver')
, streams
, DateRollingFileStream
, testTime = new Date(2012, 8, 12, 10, 37, 11);
if (semver.satisfies(process.version, '>=0.10.0')) {
streams = require('stream');
} else {
streams = require('readable-stream');
}
DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
function remove(filename, cb) {
fs.unlink(filename, function() { cb(); });
}
function now() {
return testTime.getTime();
}
describe('DateRollingFileStream', function() {
describe('arguments', function() {
var stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-1',
'yyyy-mm-dd.hh'
);
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-1', done);
});
it('should take a filename and a pattern and return a WritableStream', function() {
stream.filename.should.eql(__dirname + '/test-date-rolling-file-stream-1');
stream.pattern.should.eql('yyyy-mm-dd.hh');
stream.should.be.instanceOf(streams.Writable);
});
it('with default settings for the underlying stream', function() {
stream.theStream.mode.should.eql(420);
stream.theStream.flags.should.eql('a');
//encoding is not available on the underlying stream
//assert.equal(stream.encoding, 'utf8');
});
});
describe('default arguments', function() {
var stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2');
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-2', done);
});
it('should have pattern of .yyyy-MM-dd', function() {
stream.pattern.should.eql('.yyyy-MM-dd');
});
});
describe('with stream arguments', function() {
var stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-3',
'yyyy-MM-dd',
{ mode: parseInt('0666', 8) }
);
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-3', done);
});
it('should pass them to the underlying stream', function() {
stream.theStream.mode.should.eql(parseInt('0666', 8));
});
});
describe('with stream arguments but no pattern', function() {
var stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-4',
{ mode: parseInt('0666', 8) }
);
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-4', done);
});
it('should pass them to the underlying stream', function() {
stream.theStream.mode.should.eql(parseInt('0666', 8));
});
it('should use default pattern', function() {
stream.pattern.should.eql('.yyyy-MM-dd');
});
});
describe('with a pattern of .yyyy-MM-dd', function() {
var stream;
before(function(done) {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd',
null,
now
);
stream.write("First message\n", 'utf8', done);
});
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-5', done);
});
it('should create a file with the base name', function(done) {
fs.readFile(__dirname + '/test-date-rolling-file-stream-5', 'utf8', function(err, contents) {
contents.should.eql("First message\n");
done(err);
});
});
describe('when the day changes', function() {
before(function(done) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', done);
});
describe('the number of files', function() {
var files = [];
before(function(done) {
fs.readdir(__dirname, function(err, list) {
files = list;
done(err);
});
});
it('should be two', function() {
files.filter(
function(file) {
return file.indexOf('test-date-rolling-file-stream-5') > -1;
}
).should.have.length(2);
});
});
describe('the file without a date', function() {
it('should contain the second message', function(done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-5', 'utf8',
function(err, contents) {
contents.should.eql("Second message\n");
done(err);
}
);
});
});
describe('the file with the date', function() {
it('should contain the first message', function(done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-5.2012-09-12', 'utf8',
function(err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
});
});
});
describe('with alwaysIncludePattern', function() {
var stream;
before(function(done) {
testTime = new Date(2012, 8, 12, 0, 10, 12);
remove(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12',
function() {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-pattern',
'.yyyy-MM-dd',
{ alwaysIncludePattern: true },
now
);
stream.write("First message\n", 'utf8', done);
}
);
});
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', done);
});
it('should create a file with the pattern set', function(done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 'utf8',
function(err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
describe('when the day changes', function() {
before(function(done) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
after(function(done) {
remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', done);
});
describe('the number of files', function() {
it('should be two', function(done) {
fs.readdir(__dirname, function(err, files) {
files.filter(
function(file) {
return file.indexOf('test-date-rolling-file-stream-pattern') > -1;
}
).should.have.length(2);
done(err);
});
});
});
describe('the file with the later date', function() {
it('should contain the second message', function(done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', 'utf8',
function(err, contents) {
contents.should.eql("Second message\n");
done(err);
}
);
});
});
describe('the file with the date', function() {
it('should contain the first message', function(done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 'utf8',
function(err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
});
});
});
});

View File

@@ -1,264 +0,0 @@
"use strict";
var async = require('async')
, should = require('should')
, fs = require('fs')
, semver = require('semver')
, streams
, RollingFileStream;
if (semver.satisfies(process.version, '>=0.10.0')) {
streams = require('stream');
} else {
streams = require('readable-stream');
}
RollingFileStream = require('../../lib/streams').RollingFileStream;
function remove(filename, cb) {
fs.unlink(filename, function(err) { cb(); });
}
function create(filename, cb) {
fs.writeFile(filename, "test file", cb);
}
describe('RollingFileStream', function() {
describe('arguments', function() {
var stream;
before(function(done) {
remove(__dirname + "/test-rolling-file-stream", function() {
stream = new RollingFileStream("test-rolling-file-stream", 1024, 5);
done();
});
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream", done);
});
it('should take a filename, file size (bytes), no. backups, return Writable', function() {
stream.should.be.an.instanceOf(streams.Writable);
stream.filename.should.eql("test-rolling-file-stream");
stream.size.should.eql(1024);
stream.backups.should.eql(5);
});
it('should apply default settings to the underlying stream', function() {
stream.theStream.mode.should.eql(420);
stream.theStream.flags.should.eql('a');
//encoding isn't a property on the underlying stream
//assert.equal(stream.theStream.encoding, 'utf8');
});
});
describe('with stream arguments', function() {
it('should pass them to the underlying stream', function() {
var stream = new RollingFileStream(
'test-rolling-file-stream',
1024,
5,
{ mode: parseInt('0666', 8) }
);
stream.theStream.mode.should.eql(parseInt('0666', 8));
});
after(function(done) {
remove(__dirname + '/test-rolling-file-stream', done);
});
});
describe('without size', function() {
it('should throw an error', function() {
(function() {
new RollingFileStream(__dirname + "/test-rolling-file-stream");
}).should.throw();
});
});
describe('without number of backups', function() {
it('should default to 1 backup', function() {
var stream = new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
stream.backups.should.eql(1);
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream", done);
});
});
describe('writing less than the file size', function() {
before(function(done) {
remove(__dirname + "/test-rolling-file-stream-write-less", function() {
var stream = new RollingFileStream(
__dirname + "/test-rolling-file-stream-write-less",
100
);
stream.write("cheese", "utf8", function() {
stream.end(done);
});
});
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream-write-less", done);
});
it('should write to the file', function(done) {
fs.readFile(
__dirname + "/test-rolling-file-stream-write-less", "utf8",
function(err, contents) {
contents.should.eql("cheese");
done(err);
}
);
});
it('should write one file', function(done) {
fs.readdir(__dirname, function(err, files) {
files.filter(
function(file) { return file.indexOf('test-rolling-file-stream-write-less') > -1; }
).should.have.length(1);
done(err);
});
});
});
describe('writing more than the file size', function() {
before(function(done) {
async.forEach(
[
__dirname + "/test-rolling-file-stream-write-more",
__dirname + "/test-rolling-file-stream-write-more.1"
],
remove,
function() {
var stream = new RollingFileStream(
__dirname + "/test-rolling-file-stream-write-more",
45
);
async.forEachSeries(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end(done);
}
);
}
);
});
after(function(done) {
async.forEach(
[
__dirname + "/test-rolling-file-stream-write-more",
__dirname + "/test-rolling-file-stream-write-more.1"
],
remove,
done
);
});
it('should write two files' , function(done) {
fs.readdir(__dirname, function(err, files) {
files.filter(
function(file) {
return file.indexOf('test-rolling-file-stream-write-more') > -1;
}
).should.have.length(2);
done(err);
});
});
it('should write the last two log messages to the first file', function(done) {
fs.readFile(
__dirname + "/test-rolling-file-stream-write-more", "utf8",
function(err, contents) {
contents.should.eql('5.cheese\n6.cheese\n');
done(err);
});
});
it('should write the first five log messages to the second file', function(done) {
fs.readFile(
__dirname + '/test-rolling-file-stream-write-more.1', "utf8",
function(err, contents) {
contents.should.eql('0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
done(err);
}
);
});
});
describe('when many files already exist', function() {
before(function(done) {
async.forEach(
[
__dirname + '/test-rolling-stream-with-existing-files.11',
__dirname + '/test-rolling-stream-with-existing-files.20',
__dirname + '/test-rolling-stream-with-existing-files.-1',
__dirname + '/test-rolling-stream-with-existing-files.1.1',
__dirname + '/test-rolling-stream-with-existing-files.1'
],
remove,
function(err) {
if (err) done(err);
async.forEach(
[
__dirname + '/test-rolling-stream-with-existing-files.11',
__dirname + '/test-rolling-stream-with-existing-files.20',
__dirname + '/test-rolling-stream-with-existing-files.-1',
__dirname + '/test-rolling-stream-with-existing-files.1.1',
__dirname + '/test-rolling-stream-with-existing-files.1'
],
create,
function(err) {
if (err) done(err);
var stream = new RollingFileStream(
__dirname + "/test-rolling-stream-with-existing-files",
45,
5
);
async.forEachSeries(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end(done);
}
);
}
);
}
);
});
after(function(done) {
async.forEach([
'test-rolling-stream-with-existing-files',
'test-rolling-stream-with-existing-files.1',
'test-rolling-stream-with-existing-files.2',
'test-rolling-stream-with-existing-files.11',
'test-rolling-stream-with-existing-files.20'
], remove, done);
});
it('should roll the files', function(done) {
fs.readdir(__dirname, function(err, files) {
files.should.include('test-rolling-stream-with-existing-files');
files.should.include('test-rolling-stream-with-existing-files.1');
files.should.include('test-rolling-stream-with-existing-files.2');
files.should.include('test-rolling-stream-with-existing-files.11');
files.should.include('test-rolling-stream-with-existing-files.20');
done(err);
});
});
});
});