aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-write-buffer.js
diff options
context:
space:
mode:
authorAndreas Lind <andreas@one.com>2016-07-23 22:43:41 +0200
committerSam Roberts <vieuxtech@gmail.com>2016-11-08 09:48:41 -0800
commit0b5191f15d0f311c804d542b67e2e922d98834f8 (patch)
tree8dfd9acd815ec91d552fd8260c5c8ce927c41e98 /test/parallel/test-fs-write-buffer.js
parent5d9d41597251f79a0c64a3c16957f638705f3db6 (diff)
downloadandroid-node-v8-0b5191f15d0f311c804d542b67e2e922d98834f8.tar.gz
android-node-v8-0b5191f15d0f311c804d542b67e2e922d98834f8.tar.bz2
android-node-v8-0b5191f15d0f311c804d542b67e2e922d98834f8.zip
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>
Diffstat (limited to 'test/parallel/test-fs-write-buffer.js')
-rw-r--r--test/parallel/test-fs-write-buffer.js120
1 files changed, 99 insertions, 21 deletions
diff --git a/test/parallel/test-fs-write-buffer.js b/test/parallel/test-fs-write-buffer.js
index b6404a036a..ed77d697b3 100644
--- a/test/parallel/test-fs-write-buffer.js
+++ b/test/parallel/test-fs-write-buffer.js
@@ -2,29 +2,107 @@
const common = require('../common');
const assert = require('assert');
const path = require('path');
-const Buffer = require('buffer').Buffer;
const fs = require('fs');
-const filename = path.join(common.tmpDir, 'write.txt');
const expected = Buffer.from('hello');
common.refreshTmpDir();
-fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
- if (err) throw err;
-
- fs.write(fd,
- expected,
- 0,
- expected.length,
- null,
- common.mustCall(function(err, written) {
- if (err) throw err;
-
- assert.equal(expected.length, written);
- fs.closeSync(fd);
-
- var found = fs.readFileSync(filename, 'utf8');
- assert.deepStrictEqual(expected.toString(), found);
- fs.unlinkSync(filename);
- }));
-}));
+// fs.write with all parameters provided:
+{
+ const filename = path.join(common.tmpDir, 'write1.txt');
+ fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
+ assert.ifError(err);
+
+ const cb = common.mustCall(function(err, written) {
+ assert.ifError(err);
+
+ assert.strictEqual(expected.length, written);
+ fs.closeSync(fd);
+
+ var found = fs.readFileSync(filename, 'utf8');
+ assert.deepStrictEqual(expected.toString(), found);
+ });
+
+ fs.write(fd, expected, 0, expected.length, null, cb);
+ }));
+}
+
+// fs.write with a buffer, without the length parameter:
+{
+ const filename = path.join(common.tmpDir, 'write2.txt');
+ fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
+ assert.ifError(err);
+
+ const cb = common.mustCall(function(err, written) {
+ assert.ifError(err);
+
+ assert.strictEqual(2, written);
+ fs.closeSync(fd);
+
+ const found = fs.readFileSync(filename, 'utf8');
+ assert.deepStrictEqual('lo', found);
+ });
+
+ fs.write(fd, Buffer.from('hello'), 3, cb);
+ }));
+}
+
+// fs.write with a buffer, without the offset and length parameters:
+{
+ const filename = path.join(common.tmpDir, 'write3.txt');
+ fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
+ assert.ifError(err);
+
+ const cb = common.mustCall(function(err, written) {
+ assert.ifError(err);
+
+ assert.strictEqual(expected.length, written);
+ fs.closeSync(fd);
+
+ const found = fs.readFileSync(filename, 'utf8');
+ assert.deepStrictEqual(expected.toString(), found);
+ });
+
+ fs.write(fd, expected, cb);
+ }));
+}
+
+// fs.write with the offset passed as undefined followed by the callback:
+{
+ const filename = path.join(common.tmpDir, 'write4.txt');
+ fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
+ assert.ifError(err);
+
+ const cb = common.mustCall(function(err, written) {
+ assert.ifError(err);
+
+ assert.strictEqual(expected.length, written);
+ fs.closeSync(fd);
+
+ const found = fs.readFileSync(filename, 'utf8');
+ assert.deepStrictEqual(expected.toString(), found);
+ });
+
+ fs.write(fd, expected, undefined, cb);
+ }));
+}
+
+// fs.write with offset and length passed as undefined followed by the callback:
+{
+ const filename = path.join(common.tmpDir, 'write5.txt');
+ fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
+ assert.ifError(err);
+
+ const cb = common.mustCall(function(err, written) {
+ assert.ifError(err);
+
+ assert.strictEqual(expected.length, written);
+ fs.closeSync(fd);
+
+ const found = fs.readFileSync(filename, 'utf8');
+ assert.deepStrictEqual(expected.toString(), found);
+ });
+
+ fs.write(fd, expected, undefined, undefined, cb);
+ }));
+}