Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / test / dateFileAppender-test.js
1 "use strict";
2 var vows = require('vows')
3 , assert = require('assert')
4 , path = require('path')
5 , fs = require('fs')
6 , sandbox = require('sandboxed-module')
7 , log4js = require('../lib/log4js')
8 , EOL = require('os').EOL || '\n';
9
10 function removeFile(filename) {
11   return function() {
12     fs.unlink(path.join(__dirname, filename), function(err) {
13       if (err) {
14         console.log("Could not delete ", filename, err);
15       }
16     });
17   };
18 }
19
20 vows.describe('../lib/appenders/dateFile').addBatch({
21   'appender': {
22     'adding multiple dateFileAppenders': {
23       topic: function () {
24         var listenersCount = process.listeners('exit').length,
25         dateFileAppender = require('../lib/appenders/dateFile'),
26         count = 5,
27         logfile;
28         
29         while (count--) {
30           logfile = path.join(__dirname, 'datefa-default-test' + count + '.log');
31           log4js.addAppender(dateFileAppender.appender(logfile));
32         }
33         
34         return listenersCount;
35       },
36       teardown: function() {
37         removeFile('datefa-default-test0.log')();
38         removeFile('datefa-default-test1.log')();
39         removeFile('datefa-default-test2.log')();
40         removeFile('datefa-default-test3.log')();
41         removeFile('datefa-default-test4.log')();
42       },
43       
44       'should only add one `exit` listener': function (initialCount) {
45         assert.equal(process.listeners('exit').length, initialCount + 1);
46       },
47
48     },
49
50     'exit listener': {
51       topic: function() {
52         var exitListener
53         , openedFiles = []
54         , dateFileAppender = sandbox.require(
55           '../lib/appenders/dateFile',
56           {
57             globals: {
58               process: {
59                 on: function(evt, listener) {
60                   exitListener = listener;
61                 }
62               }
63             },
64             requires: {
65               '../streams': {
66                 DateRollingFileStream: function(filename) {
67                   openedFiles.push(filename);
68
69                   this.end = function() {
70                     openedFiles.shift();
71                   };
72                 }
73               }
74             }   
75           }
76         );
77         for (var i=0; i < 5; i += 1) {
78           dateFileAppender.appender('test' + i);
79         }
80         assert.isNotEmpty(openedFiles);
81         exitListener();
82         return openedFiles;
83       },
84       'should close all open files': function(openedFiles) {
85         assert.isEmpty(openedFiles);
86       }
87     },
88     
89     'with default settings': {
90       topic: function() {
91         var that = this,
92         testFile = path.join(__dirname, 'date-appender-default.log'),
93         appender = require('../lib/appenders/dateFile').appender(testFile),
94         logger = log4js.getLogger('default-settings');
95         log4js.clearAppenders();
96         log4js.addAppender(appender, 'default-settings');
97         
98         logger.info("This should be in the file.");
99         
100         setTimeout(function() {
101           fs.readFile(testFile, "utf8", that.callback);
102         }, 100);
103         
104       },
105       teardown: removeFile('date-appender-default.log'),
106       
107       'should write to the file': function(contents) {
108         assert.include(contents, 'This should be in the file');
109       },
110       
111       'should use the basic layout': function(contents) {
112         assert.match(
113           contents, 
114             /\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\] \[INFO\] default-settings - /
115         );
116       }
117     }
118     
119   }
120 }).addBatch({
121   'configure': {
122     'with dateFileAppender': {
123       topic: function() {
124         var log4js = require('../lib/log4js')
125         , logger;
126         //this config file defines one file appender (to ./date-file-test.log)
127         //and sets the log level for "tests" to WARN
128         log4js.configure('test/with-dateFile.json');
129         logger = log4js.getLogger('tests');
130         logger.info('this should not be written to the file');
131         logger.warn('this should be written to the file');
132         
133         fs.readFile(path.join(__dirname, 'date-file-test.log'), 'utf8', this.callback);
134       },
135       teardown: removeFile('date-file-test.log'),
136       
137       'should load appender configuration from a json file': function(err, contents) {
138         if (err) {
139           throw err;
140         }
141         assert.include(contents, 'this should be written to the file' + EOL);
142         assert.equal(contents.indexOf('this should not be written to the file'), -1);
143       }
144     },
145     'with options.alwaysIncludePattern': {
146       topic: function() {
147         var self = this
148         , log4js = require('../lib/log4js')
149         , format = require('../lib/date_format')
150         , logger
151         , options = {
152           "appenders": [
153             {
154               "category": "tests", 
155               "type": "dateFile", 
156               "filename": "test/date-file-test",
157               "pattern": "-from-MM-dd.log",
158               "alwaysIncludePattern": true,
159               "layout": { 
160                 "type": "messagePassThrough" 
161               }
162             }
163           ]
164         }
165         , thisTime = format.asString(options.appenders[0].pattern, new Date());
166         fs.writeFileSync(
167           path.join(__dirname, 'date-file-test' + thisTime), 
168           "this is existing data" + EOL,
169           'utf8'
170         );
171         log4js.clearAppenders();
172         log4js.configure(options);
173         logger = log4js.getLogger('tests');
174         logger.warn('this should be written to the file with the appended date');
175         this.teardown = removeFile('date-file-test' + thisTime);
176         //wait for filesystem to catch up
177         setTimeout(function() {
178           fs.readFile(path.join(__dirname, 'date-file-test' + thisTime), 'utf8', self.callback);
179         }, 100);
180       },
181       'should create file with the correct pattern': function(contents) {
182         assert.include(contents, 'this should be written to the file with the appended date');
183       },
184       'should not overwrite the file on open (bug found in issue #132)': function(contents) {
185         assert.include(contents, 'this is existing data');
186       }
187     },
188     'with cwd option': {
189       topic: function () {
190         var fileOpened,
191         appender = sandbox.require(
192           '../lib/appenders/dateFile',
193           { requires:
194             { '../streams':
195               { DateRollingFileStream: 
196                 function(file) {
197                   fileOpened = file;
198                   return {
199                     on: function() {},
200                     end: function() {}
201                   };
202                 }
203               }
204             }
205           }
206         );
207         appender.configure(
208           { 
209             filename: "whatever.log", 
210             maxLogSize: 10 
211           }, 
212           { cwd: '/absolute/path/to' }
213         );
214         return fileOpened;
215       },
216       'should prepend options.cwd to config.filename': function (fileOpened) {
217         var expected = path.sep + path.join("absolute", "path", "to", "whatever.log");
218         assert.equal(fileOpened, expected);
219       }
220     }
221  
222   }
223 }).exportTo(module);