From 13d6a56c7d66cd5d48af8a0aaf8107e1c5562374 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 6 Sep 2021 18:17:00 +0200 Subject: [PATCH] test: improve test coverage of `fs.ReadStream` with `FileHandle` PR-URL: https://github.com/nodejs/node/pull/40018 Reviewed-By: James M Snell Reviewed-By: Zijian Liu --- .../test-fs-read-stream-file-handle.js | 44 +++++++++++++------ .../test-fs-write-stream-file-handle.js | 27 +++++++++++- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-fs-read-stream-file-handle.js b/test/parallel/test-fs-read-stream-file-handle.js index 063075aa25f64e..810745149dc5bf 100644 --- a/test/parallel/test-fs-read-stream-file-handle.js +++ b/test/parallel/test-fs-read-stream-file-handle.js @@ -7,14 +7,14 @@ const tmpdir = require('../common/tmpdir'); const file = path.join(tmpdir.path, 'read_stream_filehandle_test.txt'); const input = 'hello world'; -let output = ''; tmpdir.refresh(); fs.writeFileSync(file, input); -fs.promises.open(file, 'r').then(common.mustCall((handle) => { +fs.promises.open(file, 'r').then((handle) => { handle.on('close', common.mustCall()); const stream = fs.createReadStream(null, { fd: handle }); + let output = ''; stream.on('data', common.mustCallAtLeast((data) => { output += data; })); @@ -24,18 +24,18 @@ fs.promises.open(file, 'r').then(common.mustCall((handle) => { })); stream.on('close', common.mustCall()); -})); +}).then(common.mustCall()); -fs.promises.open(file, 'r').then(common.mustCall((handle) => { +fs.promises.open(file, 'r').then((handle) => { handle.on('close', common.mustCall()); const stream = fs.createReadStream(null, { fd: handle }); stream.on('data', common.mustNotCall()); stream.on('close', common.mustCall()); - handle.close(); -})); + return handle.close(); +}).then(common.mustCall()); -fs.promises.open(file, 'r').then(common.mustCall((handle) => { +fs.promises.open(file, 'r').then((handle) => { handle.on('close', common.mustCall()); const stream = fs.createReadStream(null, { fd: handle }); stream.on('close', common.mustCall()); @@ -43,17 +43,17 @@ fs.promises.open(file, 'r').then(common.mustCall((handle) => { stream.on('data', common.mustCall(() => { handle.close(); })); -})); +}).then(common.mustCall()); -fs.promises.open(file, 'r').then(common.mustCall((handle) => { +fs.promises.open(file, 'r').then((handle) => { handle.on('close', common.mustCall()); const stream = fs.createReadStream(null, { fd: handle }); stream.on('close', common.mustCall()); stream.close(); -})); +}).then(common.mustCall()); -fs.promises.open(file, 'r').then(common.mustCall((handle) => { +fs.promises.open(file, 'r').then((handle) => { assert.throws(() => { fs.createReadStream(null, { fd: handle, fs }); }, { @@ -61,5 +61,23 @@ fs.promises.open(file, 'r').then(common.mustCall((handle) => { name: 'Error', message: 'The FileHandle with fs method is not implemented' }); - handle.close(); -})); + return handle.close(); +}).then(common.mustCall()); + +fs.promises.open(file, 'r').then((handle) => { + const { read: originalReadFunction } = handle; + handle.read = common.mustCallAtLeast(function read() { + return Reflect.apply(originalReadFunction, this, arguments); + }); + + const stream = fs.createReadStream(null, { fd: handle }); + + let output = ''; + stream.on('data', common.mustCallAtLeast((data) => { + output += data; + })); + + stream.on('end', common.mustCall(() => { + assert.strictEqual(output, input); + })); +}).then(common.mustCall()); diff --git a/test/parallel/test-fs-write-stream-file-handle.js b/test/parallel/test-fs-write-stream-file-handle.js index 37d9a81d7c12e4..23ddf21c50f3f2 100644 --- a/test/parallel/test-fs-write-stream-file-handle.js +++ b/test/parallel/test-fs-write-stream-file-handle.js @@ -9,7 +9,7 @@ const input = 'hello world'; tmpdir.refresh(); -fs.promises.open(file, 'w+').then(common.mustCall((handle) => { +fs.promises.open(file, 'w+').then((handle) => { handle.on('close', common.mustCall()); const stream = fs.createWriteStream(null, { fd: handle }); @@ -18,4 +18,27 @@ fs.promises.open(file, 'w+').then(common.mustCall((handle) => { const output = fs.readFileSync(file, 'utf-8'); assert.strictEqual(output, input); })); -})); +}).then(common.mustCall()); + +fs.promises.open(file, 'w+').then((handle) => { + let calls = 0; + const { + write: originalWriteFunction, + writev: originalWritevFunction + } = handle; + handle.write = function write() { + calls++; + return Reflect.apply(originalWriteFunction, this, arguments); + }; + handle.writev = function writev() { + calls++; + return Reflect.apply(originalWritevFunction, this, arguments); + }; + const stream = fs.createWriteStream(null, { fd: handle }); + + stream.end(input); + stream.on('close', common.mustCall(() => { + assert(calls > 0, 'expected at least one call to fileHandle.write or ' + + 'fileHandle.writev, got 0'); + })); +}).then(common.mustCall());