Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c19df17

Browse filesBrowse 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
Copy full SHA for c19df17

File tree

Expand file treeCollapse file tree

1 file changed

+43
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+43
-0
lines changed
Open diff view settings
Collapse file
+43Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.