Skip to content

Commit 4086b23

Browse files
emilsiverviktargos
authored andcommitted
test: increase fs promise coverage
1. Signal aborted while writing file Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L278 2. Signal aborted on first tick Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L301 3. Validate file size is withing range for reading Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L312 4. Signal aborted right before buffer read Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L321 5. Use fallback buffer allocation when input not buffer Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L374 6. Specify symlink type Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L539 7. Set modification times with lutimes Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L635 8. Use fallback encoding when input is null Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L665 9. Use fallback flag when input is null Refs: https://coverage.nodejs.org/coverage-0b6d3070a176d437/lib/internal/fs/promises.js.html#L681 PR-URL: #36813 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 94204f7 commit 4086b23

4 files changed

+127
-5
lines changed

test/parallel/test-fs-promises-file-handle-readFile.js

+72-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ const common = require('../common');
66
// FileHandle.readFile method.
77

88
const fs = require('fs');
9-
const { open } = fs.promises;
9+
const {
10+
open,
11+
readFile,
12+
writeFile,
13+
truncate
14+
} = fs.promises;
1015
const path = require('path');
1116
const tmpdir = require('../common/tmpdir');
17+
const tick = require('../common/tick');
1218
const assert = require('assert');
1319
const tmpDir = tmpdir.path;
1420

@@ -45,6 +51,70 @@ async function validateReadFileProc() {
4551
assert.ok(hostname.length > 0);
4652
}
4753

54+
async function doReadAndCancel() {
55+
// Signal aborted from the start
56+
{
57+
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
58+
const fileHandle = await open(filePathForHandle, 'w+');
59+
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
60+
fs.writeFileSync(filePathForHandle, buffer);
61+
const controller = new AbortController();
62+
const { signal } = controller;
63+
controller.abort();
64+
await assert.rejects(readFile(fileHandle, { signal }), {
65+
name: 'AbortError'
66+
});
67+
}
68+
69+
// Signal aborted on first tick
70+
{
71+
const filePathForHandle = path.resolve(tmpDir, 'dogs-running1.txt');
72+
const fileHandle = await open(filePathForHandle, 'w+');
73+
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
74+
fs.writeFileSync(filePathForHandle, buffer);
75+
const controller = new AbortController();
76+
const { signal } = controller;
77+
tick(1, () => controller.abort());
78+
await assert.rejects(readFile(fileHandle, { signal }), {
79+
name: 'AbortError'
80+
});
81+
}
82+
83+
// Signal aborted right before buffer read
84+
{
85+
const newFile = path.resolve(tmpDir, 'dogs-running2.txt');
86+
const buffer = Buffer.from('Dogs running'.repeat(1000), 'utf8');
87+
fs.writeFileSync(newFile, buffer);
88+
89+
const fileHandle = await open(newFile, 'r');
90+
91+
const controller = new AbortController();
92+
const { signal } = controller;
93+
tick(2, () => controller.abort());
94+
await assert.rejects(fileHandle.readFile({ signal, encoding: 'utf8' }), {
95+
name: 'AbortError'
96+
});
97+
}
98+
99+
// Validate file size is within range for reading
100+
{
101+
// Variable taken from https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L5
102+
const kIoMaxLength = 2 ** 31 - 1;
103+
104+
const newFile = path.resolve(tmpDir, 'dogs-running3.txt');
105+
await writeFile(newFile, Buffer.from('0'));
106+
await truncate(newFile, kIoMaxLength + 1);
107+
108+
const fileHandle = await open(newFile, 'r');
109+
110+
await assert.rejects(fileHandle.readFile(), {
111+
name: 'RangeError',
112+
code: 'ERR_FS_FILE_TOO_LARGE'
113+
});
114+
}
115+
}
116+
48117
validateReadFile()
49-
.then(() => validateReadFileProc())
118+
.then(validateReadFileProc)
119+
.then(doReadAndCancel)
50120
.then(common.mustCall());

test/parallel/test-fs-promises-file-handle-writeFile.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
const common = require('../common');
44

