Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / readdirp / test / readdirp.js
1 /*jshint asi:true */
2
3 var test     = require('tap').test
4   , path     = require('path')
5   , fs       = require('fs')
6   , util     = require('util')
7   , net      = require('net')
8   , readdirp = require('../readdirp.js')
9   , root     = path.join(__dirname, '../test/bed')
10   , totalDirs          =  6
11   , totalFiles         =  12
12   , ext1Files          =  4
13   , ext2Files          =  3
14   , ext3Files          =  2
15   , rootDir2Files      =  2
16   , nameHasLength9Dirs =  2
17   , depth1Files        =  8
18   , depth0Files        =  3
19   ;
20
21 /*
22 Structure of test bed:
23     .
24     ├── root_dir1
25     │   ├── root_dir1_file1.ext1
26     │   ├── root_dir1_file2.ext2
27     │   ├── root_dir1_file3.ext3
28     │   ├── root_dir1_subdir1
29     │   │   └── root1_dir1_subdir1_file1.ext1
30     │   └── root_dir1_subdir2
31     │       └── .gitignore
32     ├── root_dir2
33     │   ├── root_dir2_file1.ext1
34     │   ├── root_dir2_file2.ext2
35     │   ├── root_dir2_subdir1
36     │   │   └── .gitignore
37     │   └── root_dir2_subdir2
38     │       └── .gitignore
39     ├── root_file1.ext1
40     ├── root_file2.ext2
41     └── root_file3.ext3
42
43     6 directories, 13 files
44 */
45
46 // console.log('\033[2J'); // clear console
47
48 function opts (extend) {
49   var o = { root: root };
50
51   if (extend) {
52     for (var prop in extend) {
53       o[prop] = extend[prop];
54     }
55   }
56   return o;
57 }
58
59 test('\nreading root without filter', function (t) {
60   t.plan(2);
61   readdirp(opts(), function (err, res) {
62     t.equals(res.directories.length, totalDirs, 'all directories');
63     t.equals(res.files.length, totalFiles, 'all files');
64     t.end();
65   })
66 })
67
68 test('\nreading root without filter using lstat', function (t) {
69   t.plan(2);
70   readdirp(opts({ lstat: true }), function (err, res) {
71     t.equals(res.directories.length, totalDirs, 'all directories');
72     t.equals(res.files.length, totalFiles, 'all files');
73     t.end();
74   })
75 })
76
77 test('\nreading root with symlinks using lstat', function (t) {
78   t.plan(2);
79   fs.symlinkSync(path.join(root, 'root_dir1'), path.join(root, 'dirlink'));
80   fs.symlinkSync(path.join(root, 'root_file1.ext1'), path.join(root, 'link.ext1'));
81   readdirp(opts({ lstat: true }), function (err, res) {
82     t.equals(res.directories.length, totalDirs, 'all directories');
83     t.equals(res.files.length, totalFiles + 2, 'all files + symlinks');
84     fs.unlinkSync(path.join(root, 'dirlink'));
85     fs.unlinkSync(path.join(root, 'link.ext1'));
86     t.end();
87   })
88 })
89
90 test('\nreading non-standard fds', function (t) {
91   t.plan(2);
92   var server = net.createServer().listen(path.join(root, 'test.sock'), function(){
93     readdirp(opts({ entryType: 'all' }), function (err, res) {
94       t.equals(res.files.length, totalFiles + 1, 'all files + socket');
95       readdirp(opts({ entryType: 'both' }), function (err, res) {
96         t.equals(res.files.length, totalFiles, 'all regular files only');
97         server.close();
98         t.end();
99       })
100     })
101   });
102 })
103
104 test('\nreading root using glob filter', function (t) {
105   // normal
106   t.test('\n# "*.ext1"', function (t) {
107     t.plan(1);
108     readdirp(opts( { fileFilter: '*.ext1' } ), function (err, res) {
109       t.equals(res.files.length, ext1Files, 'all ext1 files');
110       t.end();
111     })
112   })
113   t.test('\n# ["*.ext1", "*.ext3"]', function (t) {
114     t.plan(1);
115     readdirp(opts( { fileFilter: [ '*.ext1', '*.ext3' ] } ), function (err, res) {
116       t.equals(res.files.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
117       t.end();
118     })
119   })
120   t.test('\n# "root_dir1"', function (t) {
121     t.plan(1);
122     readdirp(opts( { directoryFilter: 'root_dir1' }), function (err, res) {
123       t.equals(res.directories.length, 1, 'one directory');
124       t.end();
125     })
126   })
127   t.test('\n# ["root_dir1", "*dir1_subdir1"]', function (t) {
128     t.plan(1);
129     readdirp(opts( { directoryFilter: [ 'root_dir1', '*dir1_subdir1' ]}), function (err, res) {
130       t.equals(res.directories.length, 2, 'two directories');
131       t.end();
132     })
133   })
134
135   t.test('\n# negated: "!*.ext1"', function (t) {
136     t.plan(1);
137     readdirp(opts( { fileFilter: '!*.ext1' } ), function (err, res) {
138       t.equals(res.files.length, totalFiles - ext1Files, 'all but ext1 files');
139       t.end();
140     })
141   })
142   t.test('\n# negated: ["!*.ext1", "!*.ext3"]', function (t) {
143     t.plan(1);
144     readdirp(opts( { fileFilter: [ '!*.ext1', '!*.ext3' ] } ), function (err, res) {
145       t.equals(res.files.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
146       t.end();
147     })
148   })
149
150   t.test('\n# mixed: ["*.ext1", "!*.ext3"]', function (t) {
151     t.plan(1);
152     readdirp(opts( { fileFilter: [ '*.ext1', '!*.ext3' ] } ), function (err, res) {
153       t.similar(err[0].toString(), /Cannot mix negated with non negated glob filters/, 'returns meaningfull error');
154       t.end();
155     })
156   })
157
158   t.test('\n# leading and trailing spaces: [" *.ext1", "*.ext3 "]', function (t) {
159     t.plan(1);
160     readdirp(opts( { fileFilter: [ ' *.ext1', '*.ext3 ' ] } ), function (err, res) {
161       t.equals(res.files.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
162       t.end();
163     })
164   })
165   t.test('\n# leading and trailing spaces: [" !*.ext1", " !*.ext3 "]', function (t) {
166     t.plan(1);
167     readdirp(opts( { fileFilter: [ ' !*.ext1', ' !*.ext3' ] } ), function (err, res) {
168       t.equals(res.files.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
169       t.end();
170     })
171   })
172
173   t.test('\n# ** glob pattern', function (t) {
174     t.plan(1);
175     readdirp(opts( { fileFilter: '**/*.ext1' } ), function (err, res) {
176       t.equals(res.files.length, ext1Files, 'ignores ** in **/*.ext1 -> only *.ext1 files');
177       t.end();
178     })
179   })
180 })
181
182 test('\n\nreading root using function filter', function (t) {
183   t.test('\n# file filter -> "contains root_dir2"', function (t) {
184     t.plan(1);
185     readdirp(
186         opts( { fileFilter: function (fi) { return fi.name.indexOf('root_dir2') >= 0; } })
187       , function (err, res) {
188           t.equals(res.files.length, rootDir2Files, 'all rootDir2Files');
189           t.end();
190       }
191     )
192   })
193
194   t.test('\n# directory filter -> "name has length 9"', function (t) {
195     t.plan(1);
196     readdirp(
197         opts( { directoryFilter: function (di) { return di.name.length === 9; } })
198       , function (err, res) {
199           t.equals(res.directories.length, nameHasLength9Dirs, 'all all dirs with name length 9');
200           t.end();
201       }
202     )
203   })
204 })
205
206 test('\nreading root specifying maximum depth', function (t) {
207   t.test('\n# depth 1', function (t) {
208     t.plan(1);
209       readdirp(opts( { depth: 1 } ), function (err, res) {
210         t.equals(res.files.length, depth1Files, 'does not return files at depth 2');
211       })
212   })
213 })
214
215 test('\nreading root with no recursion', function (t) {
216   t.test('\n# depth 0', function (t) {
217     t.plan(1);
218       readdirp(opts( { depth: 0 } ), function (err, res) {
219         t.equals(res.files.length, depth0Files, 'does not return files at depth 0');
220       })
221   })
222 })
223
224 test('\nprogress callbacks', function (t) {
225   t.plan(2);
226
227   var pluckName = function(fi) { return fi.name; }
228     , processedFiles = [];
229
230   readdirp(
231       opts()
232     , function(fi) {
233         processedFiles.push(fi);
234       }
235     , function (err, res) {
236         t.equals(processedFiles.length, res.files.length, 'calls back for each file processed');
237         t.deepEquals(processedFiles.map(pluckName).sort(),res.files.map(pluckName).sort(), 'same file names');
238         t.end();
239       }
240   )
241 })
242
243 test('resolving of name, full and relative paths', function (t) {
244   var expected = {
245         name          :  'root_dir1_file1.ext1'
246       , parentDirName :  'root_dir1'
247       , path          :  'root_dir1/root_dir1_file1.ext1'
248       , fullPath      :  'test/bed/root_dir1/root_dir1_file1.ext1'
249       }
250     , opts = [
251         { root: './bed'          ,  prefix: ''     }
252       , { root: './bed/'         ,  prefix: ''     }
253       , { root: 'bed'            ,  prefix: ''     }
254       , { root: 'bed/'           ,  prefix: ''     }
255       , { root: '../test/bed/'   ,  prefix: ''     }
256       , { root: '.'              ,  prefix: 'bed'  }
257     ]
258   t.plan(opts.length);
259
260   opts.forEach(function (op) {
261     op.fileFilter = 'root_dir1_file1.ext1';
262
263     t.test('\n' + util.inspect(op), function (t) {
264       t.plan(4);
265
266       readdirp (op, function(err, res) {
267         t.equals(res.files[0].name, expected.name, 'correct name');
268         t.equals(res.files[0].path, path.join(op.prefix, expected.path), 'correct path');
269       })
270
271       fs.realpath(op.root, function(err, fullRoot) {
272         readdirp (op, function(err, res) {
273           t.equals(
274               res.files[0].fullParentDir
275             , path.join(fullRoot, op.prefix, expected.parentDirName)
276             , 'correct parentDir'
277           );
278           t.equals(
279               res.files[0].fullPath
280             , path.join(fullRoot, op.prefix, expected.parentDirName, expected.name)
281             , 'correct fullPath'
282           );
283         })
284       })
285     })
286   })
287 })
288
289