Skip to content

Commit 6a080ab

Browse files
iansujasnell
authored andcommitted
test: add blocks and comments to fs-promises tests
PR-URL: #23627 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bed4a8c commit 6a080ab

File tree

1 file changed

+159
-125
lines changed

1 file changed

+159
-125
lines changed

test/parallel/test-fs-promises.js

+159-125
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,37 @@ async function getHandle(dest) {
7474
{
7575
async function doTest() {
7676
tmpdir.refresh();
77+
7778
const dest = path.resolve(tmpDir, 'baz.js');
78-
await copyFile(fixtures.path('baz.js'), dest);
79-
await access(dest, 'r');
8079

81-
const handle = await open(dest, 'r+');
82-
assert.strictEqual(typeof handle, 'object');
80+
// handle is object
81+
{
82+
const handle = await getHandle(dest);
83+
assert.strictEqual(typeof handle, 'object');
84+
}
8385

84-
let stats = await handle.stat();
85-
verifyStatObject(stats);
86-
assert.strictEqual(stats.size, 35);
86+
// file stats
87+
{
88+
const handle = await getHandle(dest);
89+
let stats = await handle.stat();
90+
verifyStatObject(stats);
91+
assert.strictEqual(stats.size, 35);
8792

88-
await handle.truncate(1);
93+
await handle.truncate(1);
8994

90-
stats = await handle.stat();
91-
verifyStatObject(stats);
92-
assert.strictEqual(stats.size, 1);
95+
stats = await handle.stat();
96+
verifyStatObject(stats);
97+
assert.strictEqual(stats.size, 1);
9398

94-
stats = await stat(dest);
95-
verifyStatObject(stats);
99+
stats = await stat(dest);
100+
verifyStatObject(stats);
96101

97-
stats = await handle.stat();
98-
verifyStatObject(stats);
102+
stats = await handle.stat();
103+
verifyStatObject(stats);
99104

100-
await handle.datasync();
101-
await handle.sync();
105+
await handle.datasync();
106+
await handle.sync();
107+
}
102108

103109
// test fs.read promises when length to read is zero bytes
104110
{
@@ -113,115 +119,140 @@ async function getHandle(dest) {
113119
await unlink(dest);
114120
}
115121

116-
const buf = Buffer.from('hello fsPromises');
117-
const bufLen = buf.length;
118-
await handle.write(buf);
119-
const ret = await handle.read(Buffer.alloc(bufLen), 0, bufLen, 0);
120-
assert.strictEqual(ret.bytesRead, bufLen);
121-
assert.deepStrictEqual(ret.buffer, buf);
122-
123-
const buf2 = Buffer.from('hello FileHandle');
124-
const buf2Len = buf2.length;
125-
await handle.write(buf2, 0, buf2Len, 0);
126-
const ret2 = await handle.read(Buffer.alloc(buf2Len), 0, buf2Len, 0);
127-
assert.strictEqual(ret2.bytesRead, buf2Len);
128-
assert.deepStrictEqual(ret2.buffer, buf2);
129-
await truncate(dest, 5);
130-
assert.deepStrictEqual((await readFile(dest)).toString(), 'hello');
131-
132-
await chmod(dest, 0o666);
133-
await handle.chmod(0o666);
134-
135-
await chmod(dest, (0o10777));
136-
await handle.chmod(0o10777);
137-
138-
if (!common.isWindows) {
139-
await chown(dest, process.getuid(), process.getgid());
140-
await handle.chown(process.getuid(), process.getgid());
122+
// bytes written to file match buffer
123+
{
124+
const handle = await getHandle(dest);
125+
const buf = Buffer.from('hello fsPromises');
126+
const bufLen = buf.length;
127+
await handle.write(buf);
128+
const ret = await handle.read(Buffer.alloc(bufLen), 0, bufLen, 0);
129+
assert.strictEqual(ret.bytesRead, bufLen);
130+
assert.deepStrictEqual(ret.buffer, buf);
141131
}
142132

143-
assert.rejects(
144-
async () => {
145-
await chown(dest, 1, -1);
146-
},
147-
{
148-
code: 'ERR_OUT_OF_RANGE',
149-
name: 'RangeError [ERR_OUT_OF_RANGE]',
150-
message: 'The value of "gid" is out of range. ' +
151-
'It must be >= 0 && < 4294967296. Received -1'
152-
});
133+
// truncate file to specified length
134+
{
135+
const handle = await getHandle(dest);
136+
const buf = Buffer.from('hello FileHandle');
137+
const bufLen = buf.length;
138+
await handle.write(buf, 0, bufLen, 0);
139+
const ret = await handle.read(Buffer.alloc(bufLen), 0, bufLen, 0);
140+
assert.strictEqual(ret.bytesRead, bufLen);
141+
assert.deepStrictEqual(ret.buffer, buf);
142+
await truncate(dest, 5);
143+
assert.deepStrictEqual((await readFile(dest)).toString(), 'hello');
144+
}
153145

154-
assert.rejects(
155-
async () => {
156-
await handle.chown(1, -1);
157-
},
158-
{
159-
code: 'ERR_OUT_OF_RANGE',
160-
name: 'RangeError [ERR_OUT_OF_RANGE]',
161-
message: 'The value of "gid" is out of range. ' +
162-
'It must be >= 0 && < 4294967296. Received -1'
163-
});
146+
// invalid change of ownership
147+
{
148+
const handle = await getHandle(dest);
164149

165-
await utimes(dest, new Date(), new Date());
166-
167-
try {
168-
await handle.utimes(new Date(), new Date());
169-
} catch (err) {
170-
// Some systems do not have futimes. If there is an error,
171-
// expect it to be ENOSYS
172-
common.expectsError({
173-
code: 'ENOSYS',
174-
type: Error
175-
})(err);
150+
await chmod(dest, 0o666);
151+
await handle.chmod(0o666);
152+
153+
await chmod(dest, (0o10777));
154+
await handle.chmod(0o10777);
155+
156+
if (!common.isWindows) {
157+
await chown(dest, process.getuid(), process.getgid());
158+
await handle.chown(process.getuid(), process.getgid());
159+
}
160+
161+
assert.rejects(
162+
async () => {
163+
await chown(dest, 1, -1);
164+
},
165+
{
166+
code: 'ERR_OUT_OF_RANGE',
167+
name: 'RangeError [ERR_OUT_OF_RANGE]',
168+
message: 'The value of "gid" is out of range. ' +
169+
'It must be >= 0 && < 4294967296. Received -1'
170+
});
171+
172+
assert.rejects(
173+
async () => {
174+
await handle.chown(1, -1);
175+
},
176+
{
177+
code: 'ERR_OUT_OF_RANGE',
178+
name: 'RangeError [ERR_OUT_OF_RANGE]',
179+
message: 'The value of "gid" is out of range. ' +
180+
'It must be >= 0 && < 4294967296. Received -1'
181+
});
176182
}
177183

178-
await handle.close();
184+
// set modification times
185+
{
186+
const handle = await getHandle(dest);
179187

180-
const newPath = path.resolve(tmpDir, 'baz2.js');
181-
await rename(dest, newPath);
182-
stats = await stat(newPath);
183-
verifyStatObject(stats);
188+
await utimes(dest, new Date(), new Date());
184189

185-
if (common.canCreateSymLink()) {
186-
const newLink = path.resolve(tmpDir, 'baz3.js');
187-
await symlink(newPath, newLink);
188-
if (!common.isWindows) {
189-
await lchown(newLink, process.getuid(), process.getgid());
190+
try {
191+
await handle.utimes(new Date(), new Date());
192+
} catch (err) {
193+
// Some systems do not have futimes. If there is an error,
194+
// expect it to be ENOSYS
195+
common.expectsError({
196+
code: 'ENOSYS',
197+
type: Error
198+
})(err);
190199
}
191-
stats = await lstat(newLink);
192-
verifyStatObject(stats);
193200

194-
assert.strictEqual(newPath.toLowerCase(),
195-
(await realpath(newLink)).toLowerCase());
196-
assert.strictEqual(newPath.toLowerCase(),
197-
(await readlink(newLink)).toLowerCase());
201+
await handle.close();
202+
}
203+
204+
// create symlink
205+
{
206+
const newPath = path.resolve(tmpDir, 'baz2.js');
207+
await rename(dest, newPath);
208+
let stats = await stat(newPath);
209+
verifyStatObject(stats);
198210

199-
const newMode = 0o666;
200-
if (common.isOSX) {
201-
// lchmod is only available on macOS
202-
await lchmod(newLink, newMode);
211+
if (common.canCreateSymLink()) {
212+
const newLink = path.resolve(tmpDir, 'baz3.js');
213+
await symlink(newPath, newLink);
214+
if (!common.isWindows) {
215+
await lchown(newLink, process.getuid(), process.getgid());
216+
}
203217
stats = await lstat(newLink);
204-
assert.strictEqual(stats.mode & 0o777, newMode);
205-
} else {
206-
await Promise.all([
207-
assert.rejects(
208-
lchmod(newLink, newMode),
209-
common.expectsError({
210-
code: 'ERR_METHOD_NOT_IMPLEMENTED',
211-
type: Error,
212-
message: 'The lchmod() method is not implemented'
213-
})
214-
)
215-
]);
218+
verifyStatObject(stats);
219+
220+
assert.strictEqual(newPath.toLowerCase(),
221+
(await realpath(newLink)).toLowerCase());
222+
assert.strictEqual(newPath.toLowerCase(),
223+
(await readlink(newLink)).toLowerCase());
224+
225+
const newMode = 0o666;
226+
if (common.isOSX) {
227+
// lchmod is only available on macOS
228+
await lchmod(newLink, newMode);
229+
stats = await lstat(newLink);
230+
assert.strictEqual(stats.mode & 0o777, newMode);
231+
} else {
232+
await Promise.all([
233+
assert.rejects(
234+
lchmod(newLink, newMode),
235+
common.expectsError({
236+
code: 'ERR_METHOD_NOT_IMPLEMENTED',
237+
type: Error,
238+
message: 'The lchmod() method is not implemented'
239+
})
240+
)
241+
]);
242+
}
243+
244+
await unlink(newLink);
216245
}
217-
218-
await unlink(newLink);
219246
}
220247

221-
const newLink2 = path.resolve(tmpDir, 'baz4.js');
222-
await link(newPath, newLink2);
248+
// create hard link
249+
{
250+
const newPath = path.resolve(tmpDir, 'baz2.js');
251+
const newLink = path.resolve(tmpDir, 'baz4.js');
252+
await link(newPath, newLink);
223253

224-
await unlink(newLink2);
254+
await unlink(newLink);
255+
}
225256

226257
// testing readdir lists both files and directories
227258
{
@@ -231,7 +262,7 @@ async function getHandle(dest) {
231262
await mkdir(newDir);
232263
await writeFile(newFile, 'DAWGS WIN!', 'utf8');
233264

234-
stats = await stat(newDir);
265+
const stats = await stat(newDir);
235266
assert(stats.isDirectory());
236267
const list = await readdir(tmpDir);
237268
assert.notStrictEqual(list.indexOf('dir'), -1);
@@ -244,23 +275,23 @@ async function getHandle(dest) {
244275
{
245276
const dir = path.join(tmpDir, nextdir());
246277
await mkdir(dir, 777);
247-
stats = await stat(dir);
278+
const stats = await stat(dir);
248279
assert(stats.isDirectory());
249280
}
250281

251282
// mkdir when options is string.
252283
{
253284
const dir = path.join(tmpDir, nextdir());
254285
await mkdir(dir, '777');
255-
stats = await stat(dir);
286+
const stats = await stat(dir);
256287
assert(stats.isDirectory());
257288
}
258289

259290
// mkdirp when folder does not yet exist.
260291
{
261292
const dir = path.join(tmpDir, nextdir(), nextdir());
262293
await mkdir(dir, { recursive: true });
263-
stats = await stat(dir);
294+
const stats = await stat(dir);
264295
assert(stats.isDirectory());
265296
}
266297

@@ -283,15 +314,15 @@ async function getHandle(dest) {
283314
{
284315
const dir = path.resolve(tmpDir, `${nextdir()}/./${nextdir()}`);
285316
await mkdir(dir, { recursive: true });
286-
stats = await stat(dir);
317+
const stats = await stat(dir);
287318
assert(stats.isDirectory());
288319
}
289320

290321
// mkdirp ../
291322
{
292323
const dir = path.resolve(tmpDir, `${nextdir()}/../${nextdir()}`);
293324
await mkdir(dir, { recursive: true });
294-
stats = await stat(dir);
325+
const stats = await stat(dir);
295326
assert(stats.isDirectory());
296327
}
297328

@@ -313,15 +344,18 @@ async function getHandle(dest) {
313344
});
314345
}
315346

316-
await mkdtemp(path.resolve(tmpDir, 'FOO'));
317-
assert.rejects(
318-
// mkdtemp() expects to get a string prefix.
319-
async () => mkdtemp(1),
320-
{
321-
code: 'ERR_INVALID_ARG_TYPE',
322-
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
323-
}
324-
);
347+
// mkdtemp with invalid numeric prefix
348+
{
349+
await mkdtemp(path.resolve(tmpDir, 'FOO'));
350+
assert.rejects(
351+
// mkdtemp() expects to get a string prefix.
352+
async () => mkdtemp(1),
353+
{
354+
code: 'ERR_INVALID_ARG_TYPE',
355+
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
356+
}
357+
);
358+
}
325359

326360
}
327361

0 commit comments

Comments
 (0)