Skip to content

Commit c19df17

Browse files
mayankagarwalscodebytere
authored andcommitted
test: add test for fs.read when offset key is null
added test for uncovered if statement in lib/fs.js PR-URL: #35918 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 0fcbb1c commit c19df17

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
7+
constcommon=require('../common');
8+
constassert=require('assert');
9+
constfs=require('fs');
10+
constfsPromises=fs.promises;
11+
constfixtures=require('../common/fixtures');
12+
constfilepath=fixtures.path('x.txt');
13+
14+
constbuf=Buffer.alloc(1);
15+
// Reading only one character, hence buffer of one byte is enough
16+
17+
// Test for callback api
18+
fs.open(filepath,'r',common.mustSucceed((fd)=>{
19+
fs.read(fd,{offset: null,buffer: buf},
20+
common.mustSucceed((bytesRead,buffer)=>{
21+
assert.strictEqual(buffer[0],120);
22+
// Test is done by making sure the first letter in buffer is
23+
// same as first letter in file.
24+
// 66 is the hex for ascii code of letter B
25+
26+
fs.close(fd,common.mustSucceed(()=>{}));
27+
}));
28+
}));
29+
30+
letfilehandle=null;
31+
32+
// Test for promise api
33+
(async()=>{
34+
filehandle=awaitfsPromises.open(filepath,'r');
35+
constreadObject=awaitfilehandle.read(buf,null,buf.length);
36+
assert.strictEqual(readObject.buffer[0],120);
37+
})()
38+
.then(common.mustCall())
39+
.finally(async()=>{
40+
// Close the file handle if it is opened
41+
if(filehandle)
42+
awaitfilehandle.close();
43+
});

0 commit comments

Comments
 (0)