1 |
|
2 | var assert = require("assert");
|
3 | var p = require("path");
|
4 | var readdir = require("../index");
|
5 |
|
6 | function getAbsolutePath(file) {
|
7 | return p.join(__dirname, file);
|
8 | }
|
9 |
|
10 | function getAbsolutePaths(files) {
|
11 | return files.map(getAbsolutePath);
|
12 | }
|
13 |
|
14 | describe("readdir", function() {
|
15 | it("correctly lists all files in nested directories", function(done) {
|
16 | var expectedFiles = getAbsolutePaths([
|
17 | "/testdir/a/a",
|
18 | "/testdir/a/beans",
|
19 | "/testdir/b/123",
|
20 | "/testdir/b/b/hurp-durp",
|
21 | "/testdir/c.txt",
|
22 | "/testdir/d.txt"
|
23 | ]);
|
24 |
|
25 | readdir(p.join(__dirname, "testdir"), function(err, list) {
|
26 | assert.ifError(err);
|
27 | assert.deepEqual(list.sort(), expectedFiles.sort());
|
28 | done();
|
29 | });
|
30 | });
|
31 |
|
32 | it("ignores the files listed in the ignores array", function(done) {
|
33 | var notExpectedFiles = getAbsolutePaths([
|
34 | "/testdir/d.txt",
|
35 | "/testdir/a/beans"
|
36 | ]);
|
37 |
|
38 | readdir(p.join(__dirname, "testdir"), ["d.txt", "beans"], function(
|
39 | err,
|
40 | list
|
41 | ) {
|
42 | assert.ifError(err);
|
43 | list.forEach(function(file) {
|
44 | assert.equal(
|
45 | notExpectedFiles.indexOf(file),
|
46 | -1,
|
47 | 'Failed to ignore file "' + file + '".'
|
48 | );
|
49 | });
|
50 | done();
|
51 | });
|
52 | });
|
53 |
|
54 | it("ignores the directories listed in the ignores array", function(done) {
|
55 | var notExpectedFiles = getAbsolutePaths([
|
56 | "/testdir/a/a",
|
57 | "/testdir/a/beans"
|
58 | ]);
|
59 |
|
60 | readdir(p.join(__dirname, "testdir"), ["**/testdir/a"], function(
|
61 | err,
|
62 | list
|
63 | ) {
|
64 | assert.ifError(err);
|
65 | list.forEach(function(file) {
|
66 | assert.equal(
|
67 | notExpectedFiles.indexOf(file),
|
68 | -1,
|
69 | 'Failed to ignore file "' + file + '".'
|
70 | );
|
71 | });
|
72 | done();
|
73 | });
|
74 | });
|
75 |
|
76 | it("ignores symlinked files and directories listed in the ignores array", function(
|
77 | done
|
78 | ) {
|
79 | var notExpectedFiles = getAbsolutePaths([
|
80 | "/testsymlinks/testdir/linkeddir/hi.docx",
|
81 | "/testsymlinks/testdir/linkedfile.wmf"
|
82 | ]);
|
83 | readdir(
|
84 | p.join(__dirname, "testsymlinks/testdir"),
|
85 | ["linkeddir", "linkedfile.wmf"],
|
86 | function(err, list) {
|
87 | assert.ifError(err);
|
88 | list.forEach(function(file) {
|
89 | assert.equal(
|
90 | notExpectedFiles.indexOf(file),
|
91 | -1,
|
92 | 'Failed to ignore file "' + file + '".'
|
93 | );
|
94 | });
|
95 | done();
|
96 | }
|
97 | );
|
98 | });
|
99 |
|
100 | it("supports ignoring files with just basename globbing", function(done) {
|
101 | var notExpectedFiles = getAbsolutePaths([
|
102 | "/testdir/d.txt",
|
103 | "/testdir/a/beans"
|
104 | ]);
|
105 |
|
106 | readdir(p.join(__dirname, "testdir"), ["*.txt", "beans"], function(
|
107 | err,
|
108 | list
|
109 | ) {
|
110 | assert.ifError(err);
|
111 | list.forEach(function(file) {
|
112 | assert.equal(
|
113 | notExpectedFiles.indexOf(file),
|
114 | -1,
|
115 | 'Failed to ignore file "' + file + '".'
|
116 | );
|
117 | });
|
118 | done();
|
119 | });
|
120 | });
|
121 |
|
122 | it("supports ignoring files with the globstar syntax", function(done) {
|
123 | var notExpectedFiles = getAbsolutePaths([
|
124 | "/testdir/d.txt",
|
125 | "/testdir/a/beans"
|
126 | ]);
|
127 |
|
128 | var ignores = ["**/*.txt", "**/a/beans"];
|
129 |
|
130 | readdir(p.join(__dirname, "testdir"), ignores, function(err, list) {
|
131 | assert.ifError(err);
|
132 | list.forEach(function(file) {
|
133 | assert.equal(
|
134 | notExpectedFiles.indexOf(file),
|
135 | -1,
|
136 | 'Failed to ignore file "' + file + '".'
|
137 | );
|
138 | });
|
139 | done();
|
140 | });
|
141 | });
|
142 |
|
143 | context("when there is a function in the ignores array", function() {
|
144 | it("passes each file and directory path to the function", function(done) {
|
145 | var expectedPaths = getAbsolutePaths([
|
146 | "/testdir/a",
|
147 | "/testdir/a/a",
|
148 | "/testdir/a/beans",
|
149 | "/testdir/b",
|
150 | "/testdir/b/123",
|
151 | "/testdir/b/b",
|
152 | "/testdir/b/b/hurp-durp",
|
153 | "/testdir/c.txt",
|
154 | "/testdir/d.txt"
|
155 | ]);
|
156 | var paths = [];
|
157 | function ignoreFunction(path) {
|
158 | paths.push(path);
|
159 | return false;
|
160 | }
|
161 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
162 | err,
|
163 | list
|
164 | ) {
|
165 | assert.ifError(err);
|
166 | assert.deepEqual(paths.sort(), expectedPaths.sort());
|
167 | done();
|
168 | });
|
169 | });
|
170 |
|
171 | it("passes the stat object of each file to the function as its second argument", function(
|
172 | done
|
173 | ) {
|
174 | var paths = {};
|
175 | function ignoreFunction(path, stats) {
|
176 | paths[path] = stats;
|
177 | return false;
|
178 | }
|
179 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
180 | err,
|
181 | list
|
182 | ) {
|
183 | assert.ifError(err);
|
184 | assert(paths[getAbsolutePath("/testdir/a")].isDirectory());
|
185 | assert(paths[getAbsolutePath("/testdir/c.txt")].isFile());
|
186 | done();
|
187 | });
|
188 | });
|
189 |
|
190 | it("ignores files that the function returns true for", function(done) {
|
191 | var ignoredFiles = getAbsolutePaths([
|
192 | "/testdir/d.txt",
|
193 | "/testdir/a/beans"
|
194 | ]);
|
195 | function ignoreFunction(path) {
|
196 | return ignoredFiles.indexOf(path) != -1;
|
197 | }
|
198 |
|
199 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
200 | err,
|
201 | list
|
202 | ) {
|
203 | assert.ifError(err);
|
204 | list.forEach(function(file) {
|
205 | assert.equal(
|
206 | ignoredFiles.indexOf(file),
|
207 | -1,
|
208 | 'Failed to ignore file "' + file + '".'
|
209 | );
|
210 | });
|
211 | done();
|
212 | });
|
213 | });
|
214 |
|
215 | it("does not ignore files that the function returns false for", function(
|
216 | done
|
217 | ) {
|
218 | var notIgnoredFiles = getAbsolutePaths([
|
219 | "/testdir/d.txt",
|
220 | "/testdir/a/beans"
|
221 | ]);
|
222 | function ignoreFunction(path) {
|
223 | return notIgnoredFiles.indexOf(path) == -1;
|
224 | }
|
225 |
|
226 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
227 | err,
|
228 | list
|
229 | ) {
|
230 | assert.ifError(err);
|
231 | notIgnoredFiles.forEach(function(file) {
|
232 | assert.notEqual(
|
233 | notIgnoredFiles.indexOf(file),
|
234 | -1,
|
235 | 'Incorrectly ignored file "' + file + '".'
|
236 | );
|
237 | });
|
238 | done();
|
239 | });
|
240 | });
|
241 |
|
242 | it("ignores directories that the function returns true for", function(
|
243 | done
|
244 | ) {
|
245 | var ignoredDirectory = getAbsolutePath("/testdir/a");
|
246 | var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
|
247 | function ignoreFunction(path) {
|
248 | return ignoredDirectory == path;
|
249 | }
|
250 |
|
251 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
252 | err,
|
253 | list
|
254 | ) {
|
255 | assert.ifError(err);
|
256 | list.forEach(function(file) {
|
257 | assert.equal(
|
258 | ignoredFiles.indexOf(file),
|
259 | -1,
|
260 | 'Failed to ignore file "' + file + '".'
|
261 | );
|
262 | });
|
263 | done();
|
264 | });
|
265 | });
|
266 |
|
267 | it("does not ignore directories that the function returns false for", function(
|
268 | done
|
269 | ) {
|
270 | var ignoredDirectory = getAbsolutePath("/testdir/a");
|
271 | var notIgnoredFiles = getAbsolutePaths([
|
272 | "/testdir/b/123",
|
273 | "/testdir/b/b/hurp-durp"
|
274 | ]);
|
275 | function ignoreFunction(path) {
|
276 | return ignoredDirectory == path;
|
277 | }
|
278 |
|
279 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
280 | err,
|
281 | list
|
282 | ) {
|
283 | assert.ifError(err);
|
284 | notIgnoredFiles.forEach(function(file) {
|
285 | assert.notEqual(
|
286 | notIgnoredFiles.indexOf(file),
|
287 | -1,
|
288 | 'Incorrectly ignored file "' + file + '".'
|
289 | );
|
290 | });
|
291 | done();
|
292 | });
|
293 | });
|
294 |
|
295 | it("does not descend into directories that the function returns true for", function(
|
296 | done
|
297 | ) {
|
298 | var ignoredDirectory = getAbsolutePath("/testdir/a");
|
299 | var ignoredFiles = getAbsolutePaths(["/testdir/a/a", "/testdir/a/beans"]);
|
300 | var paths = [];
|
301 | function ignoreFunction(path) {
|
302 | paths.push(path);
|
303 | return ignoredDirectory == path;
|
304 | }
|
305 |
|
306 | readdir(p.join(__dirname, "testdir"), [ignoreFunction], function(
|
307 | err,
|
308 | list
|
309 | ) {
|
310 | assert.ifError(err);
|
311 | paths.forEach(function(file) {
|
312 | assert.equal(
|
313 | ignoredFiles.indexOf(file),
|
314 | -1,
|
315 | 'Transversed file in ignored directory "' + file + '".'
|
316 | );
|
317 | });
|
318 | done();
|
319 | });
|
320 | });
|
321 | });
|
322 |
|
323 | it("works when there are no files to report except ignored files", function(
|
324 | done
|
325 | ) {
|
326 | readdir(p.join(__dirname, "testdirBeta"), ["*"], function(err, list) {
|
327 | assert.ifError(err);
|
328 | assert.equal(list.length, 0, "expect to report 0 files");
|
329 | done();
|
330 | });
|
331 | });
|
332 |
|
333 | it("works when negated ignore list is given", function(done) {
|
334 | var expectedFiles = getAbsolutePaths(["/testdirBeta/ignore.txt"]);
|
335 |
|
336 | readdir(p.join(__dirname, "testdirBeta"), ["!*.txt"], function(err, list) {
|
337 | assert.ifError(err);
|
338 | assert.deepEqual(
|
339 | list.sort(),
|
340 | expectedFiles,
|
341 | "Failed to find expected files."
|
342 | );
|
343 | done();
|
344 | });
|
345 | });
|
346 |
|
347 | it("traverses directory and file symbolic links", function(done) {
|
348 | var expectedFiles = getAbsolutePaths([
|
349 | "/testsymlinks/testdir/linkeddir/hi.docx",
|
350 | "/testsymlinks/testdir/linkedfile.wmf"
|
351 | ]);
|
352 |
|
353 | readdir(p.join(__dirname, "testsymlinks", "testdir"), function(err, list) {
|
354 | assert.ifError(err);
|
355 | assert.deepEqual(
|
356 | list.sort(),
|
357 | expectedFiles,
|
358 | "Failed to find expected files."
|
359 | );
|
360 | done();
|
361 | });
|
362 | });
|
363 |
|
364 | if (!global.Promise) {
|
365 | console.log("Native Promise not supported - skipping tests");
|
366 | } else {
|
367 | it("works with promises", function(done) {
|
368 | var expectedFiles = getAbsolutePaths([
|
369 | "/testdir/a/a",
|
370 | "/testdir/a/beans",
|
371 | "/testdir/b/123",
|
372 | "/testdir/b/b/hurp-durp",
|
373 | "/testdir/c.txt",
|
374 | "/testdir/d.txt"
|
375 | ]);
|
376 |
|
377 | readdir(p.join(__dirname, "testdir"))
|
378 | .then(function(list) {
|
379 | assert.deepEqual(list.sort(), expectedFiles.sort());
|
380 | done();
|
381 | })
|
382 | .catch(done);
|
383 | });
|
384 |
|
385 | it("correctly ignores when using promises", function(done) {
|
386 | var expectedFiles = getAbsolutePaths([
|
387 | "/testdir/a/a",
|
388 | "/testdir/a/beans",
|
389 | "/testdir/b/123",
|
390 | "/testdir/b/b/hurp-durp"
|
391 | ]);
|
392 |
|
393 | readdir(p.join(__dirname, "testdir"), ["*.txt"])
|
394 | .then(function(list) {
|
395 | assert.deepEqual(list.sort(), expectedFiles.sort());
|
396 | done();
|
397 | })
|
398 | .catch(done);
|
399 | });
|
400 | }
|
401 | });
|