/
fs.readdir.spec.js
160 lines (145 loc) · 4.03 KB
/
fs.readdir.spec.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
'use strict';
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
const path = require('path');
const assert = helper.assert;
const withPromise = helper.withPromise;
const inVersion = helper.inVersion;
describe('fs.readdir(path, callback)', function() {
beforeEach(function() {
mock({
'path/to/file.txt': 'file content',
nested: {
sub: {
dir: {
'one.txt': 'one content',
'two.txt': 'two content',
empty: {}
}
}
}
});
});
afterEach(mock.restore);
it('lists directory contents', function(done) {
fs.readdir(path.join('path', 'to'), function(err, items) {
assert.isNull(err);
assert.isArray(items);
assert.deepEqual(items, ['file.txt']);
done();
});
});
withPromise.it('promise lists directory contents', function(done) {
fs.promises.readdir(path.join('path', 'to')).then(function(items) {
assert.isArray(items);
assert.deepEqual(items, ['file.txt']);
done();
}, done);
});
it('lists nested directory contents', function(done) {
fs.readdir(path.join('nested', 'sub', 'dir'), function(err, items) {
assert.isNull(err);
assert.isArray(items);
assert.deepEqual(items, ['empty', 'one.txt', 'two.txt']);
done();
});
});
withPromise.it('promise lists nested directory contents', function(done) {
fs.promises
.readdir(path.join('nested', 'sub', 'dir'))
.then(function(items) {
assert.isArray(items);
assert.deepEqual(items, ['empty', 'one.txt', 'two.txt']);
done();
}, done);
});
it('calls with an error for bogus path', function(done) {
fs.readdir('bogus', function(err, items) {
assert.instanceOf(err, Error);
assert.isUndefined(items);
done();
});
});
withPromise.it('promise calls with an error for bogus path', function(done) {
fs.promises.readdir('bogus').then(
function() {
assert.fail('should not succeed.');
done();
},
function(err) {
assert.instanceOf(err, Error);
done();
}
);
});
inVersion('>=10.10').it('should support withFileTypes option', function(
done
) {
fs.readdir(
path.join('nested', 'sub', 'dir'),
{withFileTypes: true},
function(err, items) {
assert.isNull(err);
assert.isArray(items);
assert.deepEqual(items, [
{name: 'empty'},
{name: 'one.txt'},
{name: 'two.txt'}
]);
assert.ok(items[0].isDirectory());
assert.ok(items[1].isFile());
assert.ok(items[2].isFile());
done();
}
);
});
withPromise.it('should support withFileTypes option', function(done) {
fs.promises
.readdir(path.join('nested', 'sub', 'dir'), {withFileTypes: true})
.then(function(items) {
assert.isArray(items);
assert.deepEqual(items, [
{name: 'empty'},
{name: 'one.txt'},
{name: 'two.txt'}
]);
assert.ok(items[0].isDirectory());
assert.ok(items[1].isFile());
assert.ok(items[2].isFile());
done();
}, done);
});
});
describe('fs.readdirSync(path)', function() {
beforeEach(function() {
mock({
'path/to/file.txt': 'file content',
nested: {
sub: {
dir: {
'one.txt': 'one content',
'two.txt': 'two content',
empty: {}
}
}
}
});
});
afterEach(mock.restore);
it('lists directory contents', function() {
const items = fs.readdirSync(path.join('path', 'to'));
assert.isArray(items);
assert.deepEqual(items, ['file.txt']);
});
it('lists nested directory contents', function() {
const items = fs.readdirSync(path.join('nested', 'sub', 'dir'));
assert.isArray(items);
assert.deepEqual(items, ['empty', 'one.txt', 'two.txt']);
});
it('throws for bogus path', function() {
assert.throws(function() {
fs.readdirSync('bogus');
});
});
});