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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 0fcbb1c commit c19df17

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
+43
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+
const common = require('../common');
8+
const assert = require('assert');
9+
const fs = require('fs');
10+
const fsPromises = fs.promises;
11+
const fixtures = require('../common/fixtures');
12+
const filepath = fixtures.path('x.txt');
13+
14+
const buf = 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+
let filehandle = null;
31+
32+
// Test for promise api
33+
(async () => {
34+
filehandle = await fsPromises.open(filepath, 'r');
35+
const readObject = await filehandle.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+
await filehandle.close();
43+
});

0 commit comments

Comments
 (0)