2 var vows = require('vows')
3 , assert = require('assert')
5 , semver = require('semver')
7 , DateRollingFileStream
8 , testTime = new Date(2012, 8, 12, 10, 37, 11);
10 if (semver.satisfies(process.version, '>=0.10.0')) {
11 streams = require('stream');
13 streams = require('readable-stream');
15 DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
17 function cleanUp(filename) {
24 return testTime.getTime();
27 vows.describe('DateRollingFileStream').addBatch({
29 topic: new DateRollingFileStream(
30 __dirname + '/test-date-rolling-file-stream-1',
33 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'),
35 'should take a filename and a pattern and return a WritableStream': function(stream) {
36 assert.equal(stream.filename, __dirname + '/test-date-rolling-file-stream-1');
37 assert.equal(stream.pattern, 'yyyy-mm-dd.hh');
38 assert.instanceOf(stream, streams.Writable);
40 'with default settings for the underlying stream': function(stream) {
41 assert.equal(stream.theStream.mode, 420);
42 assert.equal(stream.theStream.flags, 'a');
43 //encoding is not available on the underlying stream
44 //assert.equal(stream.encoding, 'utf8');
48 'default arguments': {
49 topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'),
50 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-2'),
52 'pattern should be .yyyy-MM-dd': function(stream) {
53 assert.equal(stream.pattern, '.yyyy-MM-dd');
57 'with stream arguments': {
58 topic: new DateRollingFileStream(
59 __dirname + '/test-date-rolling-file-stream-3',
61 { mode: parseInt('0666', 8) }
63 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'),
65 'should pass them to the underlying stream': function(stream) {
66 assert.equal(stream.theStream.mode, parseInt('0666', 8));
70 'with stream arguments but no pattern': {
71 topic: new DateRollingFileStream(
72 __dirname + '/test-date-rolling-file-stream-4',
73 { mode: parseInt('0666', 8) }
75 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'),
77 'should pass them to the underlying stream': function(stream) {
78 assert.equal(stream.theStream.mode, parseInt('0666', 8));
80 'should use default pattern': function(stream) {
81 assert.equal(stream.pattern, '.yyyy-MM-dd');
85 'with a pattern of .yyyy-MM-dd': {
88 stream = new DateRollingFileStream(
89 __dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd',
93 stream.write("First message\n", 'utf8', function() {
94 that.callback(null, stream);
97 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'),
99 'should create a file with the base name': {
100 topic: function(stream) {
101 fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
103 'file should contain first message': function(result) {
104 assert.equal(result.toString(), "First message\n");
108 'when the day changes': {
109 topic: function(stream) {
110 testTime = new Date(2012, 8, 13, 0, 10, 12);
111 stream.write("Second message\n", 'utf8', this.callback);
113 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5.2012-09-12'),
116 'the number of files': {
118 fs.readdir(__dirname, this.callback);
120 'should be two': function(files) {
124 return file.indexOf('test-date-rolling-file-stream-5') > -1;
132 'the file without a date': {
134 fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
136 'should contain the second message': function(contents) {
137 assert.equal(contents.toString(), "Second message\n");
141 'the file with the date': {
143 fs.readFile(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', this.callback);
145 'should contain the first message': function(contents) {
146 assert.equal(contents.toString(), "First message\n");
152 'with alwaysIncludePattern': {
155 testTime = new Date(2012, 8, 12, 0, 10, 12),
156 stream = new DateRollingFileStream(
157 __dirname + '/test-date-rolling-file-stream-pattern',
159 {alwaysIncludePattern: true},
162 stream.write("First message\n", 'utf8', function() {
163 that.callback(null, stream);
166 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'),
168 'should create a file with the pattern set': {
169 topic: function(stream) {
170 fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback);
172 'file should contain first message': function(result) {
173 assert.equal(result.toString(), "First message\n");
177 'when the day changes': {
178 topic: function(stream) {
179 testTime = new Date(2012, 8, 13, 0, 10, 12);
180 stream.write("Second message\n", 'utf8', this.callback);
182 teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'),
185 'the number of files': {
187 fs.readdir(__dirname, this.callback);
189 'should be two': function(files) {
193 return file.indexOf('test-date-rolling-file-stream-pattern') > -1;
201 'the file with the later date': {
204 __dirname + '/test-date-rolling-file-stream-pattern.2012-09-13',
208 'should contain the second message': function(contents) {
209 assert.equal(contents.toString(), "Second message\n");
213 'the file with the date': {
216 __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12',
220 'should contain the first message': function(contents) {
221 assert.equal(contents.toString(), "First message\n");