1'use strict'; 2 3 4// Test to assert the desired functioning of fs.read 5// when {offset:null} is passed as options parameter 6 7const common = require('../common'); 8const assert = require('assert'); 9const fs = require('fs'); 10const fsPromises = fs.promises; 11const fixtures = require('../common/fixtures'); 12const filepath = fixtures.path('x.txt'); 13 14const buf = Buffer.alloc(1); 15// Reading only one character, hence buffer of one byte is enough. 16 17// Tests are done by making sure the first letter in buffer is 18// same as first letter in file. 19// 120 is the ascii code of letter x. 20 21// Tests for callback API. 22fs.open(filepath, 'r', common.mustSucceed((fd) => { 23 fs.read(fd, { offset: null, buffer: buf }, 24 common.mustSucceed((bytesRead, buffer) => { 25 assert.strictEqual(buffer[0], 120); 26 fs.close(fd, common.mustSucceed(() => {})); 27 })); 28})); 29 30fs.open(filepath, 'r', common.mustSucceed((fd) => { 31 fs.read(fd, buf, { offset: null }, 32 common.mustSucceed((bytesRead, buffer) => { 33 assert.strictEqual(buffer[0], 120); 34 fs.close(fd, common.mustSucceed(() => {})); 35 })); 36})); 37 38let filehandle = null; 39 40// Tests for promises api 41(async () => { 42 filehandle = await fsPromises.open(filepath, 'r'); 43 const readObject = await filehandle.read(buf, { offset: null }); 44 assert.strictEqual(readObject.buffer[0], 120); 45})() 46.finally(() => filehandle?.close()) 47.then(common.mustCall()); 48 49// Undocumented: omitted position works the same as position === null 50(async () => { 51 filehandle = await fsPromises.open(filepath, 'r'); 52 const readObject = await filehandle.read(buf, null, buf.length); 53 assert.strictEqual(readObject.buffer[0], 120); 54})() 55.finally(() => filehandle?.close()) 56.then(common.mustCall()); 57 58(async () => { 59 filehandle = await fsPromises.open(filepath, 'r'); 60 const readObject = await filehandle.read(buf, null, buf.length, 0); 61 assert.strictEqual(readObject.buffer[0], 120); 62})() 63.finally(() => filehandle?.close()) 64.then(common.mustCall()); 65