55
// The following tests validate base functionality for the fs.promises
6-
// FileHandle.readFile method.
6+
// FileHandle.writeFile method.
77

88
const fs = require('fs');
9-
const { open } = fs.promises;
9+
const { open, writeFile } = fs.promises;
1010
const path = require('path');
1111
const tmpdir = require('../common/tmpdir');
1212
const assert = require('assert');
@@ -26,5 +26,19 @@ async function validateWriteFile() {
2626
await fileHandle.close();
2727
}
2828

29+
// Signal aborted while writing file
30+
async function doWriteAndCancel() {
31+
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
32+
const fileHandle = await open(filePathForHandle, 'w+');
33+
const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8');
34+
const controller = new AbortController();
35+
const { signal } = controller;
36+
process.nextTick(() => controller.abort());
37+
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
38+
name: 'AbortError'
39+
});
40+
}
41+
2942
validateWriteFile()
43+
.then(doWriteAndCancel)
3044
.then(common.mustCall());

test/parallel/test-fs-promises-writefile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function doWriteWithCancel() {
3131
}
3232

3333
async function doAppend() {
34-
await fsPromises.appendFile(dest, buffer2);
34+
await fsPromises.appendFile(dest, buffer2, { flag: null });
3535
const data = fs.readFileSync(dest);
3636
const buf = Buffer.concat([buffer, buffer2]);
3737
assert.deepStrictEqual(buf, data);

test/parallel/test-fs-promises.js

+38
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
link,
1717
lchmod,
1818
lstat,
19+
lutimes,
1920
mkdir,
2021
mkdtemp,
2122
open,
@@ -140,6 +141,13 @@ async function getHandle(dest) {
140141
await handle.close();
141142
}
142143

144+
// Use fallback buffer allocation when input not buffer
145+
{
146+
const handle = await getHandle(dest);
147+
const ret = await handle.read(0, 0, 0, 0);
148+
assert.strictEqual(ret.buffer.length, 16384);
149+
}
150+
143151
// Bytes written to file match buffer
144152
{
145153
const handle = await getHandle(dest);
@@ -226,6 +234,19 @@ async function getHandle(dest) {
226234
await handle.close();
227235
}
228236

237+
// Set modification times with lutimes
238+
{
239+
const a_time = new Date();
240+
a_time.setMinutes(a_time.getMinutes() - 1);
241+
const m_time = new Date();
242+
m_time.setHours(m_time.getHours() - 1);
243+
await lutimes(dest, a_time, m_time);
244+
const stats = await stat(dest);
245+
246+
assert.strictEqual(a_time.toString(), stats.atime.toString());
247+
assert.strictEqual(m_time.toString(), stats.mtime.toString());
248+
}
249+
229250
// create symlink
230251
{
231252
const newPath = path.resolve(tmpDir, 'baz2.js');
@@ -270,6 +291,15 @@ async function getHandle(dest) {
270291
}
271292
}
272293

294+
// specify symlink type
295+
{
296+
const dir = path.join(tmpDir, nextdir());
297+
await symlink(tmpDir, dir, 'dir');
298+
const stats = await lstat(dir);
299+
assert.strictEqual(stats.isSymbolicLink(), true);
300+
await unlink(dir);
301+
}
302+
273303
// create hard link
274304
{
275305
const newPath = path.resolve(tmpDir, 'baz2.js');
@@ -296,6 +326,14 @@ async function getHandle(dest) {
296326
await unlink(newFile);
297327
}
298328

329+
// Use fallback encoding when input is null
330+
{
331+
const newFile = path.resolve(tmpDir, 'dogs_running.js');
332+
await writeFile(newFile, 'dogs running', { encoding: null });
333+
const fileExists = fs.existsSync(newFile);
334+
assert.strictEqual(fileExists, true);
335+
}
336+
299337
// `mkdir` when options is number.
300338
{
301339
const dir = path.join(tmpDir, nextdir());

0 commit comments

Comments
 (0)