2 var vows = require('vows')
3 , assert = require('assert')
4 , events = require('events')
6 , semver = require('semver')
10 if (semver.satisfies(process.version, '>=0.10.0')) {
11 streams = require('stream');
13 streams = require('readable-stream');
15 RollingFileStream = require('../../lib/streams').RollingFileStream;
17 function remove(filename) {
19 fs.unlinkSync(filename);
21 //doesn't really matter if it failed
25 function create(filename) {
26 fs.writeFileSync(filename, "test file");
29 vows.describe('RollingFileStream').addBatch({
32 remove(__dirname + "/test-rolling-file-stream");
33 return new RollingFileStream("test-rolling-file-stream", 1024, 5);
35 'should take a filename, file size (bytes), no. backups, return Writable': function(stream) {
36 assert.instanceOf(stream, streams.Writable);
37 assert.equal(stream.filename, "test-rolling-file-stream");
38 assert.equal(stream.size, 1024);
39 assert.equal(stream.backups, 5);
41 'with default settings for the underlying stream': function(stream) {
42 assert.equal(stream.theStream.mode, 420);
43 assert.equal(stream.theStream.flags, 'a');
44 //encoding isn't a property on the underlying stream
45 //assert.equal(stream.theStream.encoding, 'utf8');
48 'with stream arguments': {
50 remove(__dirname + '/test-rolling-file-stream');
51 return new RollingFileStream(
52 'test-rolling-file-stream',
55 { mode: parseInt('0666', 8) }
58 'should pass them to the underlying stream': function(stream) {
59 assert.equal(stream.theStream.mode, parseInt('0666', 8));
65 new RollingFileStream(__dirname + "/test-rolling-file-stream");
70 'should throw an error': function(err) {
71 assert.instanceOf(err, Error);
74 'without number of backups': {
76 remove('test-rolling-file-stream');
77 return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
79 'should default to 1 backup': function(stream) {
80 assert.equal(stream.backups, 1);
83 'writing less than the file size': {
85 remove(__dirname + "/test-rolling-file-stream-write-less");
87 , stream = new RollingFileStream(
88 __dirname + "/test-rolling-file-stream-write-less",
91 stream.write("cheese", "utf8", function() {
93 fs.readFile(__dirname + "/test-rolling-file-stream-write-less", "utf8", that.callback);
96 'should write to the file': function(contents) {
97 assert.equal(contents, "cheese");
99 'the number of files': {
101 fs.readdir(__dirname, this.callback);
103 'should be one': function(files) {
107 return file.indexOf('test-rolling-file-stream-write-less') > -1;
115 'writing more than the file size': {
117 remove(__dirname + "/test-rolling-file-stream-write-more");
118 remove(__dirname + "/test-rolling-file-stream-write-more.1");
120 , stream = new RollingFileStream(
121 __dirname + "/test-rolling-file-stream-write-more",
125 write7Cheese(that, stream);
127 'the number of files': {
129 fs.readdir(__dirname, this.callback);
131 'should be two': function(files) {
132 assert.equal(files.filter(
134 return file.indexOf('test-rolling-file-stream-write-more') > -1;
141 fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback);
143 'should contain the last two log messages': function(contents) {
144 assert.equal(contents, '5.cheese\n6.cheese\n');
149 fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback);
151 'should contain the first five log messages': function(contents) {
152 assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
156 'when many files already exist': {
158 remove(__dirname + '/test-rolling-stream-with-existing-files.11');
159 remove(__dirname + '/test-rolling-stream-with-existing-files.20');
160 remove(__dirname + '/test-rolling-stream-with-existing-files.-1');
161 remove(__dirname + '/test-rolling-stream-with-existing-files.1.1');
162 remove(__dirname + '/test-rolling-stream-with-existing-files.1');
165 create(__dirname + '/test-rolling-stream-with-existing-files.11');
166 create(__dirname + '/test-rolling-stream-with-existing-files.20');
167 create(__dirname + '/test-rolling-stream-with-existing-files.-1');
168 create(__dirname + '/test-rolling-stream-with-existing-files.1.1');
169 create(__dirname + '/test-rolling-stream-with-existing-files.1');
172 , stream = new RollingFileStream(
173 __dirname + "/test-rolling-stream-with-existing-files",
178 write7Cheese(that, stream);
182 fs.readdir(__dirname, this.callback);
184 'should be rolled': function(files) {
185 assert.include(files, 'test-rolling-stream-with-existing-files');
186 assert.include(files, 'test-rolling-stream-with-existing-files.1');
187 assert.include(files, 'test-rolling-stream-with-existing-files.2');
188 assert.include(files, 'test-rolling-stream-with-existing-files.11');
189 assert.include(files, 'test-rolling-stream-with-existing-files.20');
195 function write7Cheese(that, stream) {
197 [0, 1, 2, 3, 4, 5, 6].forEach(function(i) {
198 stream.write(i +".cheese\n", "utf8", function(e) {
200 if (e) { return that.callback(e); }
201 if (streamed === 7) {