Skip to content

Commit ba0704d

Browse files
committed
test: fix fs write encoding coverage
Pass an encoding rather than a string length to FileHandle.write. Cover invalid encodings in fs.write, fs.writeSync, and FileHandle.write, along with valid encodings and unchanged files after rejected writes. Rejecting previously accepted invalid encodings is an intentional semver-major bug fix shared with the crypto validation changes. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex
1 parent 7e2e179 commit ba0704d

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎test/parallel/test-fs-promises-file-handle-write.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ async function validateEmptyWrite() {
4141
async function validateNonUint8ArrayWrite() {
4242
const filePathForHandle = path.resolve(tmpDir, 'tmp-data-write.txt');
4343
const fileHandle = await open(filePathForHandle, 'w+');
44-
const buffer = Buffer.from('Hello world', 'utf8').toString('base64');
44+
const string = Buffer.from('Hello world', 'utf8').toString('base64');
4545

46-
await fileHandle.write(buffer, 0, buffer.length);
46+
await fileHandle.write(string, 0, 'utf8');
4747
const readFileData = fs.readFileSync(filePathForHandle);
48-
assert.deepStrictEqual(Buffer.from(buffer, 'utf8'), readFileData);
48+
assert.deepStrictEqual(Buffer.from(string, 'utf8'), readFileData);
4949

5050
await fileHandle.close();
5151
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const fs = require('fs');
6+
const path = require('path');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
tmpdir.refresh();
10+
11+
async function main() {
12+
const file = path.join(tmpdir.path, 'write-invalid-encoding');
13+
const handle = await fs.promises.open(file, 'w+');
14+
try {
15+
for (const encoding of ['bad', 16]) {
16+
const error = {
17+
code: 'ERR_UNKNOWN_ENCODING',
18+
message: `Unknown encoding: ${encoding}`,
19+
};
20+
assert.throws(() => fs.writeSync(handle.fd, '6162', 0, encoding), error);
21+
assert.throws(
22+
() => fs.write(handle.fd, '6162', 0, encoding, common.mustNotCall()),
23+
error);
24+
await assert.rejects(handle.write('6162', 0, encoding), error);
25+
}
26+
// Rejected writes must not change the file.
27+
assert.strictEqual((await handle.stat()).size, 0);
28+
29+
for (const encoding of [undefined, null, '', 'utf8', 'UTF-8', 'hex']) {
30+
const expected = encoding === 'hex' ? 'ab' : '6162';
31+
await handle.truncate(0);
32+
assert.strictEqual(fs.writeSync(handle.fd, '6162', 0, encoding),
33+
expected.length);
34+
assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
35+
36+
await handle.truncate(0);
37+
await new Promise((resolve, reject) => {
38+
fs.write(handle.fd, '6162', 0, encoding, common.mustCall((err, written) => {
39+
if (err) return reject(err);
40+
assert.strictEqual(written, expected.length);
41+
resolve();
42+
}));
43+
});
44+
assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
45+
46+
await handle.truncate(0);
47+
const { bytesWritten } = await handle.write('6162', 0, encoding);
48+
assert.strictEqual(bytesWritten, expected.length);
49+
assert.strictEqual(fs.readFileSync(file, 'utf8'), expected);
50+
}
51+
} finally {
52+
await handle.close();
53+
}
54+
}
55+
56+
main().then(common.mustCall());

0 commit comments

Comments
 (0)