0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-fs-write-sync.js
Andreas Lind 0b5191f15d fs: Fix default params for fs.write(Sync)
Add support for fs.write(fd, buffer, cb) and fs.write(fd, buffer, offset, cb)
as documented at
https://nodejs.org/api/fs.html#fs_fs_write_fd_data_position_encoding_callback
and equivalently for fs.writeSync

Update docs and code comments to reflect the implementation.

PR-URL: https://github.com/nodejs/node/pull/7856
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
2016-11-08 09:48:41 -08:00

57 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const filename = path.join(common.tmpDir, 'write.txt');
common.refreshTmpDir();
// fs.writeSync with all parameters provided:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}
// fs.writeSync with a buffer, without the length parameter:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'), 0);
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}
// fs.writeSync with a buffer, without the offset and length parameters:
{
const fd = fs.openSync(filename, 'w');
let written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, 'foo');
written = fs.writeSync(fd, Buffer.from('bár'));
assert.ok(written > 3);
fs.closeSync(fd);
assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár');
}