From b6aa42c349a3fa9ce917a4f62401092e8f33e190 Mon Sep 17 00:00:00 2001 From: mayank agarwal Date: Sun, 1 Nov 2020 22:20:07 +0530 Subject: [PATCH] test: add test for fs.read when offset key is null added test for uncovered if statement in lib/fs.js PR-URL: https://github.com/nodejs/node/pull/35918 Reviewed-By: Rich Trott Reviewed-By: Joyee Cheung --- test/parallel/test-fs-read-offset-null.js | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 test/parallel/test-fs-read-offset-null.js diff --git a/test/parallel/test-fs-read-offset-null.js b/test/parallel/test-fs-read-offset-null.js new file mode 100644 index 00000000000000..96bcbf0f33d910 --- /dev/null +++ b/test/parallel/test-fs-read-offset-null.js @@ -0,0 +1,43 @@ +'use strict'; + + +// Test to assert the desired functioning of fs.read +// when {offset:null} is passed as options parameter + +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const fsPromises = fs.promises; +const fixtures = require('../common/fixtures'); +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 +fs.open(filepath, 'r', common.mustSucceed((fd) => { + fs.read(fd, { offset: null, buffer: buf }, + common.mustSucceed((bytesRead, buffer) => { + assert.strictEqual(buffer[0], 120); + // Test is done by making sure the first letter in buffer is + // same as first letter in file. + // 66 is the hex for ascii code of letter B + + fs.close(fd, common.mustSucceed(() => {})); + })); +})); + +let filehandle = null; + +// Test for promise api +(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(); +});