test: dispose of filehandles in filehandle.read tests

PR-URL: https://github.com/nodejs/node/pull/58543
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Livia Medeiros 2025-06-05 09:52:48 +08:00 committed by GitHub
parent 3f81645f8c
commit 9e35ddca44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 9 deletions

View File

@ -29,7 +29,7 @@ assert.throws(
);
(async () => {
const filehandle = await fsPromises.open(filepath, 'r');
await using filehandle = await fsPromises.open(filepath, 'r');
assert.rejects(
() => filehandle.read(buffer, 0, 1, 0),
{

View File

@ -35,30 +35,25 @@ fs.open(filepath, 'r', common.mustSucceed((fd) => {
}));
}));
let filehandle = null;
// Tests for promises api
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
await using filehandle = await fsPromises.open(filepath, 'r');
const readObject = await filehandle.read(buf, { offset: null });
assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());
// Undocumented: omitted position works the same as position === null
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
await using filehandle = await fsPromises.open(filepath, 'r');
const readObject = await filehandle.read(buf, null, buf.length);
assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());
(async () => {
filehandle = await fsPromises.open(filepath, 'r');
await using filehandle = await fsPromises.open(filepath, 'r');
const readObject = await filehandle.read(buf, null, buf.length, 0);
assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());