forked from paulmillr/readdirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
397 lines (359 loc) · 12.2 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* eslint-env mocha */
'use strict';
const fs = require('fs');
const sysPath = require('path');
const {Readable} = require('stream');
const {promisify} = require('util');
const chai = require('chai');
const chaiSubset = require('chai-subset');
const rimraf = require('rimraf');
const readdirp = require('.');
chai.use(chaiSubset);
chai.should();
const pRimraf = promisify(rimraf);
const mkdir = promisify(fs.mkdir);
const symlink = promisify(fs.symlink);
const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const supportsDirent = 'Dirent' in fs;
const isWindows = process.platform === 'win32';
const root = sysPath.join(__dirname, 'test-fixtures');
let testCount = 0;
let currPath;
const read = async (options) => readdirp.promise(currPath, options);
const touch = async (files = [], dirs = []) => {
for (const name of files) {
await writeFile(sysPath.join(currPath, name), `${Date.now()}`);
}
for (const dir of dirs) {
await mkdir(sysPath.join(currPath, dir));
}
};
const formatEntry = (file, dir = root) => {
return {
basename: sysPath.basename(file),
path: sysPath.normalize(file),
fullPath: sysPath.join(dir, file)
};
};
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const waitForEnd = stream => new Promise(resolve => stream.on('end', resolve));
beforeEach(async () => {
testCount++;
currPath = sysPath.join(root, testCount.toString());
await pRimraf(currPath);
await mkdir(currPath);
});
afterEach(async () => {
await pRimraf(currPath);
});
before(async () => {
await pRimraf(root);
await mkdir(root);
});
after(async () => {
await pRimraf(root);
});
describe('basic', () => {
it('reads directory', async () => {
const files = ['a.txt', 'b.txt', 'c.txt'];
await touch(files);
const res = await read();
res.should.have.lengthOf(files.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(files[index], currPath))
);
});
});
describe('symlinks', () => {
// not using arrow function, because this.skip
before(function() {
// GitHub Actions / default Windows installation disable symlink support unless admin
if (isWindows) this.skip();
});
it('handles symlinks', async () => {
const newPath = sysPath.join(currPath, 'test-symlinked.js');
await symlink(sysPath.join(__dirname, 'test.js'), newPath);
const res = await read();
const first = res[0];
first.should.containSubset(formatEntry('test-symlinked.js', currPath));
const contents = await readFile(first.fullPath);
contents.should.match(/handles symlinks/); // name of this test
});
it('handles symlinked directories', async () => {
const originalPath = sysPath.join(__dirname, 'examples');
const originalFiles = await readdir(originalPath);
const newPath = sysPath.join(currPath, 'examples');
await symlink(originalPath, newPath);
const res = await read();
const symlinkedFiles = res.map(entry => entry.basename);
symlinkedFiles.should.eql(originalFiles);
});
it('should use lstat instead of stat', async () => {
const files = ['a.txt', 'b.txt', 'c.txt'];
const symlinkName = 'test-symlinked.js';
const newPath = sysPath.join(currPath, symlinkName);
await symlink(sysPath.join(__dirname, 'test.js'), newPath);
await touch(files);
const expect = [...files, symlinkName];
const res = await read({lstat: true, alwaysStat: true});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) => {
entry.should.containSubset(formatEntry(expect[index], currPath, false));
entry.should.include.own.key('stats');
if (entry.basename === symlinkName) {
entry.stats.isSymbolicLink().should.equals(true);
}
});
});
});
describe('type', () => {
const files = ['a.txt', 'b.txt', 'c.txt'];
const dirs = ['d', 'e', 'f', 'g'];
it('files', async () => {
await touch(files, dirs);
const res = await read({type: 'files'});
res.should.have.lengthOf(files.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(files[index], currPath))
);
});
it('directories', async () => {
await touch(files, dirs);
const res = await read({type: 'directories'});
res.should.have.lengthOf(dirs.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(dirs[index], currPath))
);
});
it('both', async () => {
await touch(files, dirs);
const res = await read({type: 'both'});
const both = files.concat(dirs);
res.should.have.lengthOf(both.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(both[index], currPath))
);
});
it('all', async () => {
await touch(files, dirs);
const res = await read({type: 'all'});
const all = files.concat(dirs);
res.should.have.lengthOf(all.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(all[index], currPath))
);
});
it('invalid', async () => {
try {
await read({type: 'bogus'});
} catch (error) {
error.message.should.match(/Invalid type/);
}
});
});
describe('depth', () => {
const depth0 = ['a.js', 'b.js', 'c.js'];
const subdirs = ['subdir', 'deep'];
const depth1 = ['subdir/d.js', 'deep/e.js'];
const deepSubdirs = ['subdir/s1', 'subdir/s2', 'deep/d1', 'deep/d2'];
const depth2 = ['subdir/s1/f.js', 'deep/d1/h.js'];
beforeEach(async () => {
await touch(depth0, subdirs);
await touch(depth1, deepSubdirs);
await touch(depth2);
});
it('0', async () => {
const res = await read({depth: 0});
res.should.have.lengthOf(depth0.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(depth0[index], currPath))
);
});
it('1', async () => {
const res = await read({depth: 1});
const expect = [...depth0, ...depth1];
res.should.have.lengthOf(expect.length);
res
.sort((a, b) => a.basename > b.basename ? 1 : -1)
.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('2', async () => {
const res = await read({depth: 2});
const expect = [...depth0, ...depth1, ...depth2];
res.should.have.lengthOf(expect.length);
res
.sort((a, b) => a.basename > b.basename ? 1 : -1)
.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('default', async () => {
const res = await read();
const expect = [...depth0, ...depth1, ...depth2];
res.should.have.lengthOf(expect.length);
res
.sort((a, b) => a.basename > b.basename ? 1 : -1)
.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
});
describe('filtering', () => {
beforeEach(async () => {
await touch(['a.js', 'b.txt', 'c.js', 'd.js', 'e.rb']);
});
it('leading and trailing spaces', async () => {
const expect = ['a.js', 'c.js', 'd.js', 'e.rb'];
const res = await read({fileFilter: (a => a.basename.endsWith('.js') || a.basename.endsWith('.rb'))});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
});
it('function', async () => {
const expect = ['a.js', 'c.js', 'd.js'];
const res = await read({fileFilter: (entry) => sysPath.extname(entry.fullPath) === '.js'});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect[index], currPath))
);
if (supportsDirent) {
const expect2 = ['a.js', 'b.txt', 'c.js', 'd.js', 'e.rb'];
const res2 = await read({fileFilter: (entry) => entry.dirent.isFile() });
res2.should.have.lengthOf(expect2.length);
res2.forEach((entry, index) =>
entry.should.containSubset(formatEntry(expect2[index], currPath))
);
}
});
it('function with stats', async () => {
const expect = ['a.js', 'c.js', 'd.js'];
const res = await read({alwaysStat: true, fileFilter: (entry) => sysPath.extname(entry.fullPath) === '.js'});
res.should.have.lengthOf(expect.length);
res.forEach((entry, index) => {
entry.should.containSubset(formatEntry(expect[index], currPath));
entry.should.include.own.key('stats');
});
const expect2 = ['a.js', 'b.txt', 'c.js', 'd.js', 'e.rb'];
const res2 = await read({alwaysStat: true, fileFilter: (entry) => entry.stats.size > 0 });
res2.should.have.lengthOf(expect2.length);
res2.forEach((entry, index) => {
entry.should.containSubset(formatEntry(expect2[index], currPath));
entry.should.include.own.key('stats');
});
});
});
describe('various', () => {
it('emits readable stream', () => {
const stream = readdirp(currPath);
stream.should.be.an.instanceof(Readable);
stream.should.be.an.instanceof(readdirp.ReaddirpStream);
});
it('fails without root option passed', async () => {
try {
readdirp();
} catch (error) {
error.should.be.an.instanceof(Error);
}
});
it('disallows old API', () => {
try {
readdirp({root: '.'});
} catch (error) {
error.should.be.an.instanceof(Error);
}
});
it('exposes promise API', async () => {
const created = ['a.txt', 'c.txt'];
await touch(created);
const result = await readdirp.promise(currPath);
result.should.have.lengthOf(created.length);
result.forEach((entry, index) =>
entry.should.containSubset(formatEntry(created[index], currPath))
);
});
it('should emit warning for missing file', async () => {
// readdirp() is initialized on some big root directory
// readdirp() receives path a/b/c to its queue
// readdirp is reading something else
// a/b gets deleted, so stat()-ting a/b/c would now emit enoent
// We should emit warnings for this case.
// this.timeout(4000);
fs.mkdirSync(sysPath.join(currPath, 'a'));
fs.mkdirSync(sysPath.join(currPath, 'b'));
fs.mkdirSync(sysPath.join(currPath, 'c'));
let isWarningCalled = false;
const stream = readdirp(currPath, { type: 'all', highWaterMark: 1 });
stream
.on('warn', warning => {
warning.should.be.an.instanceof(Error);
warning.code.should.equals('ENOENT');
isWarningCalled = true;
});
await delay(1000);
await pRimraf(sysPath.join(currPath, 'a'));
stream.resume();
await Promise.race([
waitForEnd(stream),
delay(2000)
]);
isWarningCalled.should.equals(true);
}).timeout(4000);
it('should emit warning for file with strict permission', async () => {
// Windows doesn't throw permission error if you access permitted directory
if (isWindows) {
return true;
}
const permitedDir = sysPath.join(currPath, 'permited');
fs.mkdirSync(permitedDir, 0o0);
let isWarningCalled = false;
const stream = readdirp(currPath, { type: 'all' })
.on('data', () => {})
.on('warn', warning => {
warning.should.be.an.instanceof(Error);
warning.code.should.equals('EACCES');
isWarningCalled = true;
});
await Promise.race([
waitForEnd(stream),
delay(2000)
]);
isWarningCalled.should.equals(true);
});
it('should not emit warning after "end" event', async () => {
// Windows doesn't throw permission error if you access permitted directory
if (isWindows) {
return true;
}
const subdir = sysPath.join(currPath, 'subdir');
const permitedDir = sysPath.join(subdir, 'permited');
fs.mkdirSync(subdir);
fs.mkdirSync(permitedDir, 0o0);
let isWarningCalled = false;
let isEnded = false;
let timer;
const stream = readdirp(currPath, { type: 'all' })
.on('data', () => {})
.on('warn', warning => {
warning.should.be.an.instanceof(Error);
warning.code.should.equals('EACCES');
isEnded.should.equals(false);
isWarningCalled = true;
clearTimeout(timer);
})
.on('end', () => {
isWarningCalled.should.equals(true);
isEnded = true;
});
await Promise.race([
waitForEnd(stream),
delay(2000)
]);
isWarningCalled.should.equals(true);
isEnded.should.equals(true);
});
});