diff options
author | Livia Medeiros <74449973+LiviaMedeiros@users.noreply.github.com> | 2022-05-02 21:01:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 21:01:27 +0300 |
commit | 57678e55817366c39a3a241f89949c8fbe8418bc (patch) | |
tree | 12f8024cd2716feaeb6ed7cfdf0681c2a372ac1d /test | |
parent | 6be94c9443c890cd5706cfa6508c4c00e06dfe9e (diff) |
fs: add `read(buffer[, options])` versions
This adds the following:
- `fs.read(fd, buffer[, options], callback)`.
- `filehandle.read(buffer[, options])`.
PR-URL: https://github.com/nodejs/node/pull/42768
Refs: https://github.com/nodejs/node/pull/42601
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-read-offset-null.js | 44 | ||||
-rw-r--r-- | test/parallel/test-fs-read-optional-params.js | 46 | ||||
-rw-r--r-- | test/parallel/test-fs-read-promises-optional-params.js | 12 |
3 files changed, 67 insertions, 35 deletions
diff --git a/test/parallel/test-fs-read-offset-null.js b/test/parallel/test-fs-read-offset-null.js index 27e893f781b..012c94e41e9 100644 --- a/test/parallel/test-fs-read-offset-null.js +++ b/test/parallel/test-fs-read-offset-null.js @@ -14,13 +14,22 @@ const filepath = fixtures.path('x.txt'); const buf = Buffer.alloc(1); // Reading only one character, hence buffer of one byte is enough. -// Test for callback API. +// Tests are done by making sure the first letter in buffer is +// same as first letter in file. +// 120 is the ascii code of letter x. + +// Tests for callback API. fs.open(filepath, 'r', common.mustSucceed((fd) => { fs.read(fd, { offset: null, buffer: buf }, common.mustSucceed((bytesRead, buffer) => { - // Test is done by making sure the first letter in buffer is - // same as first letter in file. - // 120 is the hex for ascii code of letter x. + assert.strictEqual(buffer[0], 120); + fs.close(fd, common.mustSucceed(() => {})); + })); +})); + +fs.open(filepath, 'r', common.mustSucceed((fd) => { + fs.read(fd, buf, { offset: null }, + common.mustSucceed((bytesRead, buffer) => { assert.strictEqual(buffer[0], 120); fs.close(fd, common.mustSucceed(() => {})); })); @@ -28,15 +37,28 @@ fs.open(filepath, 'r', common.mustSucceed((fd) => { let filehandle = null; -// Test for promise api +// Tests for promises api +(async () => { + filehandle = await fsPromises.open(filepath, 'r'); + const readObject = await filehandle.read(buf, { offset: null }); + assert.strictEqual(readObject.buffer[0], 120); +})() +.finally(() => filehandle?.close()) +.then(common.mustCall()); + +// Undocumented: omitted position works the same as position === null (async () => { filehandle = await fsPromises.open(filepath, 'r'); const readObject = await filehandle.read(buf, null, buf.length); assert.strictEqual(readObject.buffer[0], 120); })() -.then(common.mustCall()) -.finally(async () => { -// Close the file handle if it is opened - if (filehandle) - await filehandle.close(); -}); +.finally(() => filehandle?.close()) +.then(common.mustCall()); + +(async () => { + filehandle = await fsPromises.open(filepath, 'r'); + const readObject = await filehandle.read(buf, null, buf.length, 0); + assert.strictEqual(readObject.buffer[0], 120); +})() +.finally(() => filehandle?.close()) +.then(common.mustCall()); diff --git a/test/parallel/test-fs-read-optional-params.js b/test/parallel/test-fs-read-optional-params.js index aac5f51d0bb..2699850b0e9 100644 --- a/test/parallel/test-fs-read-optional-params.js +++ b/test/parallel/test-fs-read-optional-params.js @@ -5,32 +5,34 @@ const fixtures = require('../common/fixtures'); const fs = require('fs'); const assert = require('assert'); const filepath = fixtures.path('x.txt'); -const fd = fs.openSync(filepath, 'r'); const expected = Buffer.from('xyz\n'); const defaultBufferAsync = Buffer.alloc(16384); -const bufferAsOption = Buffer.allocUnsafe(expected.length); +const bufferAsOption = Buffer.allocUnsafe(expected.byteLength); -// Test not passing in any options object -fs.read(fd, common.mustCall((err, bytesRead, buffer) => { - assert.strictEqual(bytesRead, expected.length); - assert.deepStrictEqual(defaultBufferAsync.length, buffer.length); -})); +function testValid(message, ...options) { + const paramsMsg = `${message} (as params)`; + const paramsFilehandle = fs.openSync(filepath, 'r'); + fs.read(paramsFilehandle, ...options, common.mustSucceed((bytesRead, buffer) => { + assert.strictEqual(bytesRead, expected.byteLength, paramsMsg); + assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength, paramsMsg); + fs.closeSync(paramsFilehandle); + })); -// Test passing in an empty options object -fs.read(fd, { position: 0 }, common.mustCall((err, bytesRead, buffer) => { - assert.strictEqual(bytesRead, expected.length); - assert.deepStrictEqual(defaultBufferAsync.length, buffer.length); -})); + const optionsMsg = `${message} (as options)`; + const optionsFilehandle = fs.openSync(filepath, 'r'); + fs.read(optionsFilehandle, bufferAsOption, ...options, common.mustSucceed((bytesRead, buffer) => { + assert.strictEqual(bytesRead, expected.byteLength, optionsMsg); + assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength, optionsMsg); + fs.closeSync(optionsFilehandle); + })); +} -// Test passing in options -fs.read(fd, { - buffer: bufferAsOption, +testValid('Not passing in any object'); +testValid('Passing in a null', null); +testValid('Passing in an empty object', {}); +testValid('Passing in an object', { offset: 0, - length: bufferAsOption.length, - position: 0 -}, - common.mustCall((err, bytesRead, buffer) => { - assert.strictEqual(bytesRead, expected.length); - assert.deepStrictEqual(bufferAsOption.length, buffer.length); - })); + length: bufferAsOption.byteLength, + position: 0, +}); diff --git a/test/parallel/test-fs-read-promises-optional-params.js b/test/parallel/test-fs-read-promises-optional-params.js index 9d19eaa4115..48311e9df45 100644 --- a/test/parallel/test-fs-read-promises-optional-params.js +++ b/test/parallel/test-fs-read-promises-optional-params.js @@ -10,10 +10,18 @@ const fd = fs.openSync(filepath, 'r'); const expected = Buffer.from('xyz\n'); const defaultBufferAsync = Buffer.alloc(16384); +const bufferAsOption = Buffer.allocUnsafe(expected.byteLength); read(fd, {}) .then(function({ bytesRead, buffer }) { - assert.strictEqual(bytesRead, expected.length); - assert.deepStrictEqual(defaultBufferAsync.length, buffer.length); + assert.strictEqual(bytesRead, expected.byteLength); + assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength); + }) + .then(common.mustCall()); + +read(fd, bufferAsOption, { position: 0 }) + .then(function({ bytesRead, buffer }) { + assert.strictEqual(bytesRead, expected.byteLength); + assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength); }) .then(common.mustCall()); |