fs: Fix default params for fs.write(Sync) · nodejs/node@1de0490
@@ -2,29 +2,107 @@
22const common = require('../common');
33const assert = require('assert');
44const path = require('path');
5-const Buffer = require('buffer').Buffer;
65const fs = require('fs');
7-const filename = path.join(common.tmpDir, 'write.txt');
86const expected = Buffer.from('hello');
97108common.refreshTmpDir();
11912-fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
13-if (err) throw err;
14-15-fs.write(fd,
16-expected,
17-0,
18-expected.length,
19-null,
20-common.mustCall(function(err, written) {
21-if (err) throw err;
22-23-assert.strictEqual(expected.length, written);
24-fs.closeSync(fd);
25-26-const found = fs.readFileSync(filename, 'utf8');
27-assert.deepStrictEqual(expected.toString(), found);
28-fs.unlinkSync(filename);
29-}));
30-}));
10+// fs.write with all parameters provided:
11+{
12+const filename = path.join(common.tmpDir, 'write1.txt');
13+fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
14+assert.ifError(err);
15+16+const cb = common.mustCall(function(err, written) {
17+assert.ifError(err);
18+19+assert.strictEqual(expected.length, written);
20+fs.closeSync(fd);
21+22+const found = fs.readFileSync(filename, 'utf8');
23+assert.deepStrictEqual(expected.toString(), found);
24+});
25+26+fs.write(fd, expected, 0, expected.length, null, cb);
27+}));
28+}
29+30+// fs.write with a buffer, without the length parameter:
31+{
32+const filename = path.join(common.tmpDir, 'write2.txt');
33+fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
34+assert.ifError(err);
35+36+const cb = common.mustCall(function(err, written) {
37+assert.ifError(err);
38+39+assert.strictEqual(2, written);
40+fs.closeSync(fd);
41+42+const found = fs.readFileSync(filename, 'utf8');
43+assert.deepStrictEqual('lo', found);
44+});
45+46+fs.write(fd, Buffer.from('hello'), 3, cb);
47+}));
48+}
49+50+// fs.write with a buffer, without the offset and length parameters:
51+{
52+const filename = path.join(common.tmpDir, 'write3.txt');
53+fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
54+assert.ifError(err);
55+56+const cb = common.mustCall(function(err, written) {
57+assert.ifError(err);
58+59+assert.strictEqual(expected.length, written);
60+fs.closeSync(fd);
61+62+const found = fs.readFileSync(filename, 'utf8');
63+assert.deepStrictEqual(expected.toString(), found);
64+});
65+66+fs.write(fd, expected, cb);
67+}));
68+}
69+70+// fs.write with the offset passed as undefined followed by the callback:
71+{
72+const filename = path.join(common.tmpDir, 'write4.txt');
73+fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
74+assert.ifError(err);
75+76+const cb = common.mustCall(function(err, written) {
77+assert.ifError(err);
78+79+assert.strictEqual(expected.length, written);
80+fs.closeSync(fd);
81+82+const found = fs.readFileSync(filename, 'utf8');
83+assert.deepStrictEqual(expected.toString(), found);
84+});
85+86+fs.write(fd, expected, undefined, cb);
87+}));
88+}
89+90+// fs.write with offset and length passed as undefined followed by the callback:
91+{
92+const filename = path.join(common.tmpDir, 'write5.txt');
93+fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) {
94+assert.ifError(err);
95+96+const cb = common.mustCall(function(err, written) {
97+assert.ifError(err);
98+99+assert.strictEqual(expected.length, written);
100+fs.closeSync(fd);
101+102+const found = fs.readFileSync(filename, 'utf8');
103+assert.deepStrictEqual(expected.toString(), found);
104+});
105+106+fs.write(fd, expected, undefined, undefined, cb);
107+}));
108+}