3 var test = require('tap').test
4 , path = require('path')
6 , util = require('util')
8 , readdirp = require('../readdirp.js')
9 , root = path.join(__dirname, '../test/bed')
16 , nameHasLength9Dirs = 2
22 Structure of test bed:
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
33 │ ├── root_dir2_file1.ext1
34 │ ├── root_dir2_file2.ext2
35 │ ├── root_dir2_subdir1
37 │ └── root_dir2_subdir2
43 6 directories, 13 files
46 // console.log('\033[2J'); // clear console
48 function opts (extend) {
49 var o = { root: root };
52 for (var prop in extend) {
53 o[prop] = extend[prop];
59 test('\nreading root without filter', function (t) {
61 readdirp(opts(), function (err, res) {
62 t.equals(res.directories.length, totalDirs, 'all directories');
63 t.equals(res.files.length, totalFiles, 'all files');
68 test('\nreading root without filter using lstat', function (t) {
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');
77 test('\nreading root with symlinks using lstat', function (t) {
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'));
90 test('\nreading non-standard fds', function (t) {
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');
104 test('\nreading root using glob filter', function (t) {
106 t.test('\n# "*.ext1"', function (t) {
108 readdirp(opts( { fileFilter: '*.ext1' } ), function (err, res) {
109 t.equals(res.files.length, ext1Files, 'all ext1 files');
113 t.test('\n# ["*.ext1", "*.ext3"]', function (t) {
115 readdirp(opts( { fileFilter: [ '*.ext1', '*.ext3' ] } ), function (err, res) {
116 t.equals(res.files.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
120 t.test('\n# "root_dir1"', function (t) {
122 readdirp(opts( { directoryFilter: 'root_dir1' }), function (err, res) {
123 t.equals(res.directories.length, 1, 'one directory');
127 t.test('\n# ["root_dir1", "*dir1_subdir1"]', function (t) {
129 readdirp(opts( { directoryFilter: [ 'root_dir1', '*dir1_subdir1' ]}), function (err, res) {
130 t.equals(res.directories.length, 2, 'two directories');
135 t.test('\n# negated: "!*.ext1"', function (t) {
137 readdirp(opts( { fileFilter: '!*.ext1' } ), function (err, res) {
138 t.equals(res.files.length, totalFiles - ext1Files, 'all but ext1 files');
142 t.test('\n# negated: ["!*.ext1", "!*.ext3"]', function (t) {
144 readdirp(opts( { fileFilter: [ '!*.ext1', '!*.ext3' ] } ), function (err, res) {
145 t.equals(res.files.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
150 t.test('\n# mixed: ["*.ext1", "!*.ext3"]', function (t) {
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');
158 t.test('\n# leading and trailing spaces: [" *.ext1", "*.ext3 "]', function (t) {
160 readdirp(opts( { fileFilter: [ ' *.ext1', '*.ext3 ' ] } ), function (err, res) {
161 t.equals(res.files.length, ext1Files + ext3Files, 'all ext1 and ext3 files');
165 t.test('\n# leading and trailing spaces: [" !*.ext1", " !*.ext3 "]', function (t) {
167 readdirp(opts( { fileFilter: [ ' !*.ext1', ' !*.ext3' ] } ), function (err, res) {
168 t.equals(res.files.length, totalFiles - ext1Files - ext3Files, 'all but ext1 and ext3 files');
173 t.test('\n# ** glob pattern', function (t) {
175 readdirp(opts( { fileFilter: '**/*.ext1' } ), function (err, res) {
176 t.equals(res.files.length, ext1Files, 'ignores ** in **/*.ext1 -> only *.ext1 files');
182 test('\n\nreading root using function filter', function (t) {
183 t.test('\n# file filter -> "contains root_dir2"', function (t) {
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');
194 t.test('\n# directory filter -> "name has length 9"', function (t) {
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');
206 test('\nreading root specifying maximum depth', function (t) {
207 t.test('\n# depth 1', function (t) {
209 readdirp(opts( { depth: 1 } ), function (err, res) {
210 t.equals(res.files.length, depth1Files, 'does not return files at depth 2');
215 test('\nreading root with no recursion', function (t) {
216 t.test('\n# depth 0', function (t) {
218 readdirp(opts( { depth: 0 } ), function (err, res) {
219 t.equals(res.files.length, depth0Files, 'does not return files at depth 0');
224 test('\nprogress callbacks', function (t) {
227 var pluckName = function(fi) { return fi.name; }
228 , processedFiles = [];
233 processedFiles.push(fi);
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');
243 test('resolving of name, full and relative paths', function (t) {
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'
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' }
260 opts.forEach(function (op) {
261 op.fileFilter = 'root_dir1_file1.ext1';
263 t.test('\n' + util.inspect(op), function (t) {
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');
271 fs.realpath(op.root, function(err, fullRoot) {
272 readdirp (op, function(err, res) {
274 res.files[0].fullParentDir
275 , path.join(fullRoot, op.prefix, expected.parentDirName)
276 , 'correct parentDir'
279 res.files[0].fullPath
280 , path.join(fullRoot, op.prefix, expected.parentDirName, expected.name)