Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / streams / rollingFileStream-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , events = require('events')
5 , fs = require('fs')
6 , semver = require('semver')
7 , streams
8 , RollingFileStream;
9
10 if (semver.satisfies(process.version, '>=0.10.0')) {
11   streams = require('stream');
12 } else {
13   streams = require('readable-stream');
14 }
15 RollingFileStream = require('../../lib/streams').RollingFileStream;
16
17 function remove(filename) {
18   try {
19     fs.unlinkSync(filename);
20   } catch (e) {
21     //doesn't really matter if it failed
22   }
23 }
24
25 function create(filename) {
26   fs.writeFileSync(filename, "test file");
27 }
28
29 vows.describe('RollingFileStream').addBatch({
30   'arguments': {
31     topic: function() {
32       remove(__dirname + "/test-rolling-file-stream");
33       return new RollingFileStream("test-rolling-file-stream", 1024, 5);
34     },
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);
40     },
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');
46     }
47   },
48   'with stream arguments': {
49     topic: function() {
50       remove(__dirname + '/test-rolling-file-stream');
51       return new RollingFileStream(
52         'test-rolling-file-stream', 
53         1024, 
54         5, 
55         { mode: parseInt('0666', 8) }
56       );
57     },
58     'should pass them to the underlying stream': function(stream) {
59       assert.equal(stream.theStream.mode, parseInt('0666', 8));
60     }
61   },
62   'without size': {
63     topic: function() {
64       try {
65         new RollingFileStream(__dirname + "/test-rolling-file-stream");
66       } catch (e) {
67         return e;
68       }
69     },
70     'should throw an error': function(err) {
71       assert.instanceOf(err, Error);
72     }
73   },
74   'without number of backups': {
75     topic: function() {
76       remove('test-rolling-file-stream');
77       return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
78     },
79     'should default to 1 backup': function(stream) {
80       assert.equal(stream.backups, 1);
81     }
82   },
83   'writing less than the file size': {
84     topic: function() {
85       remove(__dirname + "/test-rolling-file-stream-write-less");
86       var that = this
87       , stream = new RollingFileStream(
88         __dirname + "/test-rolling-file-stream-write-less", 
89         100
90       );
91       stream.write("cheese", "utf8", function() {
92         stream.end();
93         fs.readFile(__dirname + "/test-rolling-file-stream-write-less", "utf8", that.callback);
94       });
95     },
96     'should write to the file': function(contents) {
97       assert.equal(contents, "cheese");
98     },
99     'the number of files': {
100       topic: function() {
101         fs.readdir(__dirname, this.callback);
102       },
103       'should be one': function(files) {
104         assert.equal(
105           files.filter(
106             function(file) { 
107               return file.indexOf('test-rolling-file-stream-write-less') > -1; 
108             }
109           ).length, 
110           1
111         );
112       }
113     }
114   },
115   'writing more than the file size': {
116     topic: function() {
117       remove(__dirname + "/test-rolling-file-stream-write-more");
118       remove(__dirname + "/test-rolling-file-stream-write-more.1");
119       var that = this
120       , stream = new RollingFileStream(
121         __dirname + "/test-rolling-file-stream-write-more",
122         45
123       );
124
125       write7Cheese(that, stream);
126     },
127     'the number of files': {
128       topic: function() {
129         fs.readdir(__dirname, this.callback);
130       },
131       'should be two': function(files) {
132         assert.equal(files.filter(
133           function(file) { 
134             return file.indexOf('test-rolling-file-stream-write-more') > -1; 
135           }
136         ).length, 2);
137       }
138     },
139     'the first file': {
140       topic: function() {
141         fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback);
142       },
143       'should contain the last two log messages': function(contents) {
144         assert.equal(contents, '5.cheese\n6.cheese\n');
145       }
146     },
147     'the second file': {
148       topic: function() {
149         fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback);
150       },
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');
153       }
154     }
155   },
156   'when many files already exist': {
157     topic: function() {
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');
163       
164
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');
170
171       var that = this
172       , stream = new RollingFileStream(
173         __dirname + "/test-rolling-stream-with-existing-files", 
174         45,
175         5
176       );
177
178       write7Cheese(that, stream);
179     },
180     'the files': {
181       topic: function() {
182         fs.readdir(__dirname, this.callback);
183       },
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');
190       }
191     }
192   }
193 }).exportTo(module);
194
195 function write7Cheese(that, stream) {
196   var streamed = 0;
197   [0, 1, 2, 3, 4, 5, 6].forEach(function(i) {
198     stream.write(i +".cheese\n", "utf8", function(e) {
199       streamed++;
200       if (e) { return that.callback(e); }
201       if (streamed === 7) {
202         stream.end();
203         that.callback();
204       }
205     });
206   });
207 }