Skip to content

Commit e7ae40e

Browse files
committed
feature: readdir: get rid of mock-require
1 parent b460323 commit e7ae40e

3 files changed

Lines changed: 63 additions & 88 deletions

File tree

lib/readdir.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

33
const {join, extname} = require('node:path');
4-
const {readdir} = require('node:fs/promises');
4+
const {readdir: _readdir} = require('node:fs/promises');
55
const currify = require('currify');
66
const {tryToCatch} = require('try-to-catch');
7-
const superstat = require('superstat');
7+
const _superstat = require('superstat');
88
const noop = () => {};
99
const {assign} = Object;
1010

11-
const stat = currify(async (dir, name) => {
11+
const stat = currify(async (superstat, dir, name) => {
1212
const full = join(dir, name);
1313
const [, info = empty()] = await tryToCatch(superstat, full);
1414

@@ -17,10 +17,14 @@ const stat = currify(async (dir, name) => {
1717
});
1818
});
1919

20-
module.exports = async (dir) => {
20+
module.exports.readdir = async (dir, overrides = {}) => {
21+
const {
22+
readdir = _readdir,
23+
superstat = _superstat,
24+
} = overrides;
2125
const names = await readdir(dir);
2226

23-
const statsPromises = names.map(stat(dir));
27+
const statsPromises = names.map(stat(superstat, dir));
2428
const stats = await Promise.all(statsPromises);
2529

2630
return stats.map(parseStat);
@@ -44,6 +48,7 @@ function parseStat(stat) {
4448
mode,
4549
uid,
4650
} = stat;
51+
4752
const isDir = stat.isDirectory();
4853
const isLink = stat.isSymbolicLink();
4954

lib/readify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const sortify = require('@cloudcmd/sortify');
99
const {formatify} = require('@cloudcmd/formatify');
1010

1111
const _nicki = require('nicki');
12-
const _readdir = require('./readdir');
12+
const {readdir: _readdir} = require('./readdir');
1313
const isUndefined = (a) => typeof a === 'undefined';
1414

1515
const replaceProperty = currify(_replaceProperty);
@@ -40,7 +40,7 @@ module.exports.readify = async (path, options = {}) => {
4040
return await fillJSON(path, type, formated, {
4141
nicki,
4242
});
43-
}
43+
};
4444

4545
function check({path, type, sort, order}) {
4646
if (!isString(path))

test/readdir.js

Lines changed: 51 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
'use strict';
22

3-
const fs = require('node:fs');
4-
53
const {test, stub} = require('supertape');
6-
const mockRequire = require('mock-require');
74

85
const {tryToCatch} = require('try-to-catch');
9-
const {reRequire, stopAll} = mockRequire;
6+
const {readdir: _readdir} = require('../lib/readdir');
7+
108
const noop = () => {};
119

1210
test('readdir: empty dir', async (t) => {
13-
const {readdir} = fs.promises;
14-
15-
fs.promises.readdir = async () => [];
11+
const readdir = stub().resolves([]);
1612

17-
const _readdir = reRequire('../lib/readdir');
18-
const [, result] = await tryToCatch(_readdir, '.');
19-
20-
fs.promises.readdir = readdir;
13+
const [, result] = await tryToCatch(_readdir, '.', {
14+
readdir,
15+
});
2116

2217
t.deepEqual(result, [], 'should return empty array');
2318
t.end();
2419
});
2520

2621
test('readdir: empty stat', async (t) => {
27-
const {readdir} = fs.promises;
22+
const superstat = stub().rejects(Error('some'));
23+
const readdir = stub().resolves(['hello']);
2824

29-
mockRequire('superstat', async () => {
30-
throw Error('some');
25+
const [, result] = await tryToCatch(_readdir, '/', {
26+
readdir,
27+
superstat,
3128
});
3229

33-
fs.promises.readdir = async () => [
34-
'hello',
35-
];
36-
37-
const _readdir = reRequire('../lib/readdir');
38-
39-
const [, result] = await tryToCatch(_readdir, '/');
40-
41-
fs.promises.readdir = readdir;
42-
4330
const expected = [{
4431
name: 'hello',
4532
size: 0,
@@ -49,34 +36,30 @@ test('readdir: empty stat', async (t) => {
4936
type: 'file',
5037
}];
5138

52-
stopAll();
53-
5439
t.deepEqual(result, expected, 'should return empty array');
5540
t.end();
5641
});
5742

5843
test('readdir: result', async (t) => {
59-
const {readdir} = fs.promises;
60-
6144
const name = 'hello.txt';
6245
const mode = 16_893;
6346
const size = 1024;
6447
const mtime = new Date();
6548
const uid = 1000;
6649

67-
fs.promises.readdir = async () => [
50+
const readdir = stub().resolves([
6851
name,
69-
];
52+
]);
7053

71-
mockRequire('superstat', async () => ({
54+
const superstat = stub().resolves({
7255
isDirectory: noop,
7356
isSymbolicLink: noop,
7457
name,
7558
mode,
7659
size,
7760
mtime,
7861
uid,
79-
}));
62+
});
8063

8164
const expected = [{
8265
name,
@@ -87,61 +70,55 @@ test('readdir: result', async (t) => {
8770
type: 'file',
8871
}];
8972

90-
const _readdir = reRequire('../lib/readdir');
91-
const [, result] = await tryToCatch(_readdir, '.');
92-
93-
fs.promises.readdir = readdir;
94-
stopAll();
73+
const [, result] = await tryToCatch(_readdir, '.', {
74+
readdir,
75+
superstat,
76+
});
9577

9678
t.deepEqual(result, expected, 'should get raw values');
9779
t.end();
9880
});
9981

10082
test('readdir: result: no error', async (t) => {
101-
const {readdir} = fs.promises;
102-
10383
const name = 'hello.txt';
10484
const mode = 16_893;
10585
const size = 1024;
10686
const mtime = new Date();
10787
const uid = 1000;
10888

109-
fs.promises.readdir = () => [
89+
const readdir = stub().resolves([
11090
name,
111-
];
91+
]);
11292

113-
mockRequire('superstat', async () => ({
93+
const superstat = stub().resolves({
11494
isDirectory: noop,
11595
isSymbolicLink: noop,
11696
name,
11797
mode,
11898
size,
11999
mtime,
120100
uid,
121-
}));
122-
123-
const _readdir = reRequire('../lib/readdir');
124-
const [e] = await tryToCatch(_readdir, '.');
101+
});
125102

126-
stopAll();
127-
fs.promises.readdir = readdir;
103+
const [e] = await tryToCatch(_readdir, '.', {
104+
readdir,
105+
superstat,
106+
});
128107

129108
t.notOk(e, e?.message || 'should not receive error');
130109
t.end();
131110
});
132111

133112
test('readdir: result: directory link', async (t) => {
134-
const {readdir} = fs.promises;
135-
136113
const name = 'hello';
137114
const mode = 16_893;
138115
const size = 1024;
139116
const mtime = new Date();
140117
const uid = 1000;
141118

142-
fs.promises.readdir = () => [
119+
const readdir = stub().resolves([
143120
name,
144-
];
121+
]);
145122

146123
const info = {
147124
isDirectory: stub().returns(true),
@@ -155,7 +132,7 @@ test('readdir: result: directory link', async (t) => {
155132
dev: 1337,
156133
};
157134

158-
mockRequire('superstat', async () => info);
135+
const superstat = stub().resolves(info);
159136

160137
const expected = [{
161138
name,
@@ -166,11 +143,10 @@ test('readdir: result: directory link', async (t) => {
166143
type: 'directory-link',
167144
}];
168145

169-
const _readdir = reRequire('../lib/readdir');
170-
const [, result] = await tryToCatch(_readdir, '.');
171-
172-
fs.promises.readdir = readdir;
173-
stopAll();
146+
const [, result] = await tryToCatch(_readdir, '.', {
147+
readdir,
148+
superstat,
149+
});
174150

175151
t.deepEqual(result, expected, 'should get raw values');
176152
t.end();
@@ -183,9 +159,9 @@ test('readdir: result: zip link', async (t) => {
183159
const mtime = new Date();
184160
const uid = 1000;
185161

186-
const readdir = () => [
162+
const readdir = stub().resolves([
187163
name,
188-
];
164+
]);
189165

190166
const info = {
191167
isDirectory: stub().returns(false),
@@ -199,10 +175,7 @@ test('readdir: result: zip link', async (t) => {
199175
dev: 1337,
200176
};
201177

202-
mockRequire('node:fs/promises', {
203-
readdir,
204-
});
205-
mockRequire('superstat', async () => info);
178+
const superstat = stub().resolves(info);
206179

207180
const expected = [{
208181
name,
@@ -213,43 +186,40 @@ test('readdir: result: zip link', async (t) => {
213186
type: 'archive-link',
214187
}];
215188

216-
const _readdir = reRequire('../lib/readdir');
217-
const [, result] = await tryToCatch(_readdir, '.');
218-
219-
stopAll();
189+
const [, result] = await tryToCatch(_readdir, '.', {
190+
readdir,
191+
superstat,
192+
});
220193

221194
t.deepEqual(result, expected, 'should get raw values');
222195
t.end();
223196
});
224197

225198
test('readdir: result: directory link: no error', async (t) => {
226-
const {readdir} = fs.promises;
227-
228199
const name = 'hello';
229200
const mode = 16_893;
230201
const size = 1024;
231202
const mtime = new Date();
232203
const uid = 1000;
233204

234-
fs.promises.readdir = () => [
205+
const readdir = stub().resolves([
235206
name,
236-
];
207+
]);
237208

238-
mockRequire('superstat', async () => ({
239-
isDirectory: stub.returns(true),
240-
isSymbolicLink: stub.returns(true),
209+
const superstat = stub().resolves({
210+
isDirectory: stub().returns(true),
211+
isSymbolicLink: stub().returns(true),
241212
name,
242213
mode,
243214
size,
244215
mtime,
245216
uid,
246-
}));
247-
248-
const _readdir = reRequire('../lib/readdir');
249-
const [e] = await tryToCatch(_readdir, '.');
217+
});
250218

251-
stopAll();
252-
fs.promises.readdir = readdir;
219+
const [e] = await tryToCatch(_readdir, '.', {
220+
readdir,
221+
superstat,
222+
});
253223

254224
t.notOk(e, e?.message || 'should not receive error');
255225
t.end();

0 commit comments

Comments
 (0)