2013-05-25 12:00:06 +08:00
|
|
|
"use strict";
|
2013-03-20 06:14:27 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, fs = require('fs')
|
2013-04-11 19:45:16 +08:00
|
|
|
, semver = require('semver')
|
|
|
|
, streams
|
|
|
|
, DateRollingFileStream
|
2013-03-20 06:14:27 +08:00
|
|
|
, testTime = new Date(2012, 8, 12, 10, 37, 11);
|
2012-09-18 06:46:39 +08:00
|
|
|
|
2013-04-11 19:45:16 +08:00
|
|
|
if (semver.satisfies(process.version, '>=0.10.0')) {
|
|
|
|
streams = require('stream');
|
|
|
|
} else {
|
|
|
|
streams = require('readable-stream');
|
|
|
|
}
|
2013-05-25 12:00:06 +08:00
|
|
|
DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
|
2013-04-11 19:45:16 +08:00
|
|
|
|
2012-09-18 06:46:39 +08:00
|
|
|
function cleanUp(filename) {
|
2013-05-25 12:00:06 +08:00
|
|
|
return function() {
|
|
|
|
fs.unlink(filename);
|
|
|
|
};
|
2012-09-18 06:46:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function now() {
|
2013-05-25 12:00:06 +08:00
|
|
|
return testTime.getTime();
|
2012-09-18 06:46:39 +08:00
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
|
|
|
vows.describe('DateRollingFileStream').addBatch({
|
2013-05-25 12:00:06 +08:00
|
|
|
'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);
|
2012-09-05 08:58:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'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');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'with stream arguments': {
|
|
|
|
topic: 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(
|
|
|
|
__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));
|
2012-09-05 08:58:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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");
|
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
},
|
|
|
|
|
2013-05-25 12:00:06 +08:00
|
|
|
'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'),
|
2012-09-05 08:58:28 +08:00
|
|
|
|
2013-05-25 12:00:06 +08:00
|
|
|
|
|
|
|
'the number of files': {
|
|
|
|
topic: function() {
|
|
|
|
fs.readdir(__dirname, this.callback);
|
2012-09-05 08:58:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'should be two': function(files) {
|
|
|
|
assert.equal(
|
|
|
|
files.filter(
|
|
|
|
function(file) {
|
|
|
|
return file.indexOf('test-date-rolling-file-stream-5') > -1;
|
|
|
|
}
|
|
|
|
).length,
|
|
|
|
2
|
|
|
|
);
|
2012-09-18 06:46:39 +08:00
|
|
|
}
|
2013-05-25 12:00:06 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
'the file without a date': {
|
2012-09-18 06:46:39 +08:00
|
|
|
topic: function() {
|
2013-05-25 12:00:06 +08:00
|
|
|
fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
|
2012-09-18 06:46:39 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'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);
|
2012-09-18 06:46:39 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'should contain the first message': function(contents) {
|
|
|
|
assert.equal(contents.toString(), "First message\n");
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
2013-05-25 12:00:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'with alwaysIncludePattern': {
|
|
|
|
topic: function() {
|
|
|
|
var that = this,
|
|
|
|
testTime = new Date(2012, 8, 12, 0, 10, 12),
|
|
|
|
stream = new DateRollingFileStream(
|
|
|
|
__dirname + '/test-date-rolling-file-stream-pattern',
|
|
|
|
'.yyyy-MM-dd',
|
|
|
|
{alwaysIncludePattern: true},
|
|
|
|
now
|
|
|
|
);
|
|
|
|
stream.write("First message\n", 'utf8', function() {
|
|
|
|
that.callback(null, stream);
|
|
|
|
});
|
2013-05-12 04:01:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'),
|
|
|
|
|
|
|
|
'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");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'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-pattern.2012-09-13'),
|
|
|
|
|
|
|
|
|
|
|
|
'the number of files': {
|
2013-05-12 04:01:28 +08:00
|
|
|
topic: function() {
|
2013-05-25 12:00:06 +08:00
|
|
|
fs.readdir(__dirname, this.callback);
|
2013-05-12 04:01:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'should be two': function(files) {
|
|
|
|
assert.equal(
|
|
|
|
files.filter(
|
|
|
|
function(file) {
|
|
|
|
return file.indexOf('test-date-rolling-file-stream-pattern') > -1;
|
|
|
|
}
|
|
|
|
).length,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'the file with the later date': {
|
|
|
|
topic: function() {
|
|
|
|
fs.readFile(
|
|
|
|
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13',
|
|
|
|
this.callback
|
|
|
|
);
|
2013-05-12 04:01:28 +08:00
|
|
|
},
|
2013-05-25 12:00:06 +08:00
|
|
|
'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-pattern.2012-09-12',
|
|
|
|
this.callback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
'should contain the first message': function(contents) {
|
|
|
|
assert.equal(contents.toString(), "First message\n");
|
2013-05-12 04:01:28 +08:00
|
|
|
}
|
2013-05-25 12:00:06 +08:00
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
}
|
2013-05-25 12:00:06 +08:00
|
|
|
}
|
2012-09-05 08:58:28 +08:00
|
|
|
|
|
|
|
}).exportTo(module);
|