-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #7856 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Yorkie Liu <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
- Loading branch information
1 parent
5d9d415
commit 0b5191f
Showing
4 changed files
with
171 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,56 @@ | ||
'use strict'; | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var fn = path.join(common.tmpDir, 'write.txt'); | ||
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(); | ||
|
||
var foo = 'foo'; | ||
var fd = fs.openSync(fn, 'w'); | ||
// fs.writeSync with all parameters provided: | ||
{ | ||
const fd = fs.openSync(filename, 'w'); | ||
|
||
var written = fs.writeSync(fd, ''); | ||
assert.strictEqual(0, written); | ||
let written = fs.writeSync(fd, ''); | ||
assert.strictEqual(0, written); | ||
|
||
fs.writeSync(fd, foo); | ||
fs.writeSync(fd, 'foo'); | ||
|
||
var bar = 'bár'; | ||
written = fs.writeSync(fd, Buffer.from(bar), 0, Buffer.byteLength(bar)); | ||
assert.ok(written > 3); | ||
fs.closeSync(fd); | ||
written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár')); | ||
assert.ok(written > 3); | ||
fs.closeSync(fd); | ||
|
||
assert.equal(fs.readFileSync(fn), 'foobár'); | ||
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'); | ||
} |
0b5191f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fFix