Skip to content

Commit d9eb8b3

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 4af3906 commit d9eb8b3

4 files changed

+129
-5
lines changed

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

+73-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --experimental-abortcontroller
12
'use strict';
23

34
const common = require('../common');
@@ -6,9 +7,15 @@ const common = require('../common');
67
// FileHandle.readFile method.
78

89
const fs = require('fs');
9-
const { open } = fs.promises;
10+
const {
11+
open,
12+
readFile,
13+
writeFile,
14+
truncate
15+
} = fs.promises;
1016
const path = require('path');
1117
const tmpdir = require('../common/tmpdir');
18+
const tick = require('../common/tick');
1219
const assert = require('assert');
1320
const tmpDir = tmpdir.path;
1421

@@ -45,6 +52,70 @@ async function validateReadFileProc() {
4552
assert.ok(hostname.length > 0);
4653
}
4754

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

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
// Flags: --experimental-abortcontroller
12
'use strict';
23

34
const common = require('../common');
45

56
// The following tests validate base functionality for the fs.promises
6-
// FileHandle.readFile method.
7+
// FileHandle.writeFile method.
78

89
const fs = require('fs');
9-
const { open } = fs.promises;
10+
const { open, writeFile } = fs.promises;
1011
const path = require('path');
1112
const tmpdir = require('../common/tmpdir');
1213
const assert = require('assert');
@@ -26,5 +27,19 @@ async function validateWriteFile() {
2627
await fileHandle.close();
2728
}
2829

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

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

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

3434
async function doAppend() {
35-
await fsPromises.appendFile(dest, buffer2);
35+
await fsPromises.appendFile(dest, buffer2, { flag: null });
3636
const data = fs.readFileSync(dest);
3737
const buf = Buffer.concat([buffer, buffer2]);
3838
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)