Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / readdirp / test / readdirp-stream.js
1 /*jshint asi:true */
2
3 var debug           //= true;
4 var test            = debug  ? function () {} : require('tap').test
5 var test_           = !debug ? function () {} : require('tap').test
6   , path            = require('path')
7   , fs              = require('fs')
8   , util            = require('util')
9   , TransformStream = require('readable-stream').Transform
10   , through         = require('through2')
11   , proxyquire      = require('proxyquire')
12   , streamapi       = require('../stream-api')
13   , readdirp        = require('..')
14   , root            = path.join(__dirname, 'bed')
15   , totalDirs       = 6
16   , totalFiles      = 12
17   , ext1Files       = 4
18   , ext2Files       = 3
19   , ext3Files       = 2
20   ;
21   
22 // see test/readdirp.js for test bed layout
23
24 function opts (extend) {
25   var o = { root: root };
26
27   if (extend) {
28     for (var prop in extend) {
29       o[prop] = extend[prop];
30     }
31   }
32   return o;
33 }
34
35 function capture () {
36   var result = { entries: [], errors: [], ended: false }
37     , dst = new TransformStream({ objectMode: true });
38
39   dst._transform = function (entry, _, cb) {
40     result.entries.push(entry);
41     cb();
42   }
43
44   dst._flush = function (cb) {
45     result.ended = true;
46     this.push(result); 
47     cb();
48   }
49
50   return dst;
51 }
52
53 test('\nintegrated', function (t) {
54   t.test('\n# reading root without filter', function (t) {
55     t.plan(2);
56
57     readdirp(opts())
58       .on('error', function (err) {
59         t.fail('should not throw error', err);
60       })
61       .pipe(capture())
62       .pipe(through.obj(
63         function (result, _ , cb) { 
64           t.equals(result.entries.length, totalFiles, 'emits all files');
65           t.ok(result.ended, 'ends stream');
66           t.end();
67           cb();
68         }
69       ));
70   })
71
72   t.test('\n# normal: ["*.ext1", "*.ext3"]', function (t) {
73     t.plan(2);
74
75     readdirp(opts( { fileFilter: [ '*.ext1', '*.ext3' ] } ))
76       .on('error', function (err) {
77         t.fail('should not throw error', err);
78       })
79       .pipe(capture())
80       .pipe(through.obj(
81         function (result, _ , cb) { 
82           t.equals(result.entries.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
83           t.ok(result.ended, 'ends stream');
84           t.end();
85           cb();
86         }
87       ))
88   })
89
90   t.test('\n# files only', function (t) {
91     t.plan(2);
92
93     readdirp(opts( { entryType: 'files' } ))
94       .on('error', function (err) {
95         t.fail('should not throw error', err);
96       })
97       .pipe(capture())
98       .pipe(through.obj(
99         function (result, _ , cb) { 
100           t.equals(result.entries.length, totalFiles, 'returned files');
101           t.ok(result.ended, 'ends stream');
102           t.end();
103           cb();
104         }
105       ))
106   })
107
108   t.test('\n# directories only', function (t) {
109     t.plan(2);
110
111     readdirp(opts( { entryType: 'directories' } ))
112       .on('error', function (err) {
113         t.fail('should not throw error', err);
114       })
115       .pipe(capture())
116       .pipe(through.obj(
117         function (result, _ , cb) { 
118           t.equals(result.entries.length, totalDirs, 'returned directories');
119           t.ok(result.ended, 'ends stream');
120           t.end();
121           cb();
122         }
123       ))
124   })
125
126   t.test('\n# both directories + files', function (t) {
127     t.plan(2);
128
129     readdirp(opts( { entryType: 'both' } ))
130       .on('error', function (err) {
131         t.fail('should not throw error', err);
132       })
133       .pipe(capture())
134       .pipe(through.obj(
135         function (result, _ , cb) { 
136           t.equals(result.entries.length, totalDirs + totalFiles, 'returned everything');
137           t.ok(result.ended, 'ends stream');
138           t.end();
139           cb();
140         }
141       ))
142   })
143
144   t.test('\n# directory filter with directories only', function (t) {
145     t.plan(2);
146
147     readdirp(opts( { entryType: 'directories', directoryFilter: [ 'root_dir1', '*dir1_subdir1' ] } ))
148       .on('error', function (err) {
149         t.fail('should not throw error', err);
150       })
151       .pipe(capture())
152       .pipe(through.obj(
153         function (result, _ , cb) {
154           t.equals(result.entries.length, 2, 'two directories');
155           t.ok(result.ended, 'ends stream');
156           t.end();
157           cb();
158         }
159       ))
160   })
161
162   t.test('\n# directory and file filters with both entries', function (t) {
163     t.plan(2);
164
165     readdirp(opts( { entryType: 'both', directoryFilter: [ 'root_dir1', '*dir1_subdir1' ], fileFilter: [ '!*.ext1' ] } ))
166       .on('error', function (err) {
167         t.fail('should not throw error', err);
168       })
169       .pipe(capture())
170       .pipe(through.obj(
171         function (result, _ , cb) {
172           t.equals(result.entries.length, 6, '2 directories and 4 files');
173           t.ok(result.ended, 'ends stream');
174           t.end();
175           cb();
176         }
177       ))
178   })
179
180   t.test('\n# negated: ["!*.ext1", "!*.ext3"]', function (t) {
181     t.plan(2);
182
183     readdirp(opts( { fileFilter: [ '!*.ext1', '!*.ext3' ] } ))
184       .on('error', function (err) {
185         t.fail('should not throw error', err);
186       })
187       .pipe(capture())
188       .pipe(through.obj(
189         function (result, _ , cb) { 
190           t.equals(result.entries.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
191           t.ok(result.ended, 'ends stream');
192           t.end();
193         }
194       ))
195   })
196
197   t.test('\n# no options given', function (t) {
198     t.plan(1);
199     readdirp()
200       .on('error', function (err) {
201         t.similar(err.toString() , /Need to pass at least one argument/ , 'emits meaningful error');
202         t.end();
203       })
204   })
205
206   t.test('\n# mixed: ["*.ext1", "!*.ext3"]', function (t) {
207     t.plan(1);
208
209     readdirp(opts( { fileFilter: [ '*.ext1', '!*.ext3' ] } ))
210       .on('error', function (err) {
211         t.similar(err.toString() , /Cannot mix negated with non negated glob filters/ , 'emits meaningful error');
212         t.end();
213       })
214   })
215 })
216
217
218 test('\napi separately', function (t) {
219
220   t.test('\n# handleError', function (t) {
221     t.plan(1);
222
223     var api = streamapi()
224       , warning = new Error('some file caused problems');
225
226     api.stream
227       .on('warn', function (err) {
228         t.equals(err, warning, 'warns with the handled error');
229       })
230     api.handleError(warning);
231   })
232
233   t.test('\n# when stream is paused and then resumed', function (t) {
234     t.plan(6);
235     var api = streamapi()
236       , resumed = false
237       , fatalError = new Error('fatal!')
238       , nonfatalError = new Error('nonfatal!')
239       , processedData = 'some data'
240       ;
241
242     api.stream
243       .on('warn', function (err) {
244         t.equals(err, nonfatalError, 'emits the buffered warning');
245         t.ok(resumed, 'emits warning only after it was resumed');
246       })
247       .on('error', function (err) {
248         t.equals(err, fatalError, 'emits the buffered fatal error');
249         t.ok(resumed, 'emits errors only after it was resumed');
250       })
251       .on('data', function (data) {
252         t.equals(data, processedData, 'emits the buffered data');
253         t.ok(resumed, 'emits data only after it was resumed');
254       })
255       .pause()
256     
257     api.processEntry(processedData);
258     api.handleError(nonfatalError);
259     api.handleFatalError(fatalError);
260   
261     setTimeout(function () {
262       resumed = true;
263       api.stream.resume();
264     }, 1)
265   })
266
267   t.test('\n# when a stream is paused it stops walking the fs', function (t) {
268     var resumed = false,
269       mockedAPI = streamapi();
270
271     mockedAPI.processEntry = function (entry) {
272       if (!resumed) t.notOk(true, 'should not emit while paused')
273       t.ok(entry, 'emitted while resumed')
274     }.bind(mockedAPI.stream)
275
276     function wrapper () {
277       return mockedAPI
278     }
279
280     var readdirp = proxyquire('../readdirp', {'./stream-api': wrapper})
281       , stream = readdirp(opts())
282       .on('error', function (err) {
283         t.fail('should not throw error', err);
284       })
285       .on('end', function () {
286         t.end()
287       })
288       .pause();
289
290     setTimeout(function () {
291       resumed = true;
292       stream.resume();
293     }, 5)
294   })
295
296   t.test('\n# when a stream is destroyed, it emits "closed", but no longer emits "data", "warn" and "error"', function (t) {
297     var api = streamapi()
298       , fatalError = new Error('fatal!')
299       , nonfatalError = new Error('nonfatal!')
300       , processedData = 'some data'
301       , plan = 0;
302
303     t.plan(6)
304     var stream = api.stream
305       .on('warn', function (err) {
306         t.ok(!stream._destroyed, 'emits warning until destroyed');
307       })
308       .on('error', function (err) {
309         t.ok(!stream._destroyed, 'emits errors until destroyed');
310       })
311       .on('data', function (data) {
312         t.ok(!stream._destroyed, 'emits data until destroyed');
313       })
314       .on('close', function () {
315         t.ok(stream._destroyed, 'emits close when stream is destroyed');
316       })
317     
318
319     api.processEntry(processedData);
320     api.handleError(nonfatalError);
321     api.handleFatalError(fatalError);
322
323     setTimeout(function () {
324       stream.destroy()
325
326       t.notOk(stream.readable, 'stream is no longer readable after it is destroyed')
327
328       api.processEntry(processedData);
329       api.handleError(nonfatalError);
330       api.handleFatalError(fatalError);
331
332       process.nextTick(function () {
333         t.pass('emits no more data, warn or error events after it was destroyed')  
334         t.end();
335       })
336     }, 10)
337   })
338 })