Skip to content

Commit 3ef8619

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

2 files changed

+85
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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) {
33+
let fh;
34+
try {
35+
fh = await fs.promises.open(filepath, 'r');
36+
await assert.rejects(
37+
fh.read(buffer, offset, length, position),
38+
{ code }
39+
);
40+
await assert.rejects(
41+
fh.read({ buffer, offset, length, position }),
42+
{ code }
43+
);
44+
await assert.rejects(
45+
fh.read(buffer, { offset, length, position }),
46+
{ code }
47+
);
48+
} finally {
49+
await fh?.close();
50+
}
51+
}
52+
53+
{
54+
await testValid(undefined);
55+
await testValid(null);
56+
await testValid(-1);
57+
await testValid(-1n);
58+
59+
await testValid(0);
60+
await testValid(0n);
61+
await testValid(1);
62+
await testValid(1n);
63+
await testValid(9);
64+
await testValid(9n);
65+
await testValid(Number.MAX_SAFE_INTEGER, [ 'EFBIG' ]);
66+
67+
await testValid(2n ** 63n - 1n - BigInt(length), [ 'EFBIG' ]);
68+
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n);
69+
await testInvalid('ERR_OUT_OF_RANGE', 2n ** 63n - BigInt(length));
70+
71+
await testInvalid('ERR_OUT_OF_RANGE', NaN);
72+
await testInvalid('ERR_OUT_OF_RANGE', -Infinity);
73+
await testInvalid('ERR_OUT_OF_RANGE', Infinity);
74+
await testInvalid('ERR_OUT_OF_RANGE', -0.999);
75+
await testInvalid('ERR_OUT_OF_RANGE', -(2n ** 64n));
76+
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_SAFE_INTEGER + 1);
77+
await testInvalid('ERR_OUT_OF_RANGE', Number.MAX_VALUE);
78+
79+
for (const badTypeValue of [
80+
false, true, '1', Symbol(1), {}, [], () => {}, Promise.resolve(1),
81+
]) {
82+
testInvalid('ERR_INVALID_ARG_TYPE', badTypeValue);
83+
}
84+
}

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

+1-1
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)