diff --git a/test/clusteredAppender-test.js b/test/clusteredAppender-test.js index c60fdea..34ba46f 100755 --- a/test/clusteredAppender-test.js +++ b/test/clusteredAppender-test.js @@ -123,6 +123,8 @@ describe('log4js in a cluster', function() { 'process': { 'send': function(event) { events.push(event); + }, + 'env': { } } } diff --git a/test/dateFileAppender-test.js b/test/dateFileAppender-test.js index 883b2f9..6ebf32b 100644 --- a/test/dateFileAppender-test.js +++ b/test/dateFileAppender-test.js @@ -199,7 +199,7 @@ describe('../lib/appenders/dateFile', function() { done(err); } ); - }, 100); + }, 200); } ); }); diff --git a/test/logger-test.js b/test/logger-test.js index 832a8cd..d2df5d0 100644 --- a/test/logger-test.js +++ b/test/logger-test.js @@ -1,5 +1,6 @@ "use strict"; var should = require('should') +, levels = require('../lib/levels') , Logger = require('../lib/logger'); describe('../lib/logger', function() { @@ -42,7 +43,7 @@ describe('../lib/logger', function() { it('should send log events to the dispatch delegate', function() { logger.debug("interesting thing"); event.should.have.property('category').equal('exciting category'); - event.should.have.property('level').equal('debug'); + event.should.have.property('level').equal(levels.DEBUG); event.should.have.property('data').eql(["interesting thing"]); event.should.have.property('startTime'); }); diff --git a/test/streams/BaseRollingFileStream-test.js b/test/streams/BaseRollingFileStream-test.js index a414d5a..daf4b11 100644 --- a/test/streams/BaseRollingFileStream-test.js +++ b/test/streams/BaseRollingFileStream-test.js @@ -1,12 +1,12 @@ "use strict"; -var vows = require('vows') -, assert = require('assert') +var should = require('should') , fs = require('fs') , sandbox = require('sandboxed-module'); -vows.describe('../../lib/streams/BaseRollingFileStream').addBatch({ - 'when node version < 0.10.0': { - topic: function() { +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', { @@ -22,16 +22,14 @@ vows.describe('../../lib/streams/BaseRollingFileStream').addBatch({ } } ); - return streamLib.required; - }, - 'it should use readable-stream to maintain compatibility': function(required) { - assert.ok(required['readable-stream']); - assert.ok(!required.stream); - } - }, - 'when node version > 0.10.0': { - topic: 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', { @@ -47,47 +45,42 @@ vows.describe('../../lib/streams/BaseRollingFileStream').addBatch({ } } ); - return streamLib.required; - }, - 'it should use the core stream module': function(required) { - assert.ok(required.stream); - assert.ok(!required['readable-stream']); - } - }, - 'when no filename is passed': { - topic: require('../../lib/streams/BaseRollingFileStream'), - 'it should throw an error': function(BaseRollingFileStream) { - try { + 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(); - assert.fail('should not get here'); - } catch (e) { - assert.ok(e); - } - } - }, + }).should.throw(); + }); + }); - 'default behaviour': { - topic: function() { - var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream') - , stream = new BaseRollingFileStream('basetest.log'); - return stream; - }, - teardown: function() { - try { - fs.unlink('basetest.log'); - } catch (e) { - console.error("could not remove basetest.log", e); - } - }, - 'it should not want to roll': function(stream) { - assert.isFalse(stream.shouldRoll()); - }, - 'it should not roll': function(stream) { + 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.be.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; }); - assert.isTrue(cbCalled); - } - } -}).exportTo(module); + cbCalled.should.be.true; + }); + }); +}); diff --git a/test/streams/DateRollingFileStream-test.js b/test/streams/DateRollingFileStream-test.js index 33f014b..92fe269 100644 --- a/test/streams/DateRollingFileStream-test.js +++ b/test/streams/DateRollingFileStream-test.js @@ -1,6 +1,5 @@ "use strict"; -var vows = require('vows') -, assert = require('assert') +var should = require('should') , fs = require('fs') , semver = require('semver') , streams @@ -14,214 +13,242 @@ if (semver.satisfies(process.version, '>=0.10.0')) { } DateRollingFileStream = require('../../lib/streams').DateRollingFileStream; -function cleanUp(filename) { - return function() { - fs.unlink(filename); - }; +function remove(filename, cb) { + fs.unlink(filename, function() { cb(); }); } function now() { return testTime.getTime(); } -vows.describe('DateRollingFileStream').addBatch({ - 'arguments': { - topic: new DateRollingFileStream( +describe('DateRollingFileStream', function() { + describe('arguments', function() { + var stream = new DateRollingFileStream( __dirname + '/test-date-rolling-file-stream-1', 'yyyy-mm-dd.hh' - ), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'), + ); + + after(function(done) { + remove(__dirname + '/test-date-rolling-file-stream-1', done); + }); - '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'); + 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'); - } - }, + }); + }); - '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'); - } - }, + describe('default arguments', function() { + var stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'); - 'with stream arguments': { - topic: new DateRollingFileStream( + 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) } - ), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'), - - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, parseInt('0666', 8)); - } - }, + ); - 'with stream arguments but no pattern': { - topic: new DateRollingFileStream( + + 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) } - ), - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'), - - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, parseInt('0666', 8)); - }, - '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, + 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', function() { - that.callback(null, stream); - }); - }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'), + stream.write("First message\n", 'utf8', done); + }); + + after(function(done) { + remove(__dirname + '/test-date-rolling-file-stream-5', done); + }); - '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"); - } - }, + 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); + }); + }); - 'when the day changes': { - topic: function(stream) { + describe('when the day changes', function() { + + before(function(done) { 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'), + 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); + }); + }); - '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 + 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); + } ); - } - }, + }); + }); - '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"); - } - } - } - }, - - 'with alwaysIncludePattern': { - topic: function() { - var that = this, + 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), stream = new DateRollingFileStream( __dirname + '/test-date-rolling-file-stream-pattern', '.yyyy-MM-dd', - {alwaysIncludePattern: true}, + { alwaysIncludePattern: true }, now ); - stream.write("First message\n", 'utf8', function() { - that.callback(null, stream); - }); - }, - teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'), + stream.write("First message\n", 'utf8', done); + }); + + after(function(done) { + remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', done); + }); - 'should create a file with the pattern set': { - topic: function(stream) { - fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback); - }, - 'file should contain first message': function(result) { - assert.equal(result.toString(), "First message\n"); - } - }, + 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); + } + ); + }); - 'when the day changes': { - topic: function(stream) { + describe('when the day changes', function() { + before(function(done) { 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-pattern.2012-09-13'), + stream.write("Second message\n", 'utf8', done); + }); + + after(function(done) { + remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', done); + }); - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be two': function(files) { - assert.equal( + 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; } - ).length, - 2 - ); - } - }, + ).should.have.length(2); + done(err); + }); + }); + }); - 'the file with the later date': { - topic: function() { + 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', - this.callback + __dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', 'utf8', + function(err, contents) { + contents.should.eql("Second message\n"); + done(err); + } ); - }, - 'should contain the second message': function(contents) { - assert.equal(contents.toString(), "Second message\n"); - } - }, + }); + }); - 'the file with the date': { - topic: function() { + 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', - this.callback + __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 'utf8', + function(err, contents) { + contents.should.eql("First message\n"); + done(err); + } ); - }, - 'should contain the first message': function(contents) { - assert.equal(contents.toString(), "First message\n"); - } - } - } - } - -}).exportTo(module); + }); + }); + }); + }); +}); diff --git a/test/streams/rollingFileStream-test.js b/test/streams/rollingFileStream-test.js index f39c2dc..eb3465d 100644 --- a/test/streams/rollingFileStream-test.js +++ b/test/streams/rollingFileStream-test.js @@ -1,8 +1,6 @@ "use strict"; -var vows = require('vows') -, async = require('async') -, assert = require('assert') -, events = require('events') +var async = require('async') +, should = require('should') , fs = require('fs') , semver = require('semver') , streams @@ -15,196 +13,252 @@ if (semver.satisfies(process.version, '>=0.10.0')) { } RollingFileStream = require('../../lib/streams').RollingFileStream; -function remove(filename) { - try { - fs.unlinkSync(filename); - } catch (e) { - //doesn't really matter if it failed - } +function remove(filename, cb) { + fs.unlink(filename, function() { cb(); }); } -function create(filename) { - fs.writeFileSync(filename, "test file"); +function create(filename, cb) { + fs.writeFile(filename, "test file", cb); } -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 (bytes), no. backups, return 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'); +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'); - } - }, - 'with stream arguments': { - topic: function() { - remove(__dirname + '/test-rolling-file-stream'); - return new RollingFileStream( + }); + }); + + 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) } ); - }, - 'should pass them to the underlying stream': function(stream) { - assert.equal(stream.theStream.mode, parseInt('0666', 8)); - } - }, - 'without size': { - topic: function() { - try { + 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"); - } 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); - } - }, - 'writing less than the file size': { - topic: function() { - remove(__dirname + "/test-rolling-file-stream-write-less"); - var that = this - , stream = new RollingFileStream( - __dirname + "/test-rolling-file-stream-write-less", - 100 - ); - 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"); - }, - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be one': function(files) { - assert.equal( - files.filter( - function(file) { - return file.indexOf('test-rolling-file-stream-write-less') > -1; - } - ).length, - 1 + }).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 ); - } - } - }, - 'writing more than the file size': { - topic: function() { - 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); - }, - function() { - stream.end(); - that.callback(); + 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); } ); - }, - 'the number of files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be two': function(files) { - assert.equal(files.filter( + }); + + 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; } - ).length, 2); - } - }, - 'the first file': { - topic: function() { - fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback); - }, - 'should contain the last two log messages': function(contents) { - assert.equal(contents, '5.cheese\n6.cheese\n'); - } - }, - 'the second file': { - topic: function() { - fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback); - }, - 'should contain the first five log messages': function(contents) { - assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n'); - } - } - }, - 'when many files already exist': { - topic: function() { - remove(__dirname + '/test-rolling-stream-with-existing-files.11'); - remove(__dirname + '/test-rolling-stream-with-existing-files.20'); - remove(__dirname + '/test-rolling-stream-with-existing-files.-1'); - remove(__dirname + '/test-rolling-stream-with-existing-files.1.1'); - remove(__dirname + '/test-rolling-stream-with-existing-files.1'); - + ).should.have.length(2); + done(err); + }); + }); - create(__dirname + '/test-rolling-stream-with-existing-files.11'); - create(__dirname + '/test-rolling-stream-with-existing-files.20'); - create(__dirname + '/test-rolling-stream-with-existing-files.-1'); - create(__dirname + '/test-rolling-stream-with-existing-files.1.1'); - create(__dirname + '/test-rolling-stream-with-existing-files.1'); + 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); + }); + }); - var that = this - , stream = new RollingFileStream( - __dirname + "/test-rolling-stream-with-existing-files", - 45, - 5 - ); - async.forEach( - [0, 1, 2, 3, 4, 5, 6], - function(i, cb) { - stream.write(i +".cheese\n", "utf8", cb); - }, - function() { - stream.end(); - that.callback(); + 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); } ); - }, - 'the files': { - topic: function() { - fs.readdir(__dirname, this.callback); - }, - 'should be rolled': function(files) { - assert.include(files, 'test-rolling-stream-with-existing-files'); - assert.include(files, 'test-rolling-stream-with-existing-files.1'); - assert.include(files, 'test-rolling-stream-with-existing-files.2'); - assert.include(files, 'test-rolling-stream-with-existing-files.11'); - assert.include(files, 'test-rolling-stream-with-existing-files.20'); - } - } - } -}).exportTo(module); + }); + }); + + 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); + }); + }); + }); +});