Skip to content

Commit 1a0df53

Browse files
committed
squash: add tests for fs.writeSync
1 parent 87ca7d5 commit 1a0df53

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

test/parallel/test-fs-promises-write-optional-params.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const dest = path.resolve(tmpdir.path, 'tmp.txt');
1313
const buffer = Buffer.from('zyx');
1414

1515
(async () => {
16-
const fh = await fsPromises.open(dest, 'w+');
16+
let fh = await fsPromises.open(dest, 'w+');
1717

1818
assert.rejects(async () => {
1919
await fh.write(
@@ -96,6 +96,8 @@ const buffer = Buffer.from('zyx');
9696
'It must be >= 0 && <= 9007199254740991. Received -1'
9797
});
9898

99+
await fh.close();
100+
99101
for (const params of [
100102
{ buffer },
101103
{ buffer, length: 1 },
@@ -104,6 +106,7 @@ const buffer = Buffer.from('zyx');
104106
{ buffer, length: 1, position: -1, offset: 2 },
105107
{ buffer, length: null },
106108
]) {
109+
fh = await fsPromises.open(dest, 'w+');
107110
const writeResult = await fh.write(params);
108111
const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer);
109112
const readResult = await fh.read(params);
@@ -118,5 +121,6 @@ const buffer = Buffer.from('zyx');
118121
assert.deepStrictEqual(writeBufCopy, readBufCopy);
119122
}
120123
assert.deepStrictEqual(writeResult.buffer, readResult.buffer);
124+
await fh.close();
121125
}
122126
})().then(common.mustCall());
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const tmpdir = require('../common/tmpdir');
7+
const assert = require('assert');
8+
9+
tmpdir.refresh();
10+
11+
const dest = path.resolve(tmpdir.path, 'tmp.txt');
12+
const buffer = Buffer.from('zyx');
13+
14+
{
15+
let fd = fs.openSync(dest, 'w+');
16+
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+
});
73+
74+
fs.closeSync(fd);
75+
76+
for (const params of [
77+
{},
78+
{ length: 1 },
79+
{ position: 5 },
80+
{ length: 1, position: 5 },
81+
{ length: 1, position: -1, offset: 2 },
82+
{ length: null },
83+
]) {
84+
fd = fs.openSync(dest, 'w+');
85+
const bytesWritten = fs.writeSync(fd, buffer, params);
86+
const bytesRead = fs.writeSync(fd, buffer, params);
87+
88+
// Test compatibility with fs.readSync counterpart with reused params
89+
assert.strictEqual(bytesWritten, bytesRead);
90+
if (params.length !== undefined && params.length !== null) {
91+
assert.strictEqual(bytesWritten, params.length);
92+
}
93+
fs.closeSync(fd);
94+
}
95+
}

0 commit comments

Comments
 (0)