diff options
author | Sakthipriyan Vairamani (thefourtheye) <thechargingvolcano@gmail.com> | 2018-10-17 14:40:08 +0530 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-12-15 12:54:50 -0800 |
commit | 8f4b924f4a7b37bd16ddff65329c8e96fc5f0f2d (patch) | |
tree | 5e7c42bff1a19117507ddd239b8acebe6ca10016 /test/parallel/test-fs-promises-readfile-with-fd.js | |
parent | 2c5dae59341aafc69e1edd62555e6942ba8f0d50 (diff) | |
download | android-node-v8-8f4b924f4a7b37bd16ddff65329c8e96fc5f0f2d.tar.gz android-node-v8-8f4b924f4a7b37bd16ddff65329c8e96fc5f0f2d.tar.bz2 android-node-v8-8f4b924f4a7b37bd16ddff65329c8e96fc5f0f2d.zip |
fs: make writeFile consistent with readFile wrt fd
As it is, `readFile` always reads from the current position of the file,
if a file descriptor is used. But `writeFile` always writes from the
beginning of the file.
This patch fixes this inconsistency by making `writeFile` also to write
from the current position of the file when used with a file descriptor.
PR-URL: https://github.com/nodejs/node/pull/23709
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'test/parallel/test-fs-promises-readfile-with-fd.js')
-rw-r--r-- | test/parallel/test-fs-promises-readfile-with-fd.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/parallel/test-fs-promises-readfile-with-fd.js b/test/parallel/test-fs-promises-readfile-with-fd.js new file mode 100644 index 0000000000..9bf4c18536 --- /dev/null +++ b/test/parallel/test-fs-promises-readfile-with-fd.js @@ -0,0 +1,35 @@ +'use strict'; + +/* + * This test makes sure that `readFile()` always reads from the current + * position of the file, instead of reading from the beginning of the file. + */ + +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const { writeFileSync } = require('fs'); +const { open } = require('fs').promises; + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const fn = path.join(tmpdir.path, 'test.txt'); +writeFileSync(fn, 'Hello World'); + +async function readFileTest() { + const handle = await open(fn, 'r'); + + /* Read only five bytes, so that the position moves to five. */ + const buf = Buffer.alloc(5); + const { bytesRead } = await handle.read(buf, 0, 5, null); + assert.strictEqual(bytesRead, 5); + assert.deepStrictEqual(buf.toString(), 'Hello'); + + /* readFile() should read from position five, instead of zero. */ + assert.deepStrictEqual((await handle.readFile()).toString(), ' World'); +} + + +readFileTest() + .then(common.mustCall()); |