Skip to content

Commit 1d84e91

Browse files
aduh95nodejs-github-bot
authored andcommitted
test: improve test coverage of fs.ReadStream with FileHandle
PR-URL: #40018 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Zijian Liu <[email protected]>
1 parent eb65871 commit 1d84e91

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

test/parallel/test-fs-read-stream-file-handle.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const tmpdir = require('../common/tmpdir');
77
const file = path.join(tmpdir.path, 'read_stream_filehandle_test.txt');
88
const input = 'hello world';
99

10-
let output = '';
1110
tmpdir.refresh();
1211
fs.writeFileSync(file, input);
1312

14-
fs.promises.open(file, 'r').then(common.mustCall((handle) => {
13+
fs.promises.open(file, 'r').then((handle) => {
1514
handle.on('close', common.mustCall());
1615
const stream = fs.createReadStream(null, { fd: handle });
1716

17+
let output = '';
1818
stream.on('data', common.mustCallAtLeast((data) => {
1919
output += data;
2020
}));
@@ -24,42 +24,60 @@ fs.promises.open(file, 'r').then(common.mustCall((handle) => {
2424
}));
2525

2626
stream.on('close', common.mustCall());
27-
}));
27+
}).then(common.mustCall());
2828

29-
fs.promises.open(file, 'r').then(common.mustCall((handle) => {
29+
fs.promises.open(file, 'r').then((handle) => {
3030
handle.on('close', common.mustCall());
3131
const stream = fs.createReadStream(null, { fd: handle });
3232
stream.on('data', common.mustNotCall());
3333
stream.on('close', common.mustCall());
3434

35-
handle.close();
36-
}));
35+
return handle.close();
36+
}).then(common.mustCall());
3737

38-
fs.promises.open(file, 'r').then(common.mustCall((handle) => {
38+
fs.promises.open(file, 'r').then((handle) => {
3939
handle.on('close', common.mustCall());
4040
const stream = fs.createReadStream(null, { fd: handle });
4141
stream.on('close', common.mustCall());
4242

4343
stream.on('data', common.mustCall(() => {
4444
handle.close();
4545
}));
46-
}));
46+
}).then(common.mustCall());
4747

48-
fs.promises.open(file, 'r').then(common.mustCall((handle) => {
48+
fs.promises.open(file, 'r').then((handle) => {
4949
handle.on('close', common.mustCall());
5050
const stream = fs.createReadStream(null, { fd: handle });
5151
stream.on('close', common.mustCall());
5252

5353
stream.close();
54-
}));
54+
}).then(common.mustCall());
5555

56-
fs.promises.open(file, 'r').then(common.mustCall((handle) => {
56+
fs.promises.open(file, 'r').then((handle) => {
5757
assert.throws(() => {
5858
fs.createReadStream(null, { fd: handle, fs });
5959
}, {
6060
code: 'ERR_METHOD_NOT_IMPLEMENTED',
6161
name: 'Error',
6262
message: 'The FileHandle with fs method is not implemented'
6363
});
64-
handle.close();
65-
}));
64+
return handle.close();
65+
}).then(common.mustCall());
66+
67+
fs.promises.open(file, 'r').then((handle) => {
68+
const { read: originalReadFunction } = handle;
69+
handle.read = common.mustCallAtLeast(function read() {
70+
return Reflect.apply(originalReadFunction, this, arguments);
71+
});
72+
73+
const stream = fs.createReadStream(null, { fd: handle });
74+
75+
let output = '';
76+
stream.on('data', common.mustCallAtLeast((data) => {
77+
output += data;
78+
}));
79+
80+
stream.on('end', common.mustCall(() => {
81+
assert.strictEqual(output, input);
82+
}));
83+
}).then(common.mustCall());

test/parallel/test-fs-write-stream-file-handle.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const input = 'hello world';
99

1010
tmpdir.refresh();
1111

12-
fs.promises.open(file, 'w+').then(common.mustCall((handle) => {
12+
fs.promises.open(file, 'w+').then((handle) => {
1313
handle.on('close', common.mustCall());
1414
const stream = fs.createWriteStream(null, { fd: handle });
1515

@@ -18,4 +18,27 @@ fs.promises.open(file, 'w+').then(common.mustCall((handle) => {
1818
const output = fs.readFileSync(file, 'utf-8');
1919
assert.strictEqual(output, input);
2020
}));
21-
}));
21+
}).then(common.mustCall());
22+
23+
fs.promises.open(file, 'w+').then((handle) => {
24+
let calls = 0;
25+
const {
26+
write: originalWriteFunction,
27+
writev: originalWritevFunction
28+
} = handle;
29+
handle.write = function write() {
30+
calls++;
31+
return Reflect.apply(originalWriteFunction, this, arguments);
32+
};
33+
handle.writev = function writev() {
34+
calls++;
35+
return Reflect.apply(originalWritevFunction, this, arguments);
36+
};
37+
const stream = fs.createWriteStream(null, { fd: handle });
38+
39+
stream.end(input);
40+
stream.on('close', common.mustCall(() => {
41+
assert(calls > 0, 'expected at least one call to fileHandle.write or ' +
42+
'fileHandle.writev, got 0');
43+
}));
44+
}).then(common.mustCall());

0 commit comments

Comments
 (0)