/
list-dir-files-test.js
128 lines (121 loc) · 3.68 KB
/
list-dir-files-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
var path = require('path'),
assert = require('assert'),
vows = require('vows'),
readDirFiles = require('../');
var fixturesDir = path.join(__dirname, 'fixtures'),
targetDir = path.join(fixturesDir, 'dir');
var list = [
'dir/',
'dir/a',
'dir/b',
'dir/c',
'dir/d',
'dir/sub/',
'dir/sub/a',
'dir/sub/b'
];
function assertIncludesAll(base, target) {
for (var i = 0; i < base.length; i++) {
assert.include(base, target[i]);
}
}
vows.describe('read-dir-files/list').addBatch({
'When using `read-dir-files`': {
'the list() method': {
'with no options': {
topic: function () {
readDirFiles.list(targetDir, this.callback);
},
'it should contain all files': function (_, data) {
assert.isArray(data);
assertIncludesAll(data, list.map(function (entry) {
return path.join(fixturesDir, entry);
}));
}
},
'with { normalize: false }': {
topic: function () {
readDirFiles.list(targetDir, { normalize: false }, this.callback);
},
'it should contain all denormalized files': function (_, data) {
assert.isArray(data);
assertIncludesAll(data, list);
}
},
'with { recursive: false }': {
topic: function () {
readDirFiles.list(
targetDir,
{ recursive: false },
this.callback
);
},
'it should contain only files in the top-level': function (data) {
assert.isArray(data);
assertIncludesAll(data, list.slice(0, list.length - 2).map(function (entry) {
return path.join(__dirname, 'fixtures', entry);
}));
}
},
"with a filter function": {
topic: function () {
readDirFiles.list(targetDir, {
filter: function (file) {
return !/\/sub/.test(file);
}
}, this.callback);
},
'it should contain only files matching the filter': function (data) {
assert.isArray(data);
assertIncludesAll(data, list.slice(0, list.length - 3).map(function (entry) {
return path.join(__dirname, 'fixtures', entry);
}));
}
},
"when using events": {
"the `directory` event": {
topic: function () {
var that = this,
dirs = [];
readDirFiles.list(targetDir, { normalize: false })
.on('directory', function (dir) { dirs.push(dir) })
.on('end', function () {
that.callback(null, dirs);
})
},
'it should contain all denormalized dirs': function (_, dirs) {
assert.isArray(dirs);
assert.lengthOf(dirs, 2);
assertIncludesAll(dirs, [
'dir/',
'dir/sub/'
])
}
},
"the `file` event": {
topic: function () {
var that = this,
files = [];
readDirFiles.list(targetDir, { normalize: false })
.on('file', function (file) { files.push(file) })
.on('end', function () {
that.callback(null, files);
})
},
'it should contain all denormalized files': function (_, files) {
assert.isArray(files);
assert.lengthOf(files, 6);
assertIncludesAll(files, [
'dir/a',
'dir/b',
'dir/c',
'dir/d',
'dir/sub/a',
'dir/sub/b'
])
}
}
}
}
}
}).export(module);