deno.land / std@0.166.0 / node / _tools / test / parallel / test-fs-opendir.js

test-fs-opendir.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
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.8.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');const fs = require('fs');const path = require('path');
const tmpdir = require('../common/tmpdir');
const testDir = tmpdir.path;const files = ['empty', 'files', 'for', 'just', 'testing'];
// Make sure tmp directory is cleantmpdir.refresh();
// Create the necessary filesfiles.forEach(function(filename) { fs.closeSync(fs.openSync(path.join(testDir, filename), 'w'));});
function assertDirent(dirent) { assert(dirent instanceof fs.Dirent); assert.strictEqual(dirent.isFile(), true); assert.strictEqual(dirent.isDirectory(), false); // TODO(wafuwafu13): Support these method // assert.strictEqual(dirent.isSocket(), false); // assert.strictEqual(dirent.isBlockDevice(), false); // assert.strictEqual(dirent.isCharacterDevice(), false); // assert.strictEqual(dirent.isFIFO(), false); assert.strictEqual(dirent.isSymbolicLink(), false);}
// NOTE: this error doesn't occur in Denoconst dirclosedError = { code: 'ERR_DIR_CLOSED'};
// NOTE: this error doesn't occur in Denoconst dirconcurrentError = { code: 'ERR_DIR_CONCURRENT_OPERATION'};
const invalidCallbackObj = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError'};
// Check the opendir Sync version{ const dir = fs.opendirSync(testDir); const entries = files.map(() => { const dirent = dir.readSync(); assertDirent(dirent); return dirent.name; }); assert.deepStrictEqual(files, entries.sort());
// dir.read should return null when no more entries exist assert.strictEqual(dir.readSync(), null);
// check .path assert.strictEqual(dir.path, testDir);
dir.closeSync();
// assert.throws(() => dir.readSync(), dirclosedError); // assert.throws(() => dir.closeSync(), dirclosedError);}
// Check the opendir async versionfs.opendir(testDir, common.mustSucceed((dir) => { let sync = true; dir.read(common.mustSucceed((dirent) => { assert(!sync);
// Order is operating / file system dependent assert(files.includes(dirent.name), `'files' should include ${dirent}`); assertDirent(dirent);
let syncInner = true; dir.read(common.mustSucceed((dirent) => { assert(!syncInner);
dir.close(common.mustSucceed()); })); syncInner = false; })); sync = false;}));
// opendir() on file should throw ENOTDIRassert.throws(function() { fs.opendirSync(__filename);}, /Error: ENOTDIR: not a directory/);
assert.throws(function() { fs.opendir(__filename);}, /TypeError \[ERR_INVALID_ARG_TYPE\]: The "callback" argument must be of type function/);
fs.opendir(__filename, common.mustCall(function(e) { assert.strictEqual(e.code, 'ENOTDIR');}));
[false, 1, [], {}, null, undefined].forEach((i) => { assert.throws( () => fs.opendir(i, common.mustNotCall()), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' } ); assert.throws( () => fs.opendirSync(i), { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' } );});
// Promise-based testsasync function doPromiseTest() { // Check the opendir Promise version const dir = await fs.promises.opendir(testDir); const entries = [];
let i = files.length; while (i--) { const dirent = await dir.read(); entries.push(dirent.name); assertDirent(dirent); }
assert.deepStrictEqual(files, entries.sort());
// dir.read should return null when no more entries exist assert.strictEqual(await dir.read(), null);
await dir.close();}doPromiseTest().then(common.mustCall());
// Async iteratorasync function doAsyncIterTest() { const entries = []; for await (const dirent of await fs.promises.opendir(testDir)) { entries.push(dirent.name); assertDirent(dirent); }
assert.deepStrictEqual(files, entries.sort());
// Automatically closed during iterator}doAsyncIterTest().then(common.mustCall());
// Async iterators should do automatic cleanup
async function doAsyncIterBreakTest() { const dir = await fs.promises.opendir(testDir); for await (const dirent of dir) { // eslint-disable-line no-unused-vars break; }
// await assert.rejects(async () => dir.read(), dirclosedError);}doAsyncIterBreakTest().then(common.mustCall());
async function doAsyncIterReturnTest() { const dir = await fs.promises.opendir(testDir); await (async function() { for await (const dirent of dir) { return; } })();
// await assert.rejects(async () => dir.read(), dirclosedError);}doAsyncIterReturnTest().then(common.mustCall());
async function doAsyncIterThrowTest() { const dir = await fs.promises.opendir(testDir); try { for await (const dirent of dir) { // eslint-disable-line no-unused-vars throw new Error('oh no'); } } catch (err) { if (err.message !== 'oh no') { throw err; } }
// await assert.rejects(async () => dir.read(), dirclosedError);}doAsyncIterThrowTest().then(common.mustCall());
// Check error thrown on invalid values of bufferSizefor (const bufferSize of [-1, 0, 0.5, 1.5, Infinity, NaN]) { assert.throws( () => fs.opendirSync(testDir, common.mustNotMutateObjectDeep({ bufferSize })), { code: 'ERR_OUT_OF_RANGE' });}for (const bufferSize of ['', '1', null]) { assert.throws( () => fs.opendirSync(testDir, common.mustNotMutateObjectDeep({ bufferSize })), { code: 'ERR_INVALID_ARG_TYPE' });}
// Check that passing a positive integer as bufferSize works{ const dir = fs.opendirSync(testDir, common.mustNotMutateObjectDeep({ bufferSize: 1024 })); assertDirent(dir.readSync()); dir.close();}
// TODO(wafuwafu13): enable this// // Check that when passing a string instead of function - throw an exception// async function doAsyncIterInvalidCallbackTest() {// const dir = await fs.promises.opendir(testDir);// assert.throws(() => dir.close('not function'), invalidCallbackObj);// }// doAsyncIterInvalidCallbackTest().then(common.mustCall());
// Check first call to close() - should not report an error.async function doAsyncIterDirClosedTest() { const dir = await fs.promises.opendir(testDir); await dir.close(); // await assert.rejects(() => dir.close(), dirclosedError);}doAsyncIterDirClosedTest().then(common.mustCall());
// Check that readSync() and closeSync() during read() throw exceptionsasync function doConcurrentAsyncAndSyncOps() { const dir = await fs.promises.opendir(testDir); const promise = dir.read();
// assert.throws(() => dir.closeSync(), dirconcurrentError); // assert.throws(() => dir.readSync(), dirconcurrentError);
await promise; dir.closeSync();}doConcurrentAsyncAndSyncOps().then(common.mustCall());
// TODO(wafuwafu13): enable this// // Check read throw exceptions on invalid callback// {// const dir = fs.opendirSync(testDir);// assert.throws(() => dir.read('INVALID_CALLBACK'), /ERR_INVALID_ARG_TYPE/);// }
// Check that concurrent read() operations don't do weird things.async function doConcurrentAsyncOps() { const dir = await fs.promises.opendir(testDir); const promise1 = dir.read(); const promise2 = dir.read();
assertDirent(await promise1); assertDirent(await promise2); dir.closeSync();}doConcurrentAsyncOps().then(common.mustCall());
// Check that concurrent read() + close() operations don't do weird things.async function doConcurrentAsyncMixedOps() { const dir = await fs.promises.opendir(testDir); const promise1 = dir.read(); const promise2 = dir.close();
assertDirent(await promise1); await promise2;}doConcurrentAsyncMixedOps().then(common.mustCall());
// Check if directory already closed - the callback should pass an error.{ const dir = fs.opendirSync(testDir); dir.closeSync(); dir.close(common.mustCall((error) => { // assert.strictEqual(error.code, dirclosedError.code); }));}
// Check if directory already closed - throw an promise exception.{ const dir = fs.opendirSync(testDir); dir.closeSync(); // assert.rejects(dir.close(), dirclosedError).then(common.mustCall());}
std

Version Info

Tagged at
a year ago