Skip to content

Commit ac0e126

Browse files
committed
squash: update new tests
1 parent 5037e54 commit ac0e126

File tree

2 files changed

+108
-177
lines changed

2 files changed

+108
-177
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,41 @@
11
'use strict';
22

33
const common = require('../common');
4-
const fs = require('fs');
5-
const fsPromises = fs.promises;
4+
5+
// This test ensures that filehandle.write accepts "named parameters" object
6+
// and doesn't interpret objects as strings
7+
8+
const assert = require('assert');
9+
const fsPromises = require('fs').promises;
610
const path = require('path');
711
const tmpdir = require('../common/tmpdir');
8-
const assert = require('assert');
912

1013
tmpdir.refresh();
1114

1215
const dest = path.resolve(tmpdir.path, 'tmp.txt');
1316
const buffer = Buffer.from('zyx');
1417

15-
(async () => {
16-
let fh = await fsPromises.open(dest, 'w+');
17-
18-
assert.rejects(async () => {
19-
await fh.write(
20-
{}
21-
);
22-
}, {
23-
code: 'ERR_INVALID_ARG_TYPE',
24-
name: 'TypeError',
25-
message: 'The "buffer" argument must be an instance of Buffer, ' +
26-
'TypedArray, or DataView. Received undefined'
27-
});
28-
assert.rejects(async () => {
29-
await fh.write(
30-
{ buffer: 'abc' }
31-
);
32-
}, {
33-
code: 'ERR_INVALID_ARG_TYPE',
34-
name: 'TypeError',
35-
message: 'The "buffer" argument must be an instance of Buffer, ' +
36-
'TypedArray, or DataView. Received type string (\'abc\')'
37-
});
38-
assert.rejects(async () => {
39-
await fh.write(
40-
{ buffer, length: 5 }
41-
);
42-
}, {
43-
code: 'ERR_OUT_OF_RANGE',
44-
name: 'RangeError',
45-
message: 'The value of "length" is out of range. ' +
46-
'It must be <= 3. Received 5'
47-
});
48-
assert.rejects(async () => {
49-
await fh.write(
50-
{ buffer, offset: 5 }
51-
);
52-
}, {
53-
code: 'ERR_OUT_OF_RANGE',
54-
name: 'RangeError',
55-
message: 'The value of "offset" is out of range. ' +
56-
'It must be <= 3. Received 5'
57-
});
58-
assert.rejects(async () => {
59-
await fh.write(
60-
{ buffer, offset: 1 }
61-
);
62-
}, {
63-
code: 'ERR_OUT_OF_RANGE',
64-
name: 'RangeError',
65-
message: 'The value of "length" is out of range. ' +
66-
'It must be <= 2. Received 3'
67-
});
68-
assert.rejects(async () => {
69-
await fh.write(
70-
{ buffer, length: 1, offset: 3 }
71-
);
72-
}, {
73-
code: 'ERR_OUT_OF_RANGE',
74-
name: 'RangeError',
75-
message: 'The value of "length" is out of range. ' +
76-
'It must be <= 0. Received 1'
77-
});
78-
assert.rejects(async () => {
79-
await fh.write(
80-
{ buffer, length: -1 }
81-
);
82-
}, {
83-
code: 'ERR_OUT_OF_RANGE',
84-
name: 'RangeError',
85-
message: 'The value of "length" is out of range. ' +
86-
'It must be >= 0. Received -1'
87-
});
88-
assert.rejects(async () => {
89-
await fh.write(
90-
{ buffer, offset: -1 }
91-
);
92-
}, {
93-
code: 'ERR_OUT_OF_RANGE',
94-
name: 'RangeError',
95-
message: 'The value of "offset" is out of range. ' +
96-
'It must be >= 0 && <= 9007199254740991. Received -1'
97-
});
98-
99-
await fh.close();
18+
async function testInvalid(dest, expectedCode, params) {
19+
let fh;
20+
try {
21+
fh = await fsPromises.open(dest, 'w+');
22+
await assert.rejects(
23+
async () => fh.write(params),
24+
{ code: expectedCode });
25+
} finally {
26+
await fh?.close();
27+
}
28+
}
10029

101-
for (const params of [
102-
{ buffer },
103-
{ buffer, length: 1 },
104-
{ buffer, position: 5 },
105-
{ buffer, length: 1, position: 5 },
106-
{ buffer, length: 1, position: -1, offset: 2 },
107-
{ buffer, length: null },
108-
]) {
30+
async function testValid(dest, params) {
31+
let fh;
32+
try {
10933
fh = await fsPromises.open(dest, 'w+');
11034
const writeResult = await fh.write(params);
11135
const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer);
11236
const readResult = await fh.read(params);
11337
const readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer);
11438

115-
// Test compatibility with filehandle.read counterpart with reused params
11639
assert.ok(writeResult.bytesWritten >= readResult.bytesRead);
11740
if (params.length !== undefined && params.length !== null) {
11841
assert.strictEqual(writeResult.bytesWritten, params.length);
@@ -121,6 +44,44 @@ const buffer = Buffer.from('zyx');
12144
assert.deepStrictEqual(writeBufCopy, readBufCopy);
12245
}
12346
assert.deepStrictEqual(writeResult.buffer, readResult.buffer);
124-
await fh.close();
47+
} finally {
48+
await fh?.close();
49+
}
50+
}
51+
52+
(async () => {
53+
// Test if first argument is not wrongly interpreted as ArrayBufferView|string
54+
for (const badParams of [
55+
undefined, null, true, 42, 42n, Symbol('42'), NaN, [],
56+
{},
57+
{ buffer: 'amNotParam' },
58+
{ string: 'amNotParam' },
59+
{ buffer: new Uint8Array(1).buffer },
60+
new Date(),
61+
new String('notPrimitive'),
62+
{ toString() { return 'amObject'; } },
63+
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
64+
]) {
65+
await testInvalid(dest, 'ERR_INVALID_ARG_TYPE', badParams);
66+
}
67+
68+
// Various invalid params
69+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: 5 });
70+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, offset: 5 });
71+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, offset: 1 });
72+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: 1, offset: 3 });
73+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, length: -1 });
74+
await testInvalid(dest, 'ERR_OUT_OF_RANGE', { buffer, offset: -1 });
75+
76+
// Test compatibility with filehandle.read counterpart with reused params
77+
for (const params of [
78+
{ buffer },
79+
{ buffer, length: 1 },
80+
{ buffer, position: 5 },
81+
{ buffer, length: 1, position: 5 },
82+
{ buffer, length: 1, position: -1, offset: 2 },
83+
{ buffer, length: null },
84+
]) {
85+
await testValid(dest, params);
12586
}
12687
})().then(common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,74 @@
11
'use strict';
22

33
require('../common');
4+
5+
// This test ensures that fs.writeSync accepts "named parameters" object
6+
// and doesn't interpret objects as strings
7+
8+
const assert = require('assert');
49
const fs = require('fs');
510
const path = require('path');
611
const tmpdir = require('../common/tmpdir');
7-
const assert = require('assert');
812

913
tmpdir.refresh();
1014

1115
const dest = path.resolve(tmpdir.path, 'tmp.txt');
1216
const buffer = Buffer.from('zyx');
1317

14-
{
15-
let fd = fs.openSync(dest, 'w+');
18+
function testInvalid(dest, expectedCode, ...bufferAndParams) {
19+
let fd;
20+
try {
21+
fd = fs.openSync(dest, 'w+');
22+
assert.throws(
23+
() => fs.writeSync(fd, ...bufferAndParams),
24+
{ code: expectedCode });
25+
} finally {
26+
if (fd != null) fs.closeSync(fd);
27+
}
28+
}
1629

17-
assert.throws(() => {
18-
fs.writeSync(fd, {}); // Missing buffer, {} is second argument
19-
}, {
20-
code: 'ERR_INVALID_ARG_TYPE',
21-
name: 'TypeError',
22-
message: 'The "buffer" argument must be of type string or an instance ' +
23-
'of Buffer, TypedArray, or DataView. Received an instance of Object'
24-
});
25-
assert.throws(() => {
26-
fs.writeSync(fd, buffer, { length: 5 });
27-
}, {
28-
code: 'ERR_OUT_OF_RANGE',
29-
name: 'RangeError',
30-
message: 'The value of "length" is out of range. ' +
31-
'It must be <= 3. Received 5'
32-
});
33-
assert.throws(() => {
34-
fs.writeSync(fd, buffer, { offset: 5 });
35-
}, {
36-
code: 'ERR_OUT_OF_RANGE',
37-
name: 'RangeError',
38-
message: 'The value of "offset" is out of range. ' +
39-
'It must be <= 3. Received 5'
40-
});
41-
assert.throws(() => {
42-
fs.writeSync(fd, buffer, { offset: 1 });
43-
}, {
44-
code: 'ERR_OUT_OF_RANGE',
45-
name: 'RangeError',
46-
message: 'The value of "length" is out of range. ' +
47-
'It must be <= 2. Received 3'
48-
});
49-
assert.throws(() => {
50-
fs.writeSync(fd, buffer, { length: 1, offset: 3 });
51-
}, {
52-
code: 'ERR_OUT_OF_RANGE',
53-
name: 'RangeError',
54-
message: 'The value of "length" is out of range. ' +
55-
'It must be <= 0. Received 1'
56-
});
57-
assert.throws(() => {
58-
fs.writeSync(fd, buffer, { length: -1 });
59-
}, {
60-
code: 'ERR_OUT_OF_RANGE',
61-
name: 'RangeError',
62-
message: 'The value of "length" is out of range. ' +
63-
'It must be >= 0. Received -1'
64-
});
65-
assert.throws(() => {
66-
fs.writeSync(fd, buffer, { offset: -1 });
67-
}, {
68-
code: 'ERR_OUT_OF_RANGE',
69-
name: 'RangeError',
70-
message: 'The value of "offset" is out of range. ' +
71-
'It must be >= 0 && <= 9007199254740991. Received -1'
72-
});
30+
function testValid(dest, buffer, params) {
31+
let fd;
32+
try {
33+
fd = fs.openSync(dest, 'w+');
34+
const bytesWritten = fs.writeSync(fd, buffer, params);
35+
const bytesRead = fs.readSync(fd, buffer, params);
7336

74-
// Test if object is not interpreted as string
75-
for (const buffer of [
37+
assert.ok(bytesWritten >= bytesRead);
38+
if (params.length !== undefined && params.length !== null) {
39+
assert.strictEqual(bytesWritten, params.length);
40+
}
41+
} finally {
42+
if (fd != null) fs.closeSync(fd);
43+
}
44+
}
45+
46+
{
47+
// Test if second argument is not wrongly interpreted as string or params
48+
for (const badBuffer of [
49+
undefined, null, true, 42, 42n, Symbol('42'), NaN, [],
7650
{},
51+
{ buffer: 'amNotParam' },
52+
{ string: 'amNotParam' },
53+
{ buffer: new Uint8Array(1) },
54+
{ buffer: new Uint8Array(1).buffer },
7755
new Date(),
7856
new String('notPrimitive'),
7957
{ toString() { return 'amObject'; } },
8058
{ [Symbol.toPrimitive]: (hint) => 'amObject' },
8159
]) {
82-
assert.throws(() => {
83-
fs.writeSync(fd, buffer);
84-
}, {
85-
code: 'ERR_INVALID_ARG_TYPE',
86-
name: 'TypeError',
87-
message: /^The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView/
88-
});
60+
testInvalid(dest, 'ERR_INVALID_ARG_TYPE', badBuffer);
8961
}
9062

91-
fs.closeSync(fd);
63+
// Various invalid params
64+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { length: 5 });
65+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { offset: 5 });
66+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { offset: 1 });
67+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { length: 1, offset: 3 });
68+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { length: -1 });
69+
testInvalid(dest, 'ERR_OUT_OF_RANGE', buffer, { offset: -1 });
9270

71+
// Test compatibility with fs.readSync counterpart with reused params
9372
for (const params of [
9473
{},
9574
{ length: 1 },
@@ -98,15 +77,6 @@ const buffer = Buffer.from('zyx');
9877
{ length: 1, position: -1, offset: 2 },
9978
{ length: null },
10079
]) {
101-
fd = fs.openSync(dest, 'w+');
102-
const bytesWritten = fs.writeSync(fd, buffer, params);
103-
const bytesRead = fs.writeSync(fd, buffer, params);
104-
105-
// Test compatibility with fs.readSync counterpart with reused params
106-
assert.strictEqual(bytesWritten, bytesRead);
107-
if (params.length !== undefined && params.length !== null) {
108-
assert.strictEqual(bytesWritten, params.length);
109-
}
110-
fs.closeSync(fd);
80+
testValid(dest, buffer, params);
11181
}
11282
}

0 commit comments

Comments
 (0)