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', '/testdir/a/beans',
|
18 | '/testdir/b/123', '/testdir/b/b/hurp-durp',
|
19 | '/testdir/c.txt', '/testdir/d.txt'
|
20 | ])
|
21 |
|
22 | readdir(p.join(__dirname, 'testdir'), function(err, list) {
|
23 | assert.ifError(err)
|
24 | assert.deepEqual(list.sort(), expectedFiles.sort())
|
25 | done()
|
26 | })
|
27 | })
|
28 |
|
29 | it('ignores the files listed in the ignores array', function(done) {
|
30 | var notExpectedFiles = getAbsolutePaths([
|
31 | '/testdir/d.txt', '/testdir/a/beans'
|
32 | ])
|
33 |
|
34 | readdir(p.join(__dirname, 'testdir'), ['d.txt', 'beans'], function(err, list) {
|
35 | assert.ifError(err)
|
36 | list.forEach(function(file) {
|
37 | assert.equal(notExpectedFiles.indexOf(file), -1,
|
38 | 'Failed to ignore file "' + file + '".')
|
39 | })
|
40 | done()
|
41 | })
|
42 | })
|
43 |
|
44 | it('ignores the directories listed in the ignores array', function(done) {
|
45 | var notExpectedFiles = getAbsolutePaths([
|
46 | '/testdir/a/a', '/testdir/a/beans'
|
47 | ])
|
48 |
|
49 | readdir(p.join(__dirname, 'testdir'), ['**/testdir/a'], function(err, list) {
|
50 | assert.ifError(err)
|
51 | list.forEach(function(file) {
|
52 | assert.equal(notExpectedFiles.indexOf(file), -1,
|
53 | 'Failed to ignore file "' + file + '".')
|
54 | })
|
55 | done()
|
56 | })
|
57 | })
|
58 |
|
59 | it('ignores symlinked files and directories listed in the ignores array', function(done) {
|
60 | var notExpectedFiles = getAbsolutePaths([
|
61 | '/testsymlinks/testdir/linkeddir/hi.docx', '/testsymlinks/testdir/linkedfile.wmf'
|
62 | ])
|
63 | readdir(p.join(__dirname, 'testsymlinks/testdir'), ['linkeddir', 'linkedfile.wmf'], function(err, list) {
|
64 | assert.ifError(err)
|
65 | list.forEach(function(file) {
|
66 | assert.equal(notExpectedFiles.indexOf(file), -1,
|
67 | 'Failed to ignore file "' + file + '".')
|
68 | })
|
69 | done()
|
70 | })
|
71 | })
|
72 |
|
73 | it('supports ignoring files with just basename globbing', function(done) {
|
74 | var notExpectedFiles = getAbsolutePaths([
|
75 | '/testdir/d.txt', '/testdir/a/beans'
|
76 | ])
|
77 |
|
78 | readdir(p.join(__dirname, 'testdir'), ['*.txt', 'beans'], function(err, list) {
|
79 | assert.ifError(err)
|
80 | list.forEach(function(file) {
|
81 | assert.equal(notExpectedFiles.indexOf(file), -1,
|
82 | 'Failed to ignore file "' + file + '".')
|
83 | })
|
84 | done()
|
85 | })
|
86 | })
|
87 |
|
88 | it('supports ignoring files with the globstar syntax', function(done) {
|
89 | var notExpectedFiles = getAbsolutePaths([
|
90 | '/testdir/d.txt', '/testdir/a/beans'
|
91 | ])
|
92 |
|
93 | var ignores = ['**/*.txt', '**/a/beans']
|
94 |
|
95 | readdir(p.join(__dirname, 'testdir'), ignores, function(err, list) {
|
96 | assert.ifError(err)
|
97 | list.forEach(function(file) {
|
98 | assert.equal(notExpectedFiles.indexOf(file), -1,
|
99 | 'Failed to ignore file "' + file + '".')
|
100 | })
|
101 | done()
|
102 | })
|
103 | })
|
104 |
|
105 | context('when there is a function in the ignores array', function() {
|
106 | it('passes each file and directory path to the function', function(done) {
|
107 | var expectedPaths = getAbsolutePaths([
|
108 | '/testdir/a',
|
109 | '/testdir/a/a',
|
110 | '/testdir/a/beans',
|
111 | '/testdir/b',
|
112 | '/testdir/b/123',
|
113 | '/testdir/b/b',
|
114 | '/testdir/b/b/hurp-durp',
|
115 | '/testdir/c.txt',
|
116 | '/testdir/d.txt'
|
117 | ])
|
118 | var paths = []
|
119 | function ignoreFunction(path) {
|
120 | paths.push(path)
|
121 | return false
|
122 | }
|
123 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
124 | assert.ifError(err)
|
125 | assert.deepEqual(paths.sort(), expectedPaths.sort())
|
126 | done()
|
127 | })
|
128 | })
|
129 |
|
130 | it('passes the stat object of each file to the function as its second argument', function(done) {
|
131 | var paths = {}
|
132 | function ignoreFunction(path, stats) {
|
133 | paths[path] = stats
|
134 | return false
|
135 | }
|
136 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
137 | assert.ifError(err)
|
138 | assert(paths[getAbsolutePath('/testdir/a')].isDirectory())
|
139 | assert(paths[getAbsolutePath('/testdir/c.txt')].isFile())
|
140 | done()
|
141 | })
|
142 | })
|
143 |
|
144 | it('ignores files that the function returns true for', function(done) {
|
145 | var ignoredFiles = getAbsolutePaths([
|
146 | '/testdir/d.txt',
|
147 | '/testdir/a/beans'
|
148 | ])
|
149 | function ignoreFunction(path) {
|
150 | return ignoredFiles.indexOf(path) != -1
|
151 | }
|
152 |
|
153 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
154 | assert.ifError(err)
|
155 | list.forEach(function(file) {
|
156 | assert.equal(ignoredFiles.indexOf(file), -1,
|
157 | 'Failed to ignore file "' + file + '".')
|
158 | })
|
159 | done()
|
160 | })
|
161 | })
|
162 |
|
163 | it('does not ignore files that the function returns false for', function(done) {
|
164 | var notIgnoredFiles = getAbsolutePaths([
|
165 | '/testdir/d.txt',
|
166 | '/testdir/a/beans'
|
167 | ])
|
168 | function ignoreFunction(path) {
|
169 | return notIgnoredFiles.indexOf(path) == -1
|
170 | }
|
171 |
|
172 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
173 | assert.ifError(err)
|
174 | notIgnoredFiles.forEach(function(file) {
|
175 | assert.notEqual(notIgnoredFiles.indexOf(file), -1,
|
176 | 'Incorrectly ignored file "' + file + '".')
|
177 | })
|
178 | done()
|
179 | })
|
180 | })
|
181 |
|
182 | it('ignores directories that the function returns true for', function(done) {
|
183 | var ignoredDirectory = getAbsolutePath('/testdir/a')
|
184 | var ignoredFiles = getAbsolutePaths([
|
185 | '/testdir/a/a',
|
186 | '/testdir/a/beans'
|
187 | ])
|
188 | function ignoreFunction(path) {
|
189 | return ignoredDirectory == path
|
190 | }
|
191 |
|
192 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
193 | assert.ifError(err)
|
194 | list.forEach(function(file) {
|
195 | assert.equal(ignoredFiles.indexOf(file), -1,
|
196 | 'Failed to ignore file "' + file + '".')
|
197 | })
|
198 | done()
|
199 | })
|
200 | })
|
201 |
|
202 | it('does not ignore directories that the function returns false for', function(done) {
|
203 | var ignoredDirectory = getAbsolutePath('/testdir/a')
|
204 | var notIgnoredFiles = getAbsolutePaths([
|
205 | '/testdir/b/123',
|
206 | '/testdir/b/b/hurp-durp'
|
207 | ])
|
208 | function ignoreFunction(path) {
|
209 | return ignoredDirectory == path
|
210 | }
|
211 |
|
212 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
213 | assert.ifError(err)
|
214 | notIgnoredFiles.forEach(function(file) {
|
215 | assert.notEqual(notIgnoredFiles.indexOf(file), -1,
|
216 | 'Incorrectly ignored file "' + file + '".')
|
217 | })
|
218 | done()
|
219 | })
|
220 | })
|
221 |
|
222 | it('does not descend into directories that the function returns true for', function(done) {
|
223 | var ignoredDirectory = getAbsolutePath('/testdir/a')
|
224 | var ignoredFiles = getAbsolutePaths([
|
225 | '/testdir/a/a',
|
226 | '/testdir/a/beans'
|
227 | ])
|
228 | var paths = []
|
229 | function ignoreFunction(path) {
|
230 | paths.push(path)
|
231 | return ignoredDirectory == path
|
232 | }
|
233 |
|
234 | readdir(p.join(__dirname, 'testdir'), [ignoreFunction], function(err, list) {
|
235 | assert.ifError(err)
|
236 | paths.forEach(function(file) {
|
237 | assert.equal(ignoredFiles.indexOf(file), -1,
|
238 | 'Transversed file in ignored directory "' + file + '".')
|
239 | })
|
240 | done()
|
241 | })
|
242 | })
|
243 | })
|
244 |
|
245 | it('works when there are no files to report except ignored files', function(done) {
|
246 | readdir(p.join(__dirname, 'testdirBeta'), ['*'], function(err, list) {
|
247 | assert.ifError(err)
|
248 | assert.equal(list.length, 0, 'expect to report 0 files')
|
249 | done()
|
250 | })
|
251 | })
|
252 |
|
253 | it('works when negated ignore list is given', function(done) {
|
254 | var expectedFiles = getAbsolutePaths([
|
255 | '/testdirBeta/ignore.txt'
|
256 | ])
|
257 |
|
258 | readdir(p.join(__dirname, 'testdirBeta'), ['!*.txt'], function(err, list) {
|
259 | assert.ifError(err)
|
260 | assert.deepEqual(list.sort(), expectedFiles,
|
261 | 'Failed to find expected files.')
|
262 | done()
|
263 | })
|
264 | })
|
265 |
|
266 | it('traverses directory and file symbolic links', function(done) {
|
267 | var expectedFiles = getAbsolutePaths([
|
268 | '/testsymlinks/testdir/linkeddir/hi.docx',
|
269 | '/testsymlinks/testdir/linkedfile.wmf'
|
270 | ])
|
271 |
|
272 | readdir(p.join(__dirname,'testsymlinks','testdir'), function(err, list) {
|
273 | assert.ifError(err)
|
274 | assert.deepEqual(list.sort(), expectedFiles,
|
275 | 'Failed to find expected files.')
|
276 | done()
|
277 | })
|
278 | })
|
279 | })
|