Skip to content

Commit 8dce6dc

Browse files
committed
fs: improve large file warning handling in readFile tests
1 parent c4337a2 commit 8dce6dc

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/internal/fs/promises.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ async function readFileHandle(filehandle, options) {
536536
process.emitWarning(
537537
`Detected \`fs.readFile()\` to read a file larger than the recommended limit (${size} > ${limit} bytes). Please consider using \`fs.createReadStream()\` instead to minimize memory overhead and increase performance.`,
538538
'LargeFileWarning',
539-
ERR_FS_FILE_TOO_LARGE
539+
ERR_FS_FILE_TOO_LARGE,
540540
);
541541
}
542542

test/parallel/test-fs-promises-file-handle-readFile.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,24 @@ async function doReadAndCancel() {
138138

139139
await using fileHandle = await open(newFile, 'r');
140140

141-
await assert.rejects(fileHandle.readFile(), {
142-
name: 'RangeError',
143-
code: 'ERR_FS_FILE_TOO_LARGE'
141+
let warningEmitted = false;
142+
let warningMessage = '';
143+
process.once('warning', (warning) => {
144+
if (warning.name === 'LargeFileWarning') {
145+
warningEmitted = true;
146+
warningMessage = warning.message;
147+
}
144148
});
149+
150+
const data = await fileHandle.readFile();
151+
152+
const expectedWarningMsg = 'Expected LargeFileWarning to be emitted for 5GB file';
153+
assert.strictEqual(warningEmitted, true, expectedWarningMsg);
154+
155+
assert.match(warningMessage,
156+
/larger than the recommended limit \(\d+ > \d+ bytes\)/);
157+
158+
console.log(`File read successfully, size: ${data.length} bytes`);
145159
}
146160
}
147161
}

0 commit comments

Comments
 (0)