all tests converted to mocha

This commit is contained in:
Gareth Jones
2013-08-29 08:49:42 +10:00
parent fe1f1228ca
commit 46ad57b4e0
6 changed files with 466 additions and 389 deletions

View File

@@ -123,6 +123,8 @@ describe('log4js in a cluster', function() {
'process': { 'process': {
'send': function(event) { 'send': function(event) {
events.push(event); events.push(event);
},
'env': {
} }
} }
} }

View File

@@ -199,7 +199,7 @@ describe('../lib/appenders/dateFile', function() {
done(err); done(err);
} }
); );
}, 100); }, 200);
} }
); );
}); });

View File

@@ -1,5 +1,6 @@
"use strict"; "use strict";
var should = require('should') var should = require('should')
, levels = require('../lib/levels')
, Logger = require('../lib/logger'); , Logger = require('../lib/logger');
describe('../lib/logger', function() { describe('../lib/logger', function() {
@@ -42,7 +43,7 @@ describe('../lib/logger', function() {
it('should send log events to the dispatch delegate', function() { it('should send log events to the dispatch delegate', function() {
logger.debug("interesting thing"); logger.debug("interesting thing");
event.should.have.property('category').equal('exciting category'); 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('data').eql(["interesting thing"]);
event.should.have.property('startTime'); event.should.have.property('startTime');
}); });

View File

@@ -1,12 +1,12 @@
"use strict"; "use strict";
var vows = require('vows') var should = require('should')
, assert = require('assert')
, fs = require('fs') , fs = require('fs')
, sandbox = require('sandboxed-module'); , sandbox = require('sandboxed-module');
vows.describe('../../lib/streams/BaseRollingFileStream').addBatch({ describe('../../lib/streams/BaseRollingFileStream', function() {
'when node version < 0.10.0': { describe('when node version < 0.10.0', function() {
topic: function() { it('should use readable-stream to maintain compatibility', function() {
var streamLib = sandbox.load( var streamLib = sandbox.load(
'../../lib/streams/BaseRollingFileStream', '../../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': { streamLib.required.should.have.property('readable-stream');
topic: function() { 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( var streamLib = sandbox.load(
'../../lib/streams/BaseRollingFileStream', '../../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': { streamLib.required.should.have.property('stream');
topic: require('../../lib/streams/BaseRollingFileStream'), streamLib.required.should.not.have.property('readable-stream');
'it should throw an error': function(BaseRollingFileStream) { });
try { });
describe('when no filename is passed', function() {
it('should throw an error', function() {
var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream');
(function() {
new BaseRollingFileStream(); new BaseRollingFileStream();
assert.fail('should not get here'); }).should.throw();
} catch (e) { });
assert.ok(e); });
}
}
},
'default behaviour': { describe('default behaviour', function() {
topic: function() { var stream;
var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream')
, stream = new BaseRollingFileStream('basetest.log'); before(function() {
return stream; var BaseRollingFileStream = require('../../lib/streams/BaseRollingFileStream');
}, stream = new BaseRollingFileStream('basetest.log');
teardown: function() { });
try {
fs.unlink('basetest.log'); after(function(done) {
} catch (e) { fs.unlink('basetest.log', done);
console.error("could not remove basetest.log", e); });
}
}, it('should not want to roll', function() {
'it should not want to roll': function(stream) { stream.shouldRoll().should.be.false;
assert.isFalse(stream.shouldRoll()); });
},
'it should not roll': function(stream) { it('should not roll', function() {
var cbCalled = false; var cbCalled = false;
//just calls the callback straight away, no async calls //just calls the callback straight away, no async calls
stream.roll('basetest.log', function() { cbCalled = true; }); stream.roll('basetest.log', function() { cbCalled = true; });
assert.isTrue(cbCalled); cbCalled.should.be.true;
} });
} });
}).exportTo(module); });

View File

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

View File

@@ -1,8 +1,6 @@
"use strict"; "use strict";
var vows = require('vows') var async = require('async')
, async = require('async') , should = require('should')
, assert = require('assert')
, events = require('events')
, fs = require('fs') , fs = require('fs')
, semver = require('semver') , semver = require('semver')
, streams , streams
@@ -15,196 +13,252 @@ if (semver.satisfies(process.version, '>=0.10.0')) {
} }
RollingFileStream = require('../../lib/streams').RollingFileStream; RollingFileStream = require('../../lib/streams').RollingFileStream;
function remove(filename) { function remove(filename, cb) {
try { fs.unlink(filename, function() { cb(); });
fs.unlinkSync(filename);
} catch (e) {
//doesn't really matter if it failed
}
} }
function create(filename) { function create(filename, cb) {
fs.writeFileSync(filename, "test file"); fs.writeFile(filename, "test file", cb);
} }
vows.describe('RollingFileStream').addBatch({ describe('RollingFileStream', function() {
'arguments': {
topic: function() { describe('arguments', function() {
remove(__dirname + "/test-rolling-file-stream"); var stream;
return new RollingFileStream("test-rolling-file-stream", 1024, 5);
}, before(function(done) {
'should take a filename, file size (bytes), no. backups, return Writable': function(stream) { remove(__dirname + "/test-rolling-file-stream", function() {
assert.instanceOf(stream, streams.Writable); stream = new RollingFileStream("test-rolling-file-stream", 1024, 5);
assert.equal(stream.filename, "test-rolling-file-stream"); done();
assert.equal(stream.size, 1024); });
assert.equal(stream.backups, 5); });
},
'with default settings for the underlying stream': function(stream) { after(function(done) {
assert.equal(stream.theStream.mode, 420); remove(__dirname + "/test-rolling-file-stream", done);
assert.equal(stream.theStream.flags, 'a'); });
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 //encoding isn't a property on the underlying stream
//assert.equal(stream.theStream.encoding, 'utf8'); //assert.equal(stream.theStream.encoding, 'utf8');
} });
}, });
'with stream arguments': {
topic: function() { describe('with stream arguments', function() {
remove(__dirname + '/test-rolling-file-stream'); it('should pass them to the underlying stream', function() {
return new RollingFileStream( var stream = new RollingFileStream(
'test-rolling-file-stream', 'test-rolling-file-stream',
1024, 1024,
5, 5,
{ mode: parseInt('0666', 8) } { mode: parseInt('0666', 8) }
); );
}, stream.theStream.mode.should.eql(parseInt('0666', 8));
'should pass them to the underlying stream': function(stream) { });
assert.equal(stream.theStream.mode, parseInt('0666', 8));
} after(function(done) {
}, remove(__dirname + '/test-rolling-file-stream', done);
'without size': { });
topic: function() { });
try {
describe('without size', function() {
it('should throw an error', function() {
(function() {
new RollingFileStream(__dirname + "/test-rolling-file-stream"); new RollingFileStream(__dirname + "/test-rolling-file-stream");
} catch (e) { }).should.throw();
return e; });
} });
},
'should throw an error': function(err) { describe('without number of backups', function() {
assert.instanceOf(err, Error); it('should default to 1 backup', function() {
} var stream = new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
}, stream.backups.should.eql(1);
'without number of backups': { });
topic: function() {
remove('test-rolling-file-stream'); after(function(done) {
return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024); remove(__dirname + "/test-rolling-file-stream", done);
}, });
'should default to 1 backup': function(stream) { });
assert.equal(stream.backups, 1);
} describe('writing less than the file size', function() {
},
'writing less than the file size': { before(function(done) {
topic: function() { remove(__dirname + "/test-rolling-file-stream-write-less", function() {
remove(__dirname + "/test-rolling-file-stream-write-less"); var stream = new RollingFileStream(
var that = this __dirname + "/test-rolling-file-stream-write-less",
, stream = new RollingFileStream( 100
__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
); );
} stream.write("cheese", "utf8", function() {
} stream.end(done);
}, });
'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"); after(function(done) {
var that = this remove(__dirname + "/test-rolling-file-stream-write-less", done);
, stream = new RollingFileStream( });
__dirname + "/test-rolling-file-stream-write-more",
45 it('should write to the file', function(done) {
); fs.readFile(
async.forEach( __dirname + "/test-rolling-file-stream-write-less", "utf8",
[0, 1, 2, 3, 4, 5, 6], function(err, contents) {
function(i, cb) { contents.should.eql("cheese");
stream.write(i +".cheese\n", "utf8", cb); done(err);
},
function() {
stream.end();
that.callback();
} }
); );
}, });
'the number of files': {
topic: function() { it('should write one file', function(done) {
fs.readdir(__dirname, this.callback); fs.readdir(__dirname, function(err, files) {
}, files.filter(
'should be two': function(files) { function(file) { return file.indexOf('test-rolling-file-stream-write-less') > -1; }
assert.equal(files.filter( ).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) { function(file) {
return file.indexOf('test-rolling-file-stream-write-more') > -1; return file.indexOf('test-rolling-file-stream-write-more') > -1;
} }
).length, 2); ).should.have.length(2);
} done(err);
}, });
'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');
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);
});
});
create(__dirname + '/test-rolling-stream-with-existing-files.11'); it('should write the first five log messages to the second file', function(done) {
create(__dirname + '/test-rolling-stream-with-existing-files.20'); fs.readFile(
create(__dirname + '/test-rolling-stream-with-existing-files.-1'); __dirname + '/test-rolling-file-stream-write-more.1', "utf8",
create(__dirname + '/test-rolling-stream-with-existing-files.1.1'); function(err, contents) {
create(__dirname + '/test-rolling-stream-with-existing-files.1'); contents.should.eql('0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.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();
} }
); );
}, });
'the files': { });
topic: function() {
fs.readdir(__dirname, this.callback); describe('when many files already exist', function() {
}, before(function(done) {
'should be rolled': function(files) { async.forEach(
assert.include(files, 'test-rolling-stream-with-existing-files'); [
assert.include(files, 'test-rolling-stream-with-existing-files.1'); __dirname + '/test-rolling-stream-with-existing-files.11',
assert.include(files, 'test-rolling-stream-with-existing-files.2'); __dirname + '/test-rolling-stream-with-existing-files.20',
assert.include(files, 'test-rolling-stream-with-existing-files.11'); __dirname + '/test-rolling-stream-with-existing-files.-1',
assert.include(files, 'test-rolling-stream-with-existing-files.20'); __dirname + '/test-rolling-stream-with-existing-files.1.1',
} __dirname + '/test-rolling-stream-with-existing-files.1'
} ],
} remove,
}).exportTo(module); 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);
});
});
});
});