123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- "use strict";
- var should = require('should')
- , fs = require('fs')
- , zlib = require('zlib')
- , util = require('util')
- , async = require('async')
- , format = require('date-format')
- , streams = require('readable-stream')
- , DateRollingFileStream
- , testTime = new Date(2012, 8, 12, 10, 37, 11);
- DateRollingFileStream = require('../lib').DateRollingFileStream;
- function remove(filename, cb) {
- fs.unlink(filename, function () {
- cb();
- });
- }
- function now() {
- return testTime.getTime();
- }
- describe('DateRollingFileStream', function () {
- describe('arguments', function () {
- var stream;
- before(function (done) {
- stream = new DateRollingFileStream(
- __dirname + '/test-date-rolling-file-stream-1',
- 'yyyy-mm-dd.hh'
- );
- done();
- });
- 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 (done) {
- 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);
- done();
- });
- it('with default settings for the underlying stream', function (done) {
- 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');
- done();
- });
- });
- describe('default arguments', function () {
- var stream;
- before(function(done) {
- stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2');
- done();
- });
- after(function (done) {
- remove(__dirname + '/test-date-rolling-file-stream-2', done);
- });
- it('should have pattern of .yyyy-MM-dd', function (done) {
- stream.pattern.should.eql('.yyyy-MM-dd');
- done();
- });
- });
- describe('with stream arguments', function () {
- var stream;
- before(function(done) {
- stream = new DateRollingFileStream(
- __dirname + '/test-date-rolling-file-stream-3',
- 'yyyy-MM-dd',
- {mode: parseInt('0666', 8)}
- );
- done();
- });
- after(function (done) {
- remove(__dirname + '/test-date-rolling-file-stream-3', done);
- });
- it('should pass them to the underlying stream', function (done) {
- stream.theStream.mode.should.eql(parseInt('0666', 8));
- done();
- });
- });
- describe('with stream arguments but no pattern', function () {
- var stream;
- before(function (done) {
- stream = new DateRollingFileStream(
- __dirname + '/test-date-rolling-file-stream-4',
- {mode: parseInt('0666', 8)}
- );
- done();
- });
- after(function (done) {
- remove(__dirname + '/test-date-rolling-file-stream-4', done);
- });
- it('should pass them to the underlying stream', function (done) {
- stream.theStream.mode.should.eql(parseInt('0666', 8));
- done();
- });
- it('should use default pattern', function (done) {
- stream.pattern.should.eql('.yyyy-MM-dd');
- done();
- });
- });
- 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 (done) {
- files.filter(
- function (file) {
- return file.indexOf('test-date-rolling-file-stream-5') > -1;
- }
- ).should.have.length(2);
- done();
- });
- });
- 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
- );
- setTimeout(function() {
- stream.write("First message\n", 'utf8', done);
- }, 50);
- }
- );
- });
- 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);
- }
- );
- });
- });
- });
- });
- describe('with compress option', function () {
- var stream;
- before(function (done) {
- testTime = new Date(2012, 8, 12, 0, 10, 12);
- stream = new DateRollingFileStream(
- __dirname + '/compressed.log',
- '.yyyy-MM-dd',
- {compress: true},
- now
- );
- stream.write("First message\n", 'utf8', done);
- });
- 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);
- });
- it('should be two files, one compressed', function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('compressed.log') > -1;
- }
- );
- logFiles.should.have.length(2);
- zlib.gunzip(
- fs.readFileSync(__dirname + '/compressed.log.2012-09-12.gz'),
- function (err, contents) {
- contents.toString('utf8').should.eql('First message\n');
- fs.readFileSync(__dirname + '/compressed.log', 'utf8').should.eql('Second message\n');
- done(err);
- }
- );
- });
- });
- });
- after(function (done) {
- remove(
- __dirname + '/compressed.log',
- function () {
- remove(__dirname + '/compressed.log.2012-09-12.gz', done);
- }
- );
- });
- });
- describe('with keepFileExt option', function () {
- var stream;
- before(function (done) {
- testTime = new Date(2012, 8, 12, 0, 10, 12);
- stream = new DateRollingFileStream(
- __dirname + '/keepFileExt.log',
- '.yyyy-MM-dd',
- {keepFileExt: true},
- now
- );
- stream.write("First message\n", 'utf8', done);
- });
- 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);
- });
- it('should be two files', function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('keepFileExt') > -1;
- }
- );
- logFiles.should.have.length(2);
- fs.readFileSync(__dirname + '/keepFileExt.2012-09-12.log', 'utf8')
- .should.eql('First message\n');
- fs.readFileSync(__dirname + '/keepFileExt.log', 'utf8')
- .should.eql('Second message\n');
- done(err);
- });
- });
- });
- after(function (done) {
- remove(
- __dirname + '/keepFileExt.log',
- function () {
- remove(__dirname + '/keepFileExt.2012-09-12.log', done);
- }
- );
- });
- });
- describe('with compress option and keepFileExt option', function () {
- var stream;
- before(function (done) {
- testTime = new Date(2012, 8, 12, 0, 10, 12);
- stream = new DateRollingFileStream(
- __dirname + '/compressedAndKeepExt.log',
- '.yyyy-MM-dd',
- {compress: true, keepFileExt: true},
- now
- );
- stream.write("First message\n", 'utf8', done);
- });
- 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);
- });
- it('should be two files, one compressed', function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('compressedAndKeepExt') > -1;
- }
- );
- logFiles.should.have.length(2);
- zlib.gunzip(
- fs.readFileSync(__dirname + '/compressedAndKeepExt.2012-09-12.log.gz'),
- function (err, contents) {
- contents.toString('utf8').should.eql('First message\n');
- fs.readFileSync(__dirname + '/compressedAndKeepExt.log', 'utf8')
- .should.eql('Second message\n');
- done(err);
- }
- );
- });
- });
- });
- after(function (done) {
- remove(
- __dirname + '/compressedAndKeepExt.log',
- function () {
- remove(__dirname + '/compressedAndKeepExt.log.2012-09-12.gz', done);
- }
- );
- });
- });
- describe('with daysToKeep option', function () {
- var stream;
- var daysToKeep = 4;
- var numOriginalLogs = 10;
- before(function (done) {
- var day = 0;
- var streams = [];
- async.whilst(
- function () {
- return day < numOriginalLogs;
- },
- function (nextCallback) {
- testTime = new Date(2012, 8, 20 - day, 0, 10, 12);
- var currentStream = new DateRollingFileStream(
- __dirname + '/daysToKeep.log',
- '.yyyy-MM-dd',
- {
- alwaysIncludePattern: true,
- daysToKeep: daysToKeep
- },
- now
- );
- async.waterfall([
- function (callback) {
- currentStream.write(util.format("Message on day %d\n", day), 'utf8', callback);
- },
- function (callback) {
- fs.utimes(currentStream.filename, testTime, testTime, callback);
- }
- ],
- function (err) {
- day++;
- streams.push(currentStream);
- nextCallback(err);
- });
- },
- function (err, n) {
- stream = streams[0];
- done(err);
- });
- describe('when the day changes', function () {
- before(function (done) {
- testTime = new Date(2012, 8, 21, 0, 10, 12);
- stream.write("Second message\n", 'utf8', done);
- });
- it('should be daysToKeep + 1 files left from numOriginalLogs', function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('daysToKeep.log') > -1;
- }
- );
- logFiles.should.have.length(daysToKeep + 1);
- done(err);
- });
- });
- });
- after(function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('daysToKeep.log') > -1;
- }
- );
- async.each(logFiles, function (logFile, nextCallback) {
- remove(__dirname + "/" + logFile, nextCallback);
- },
- function (err) {
- done(err);
- });
- });
- });
- });
- });
- describe('with daysToKeep and compress options', function () {
- var stream;
- var daysToKeep = 4;
- var numOriginalLogs = 10;
- before(function (done) {
- var day = 0;
- var streams = [];
- async.whilst(
- function () {
- return day < numOriginalLogs;
- },
- function (nextCallback) {
- testTime = new Date(2012, 8, 20 - day, 0, 10, 12);
- var currentStream = new DateRollingFileStream(
- __dirname + '/compressedDaysToKeep.log',
- '.yyyy-MM-dd',
- {
- alwaysIncludePattern: true,
- compress: true,
- daysToKeep: daysToKeep
- },
- now
- );
- async.waterfall([
- function (callback) {
- currentStream.write(util.format("Message on day %d\n", day), 'utf8', callback);
- },
- function (callback) {
- currentStream.compress(currentStream.filename, callback);
- },
- function (callback) {
- fs.utimes(currentStream.filename + ".gz", testTime, testTime, callback);
- }
- ],
- function (err) {
- day++;
- streams.push(currentStream);
- nextCallback(err);
- });
- },
- function (err, n) {
- // Uncompress the most recent stream which will be the one we roll over
- // for testing
- stream = streams[0];
- var compressedFilename = stream.filename + '.gz';
- var gzip = zlib.createGzip();
- var inp = fs.createReadStream(compressedFilename);
- var out = fs.createWriteStream(stream.filename);
- inp.pipe(gzip).pipe(out);
- out.on('finish', function (err) {
- fs.unlink(compressedFilename, done);
- });
- });
- });
- describe('when the day changes', function () {
- before(function (done) {
- testTime = new Date(2012, 8, 21, 0, 10, 12);
- stream.write("New file message\n", 'utf8', done);
- });
- it('should be 4 files left from original 3', function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('compressedDaysToKeep.log') > -1;
- }
- );
- logFiles.should.have.length(daysToKeep + 1);
- done(err);
- });
- });
- });
- after(function (done) {
- fs.readdir(__dirname, function (err, files) {
- var logFiles = files.filter(
- function (file) {
- return file.indexOf('compressedDaysToKeep.log') > -1;
- }
- );
- async.each(logFiles, function (logFile, nextCallback) {
- remove(__dirname + "/" + logFile, nextCallback);
- },
- function (err) {
- done(err);
- });
- });
- });
- });
- });
|