Skip to content

Commit 1ae5b9e

Browse files
committed
squash: add test for Promises API
1 parent 7597615 commit 1ae5b9e

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import fs from 'fs';
4+
import assert from 'assert';
5+
6+
// This test ensures that "position" argument is correctly validated
7+
8+
const filepath = fixtures.path('x.txt');
9+
10+
const buffer = Buffer.from('xyz\n');
11+
const offset = 0;
12+
const length = buffer.byteLength;
13+
14+
// allowedErrors is an array of acceptable internal errors
15+
// For example, on some platforms read syscall might return -EFBIG
16+
async function testValid(position, allowedErrors = []) {
17+
let fh;
18+
try {
19+
fh = await fs.promises.open(filepath, 'r');
20+
await fh.read(buffer, offset, length, position);
21+
await fh.read({ buffer, offset, length, position });
22+
await fh.read(buffer, { offset, length, position });
23+
} catch (err) {
24+
if (!allowedErrors.includes(err.code)) {
25+
assert.fail(err);
26+
}
27+
} finally {
28+
await fh?.close();
29+
}
30+
}
31+
32+
async function testInvalid(code, position) { let fh;
33+
try {
34+
fh = await fs.promises.open(filepath, 'r');
35+
await assert.rejects(
36+
fh.read(buffer, offset, length, position),
37+
{ code }
38+
);
39+
await assert.rejects(
40+
fh.read({ buffer, offset, length, position }),
41+
{ code }
42+
);
43+
await assert.rejects(
44+
fh.read(buffer, { offset, length, position }),
45+
{ code }
46+
);
47+
} finally {
48+
await fh?.close();
49+
}
50+
}
51+
52+
{
53+
await testValid(undefined);
54+
await testValid(null);
55+
await testValid(-1);
56+
await testValid(-1n);
57+
58+
await testValid(0);
59+
await testValid(0n);
60+
await testValid(1);
61+
await testValid(1n);
62+
await testValid(9);
63+
await testValid(9n);
64+
await testValid(Number.MAX_SAFE_INTEGER, [ 'EFBIG' ]);
65+
66+
await testValid(2n ** 63n - 1n - BigInt(length), [ 'EFBIG' ]);
67+
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n);
68+
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n - BigInt(length));
69+
70+
await testInvalid('ERR_OUT_OF_RANGE', NaN);
71+
await testInvalid('ERR_OUT_OF_RANGE', -Infinity);
72+
await testInvalid('ERR_OUT_OF_RANGE', Infinity);
73+
await testInvalid('ERR_OUT_OF_RANGE', -0.999);
74+
await testInvalid('ERR_OUT_OF_RANGE', -(2n ** 64n));
75+
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_SAFE_INTEGER + 1);
76+
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_VALUE);
77+
78+
for (const badTypeValue of [
79+
false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1),
80+
]) {
81+
testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
82+
}
83+
}

test/parallel/test-fs-readSync-position-validation.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function testValid(position, allowedErrors = []) {
2828
}
2929
}
3030

31-
function testInvalid(code, position, internalCatch = false) {
31+
function testInvalid(code, position) {
3232
let fdSync;
3333
try {
3434
fdSync = fs.openSync(filepath, 'r');

0 commit comments

Comments
 (0)