diff --git a/test/parallel/test-fs-promises-write-optional-params.js b/test/parallel/test-fs-promises-write-optional-params.js index fb16af15e857d7..4dd87155153769 100644 --- a/test/parallel/test-fs-promises-write-optional-params.js +++ b/test/parallel/test-fs-promises-write-optional-params.js @@ -33,26 +33,33 @@ async function testInvalid(dest, expectedCode, ...params) { async function testValid(dest, buffer, options) { const length = options?.length; const offset = options?.offset; - let fh; - try { - fh = await fsPromises.open(dest, 'w+'); - const writeResult = await fh.write(buffer, options); - const writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer); + let fh, writeResult, writeBufCopy, readResult, readBufCopy; - const readResult = await fh.read(buffer, options); - const readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer); + try { + fh = await fsPromises.open(dest, 'w'); + writeResult = await fh.write(buffer, options); + writeBufCopy = Uint8Array.prototype.slice.call(writeResult.buffer); + } finally { + await fh?.close(); + } - assert.ok(writeResult.bytesWritten >= readResult.bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(writeResult.bytesWritten, length); - } - if (offset === undefined || offset === 0) { - assert.deepStrictEqual(writeBufCopy, readBufCopy); - } - assert.deepStrictEqual(writeResult.buffer, readResult.buffer); + try { + fh = await fsPromises.open(dest, 'r'); + readResult = await fh.read(buffer, options); + readBufCopy = Uint8Array.prototype.slice.call(readResult.buffer); } finally { await fh?.close(); } + + assert.ok(writeResult.bytesWritten >= readResult.bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(writeResult.bytesWritten, length); + assert.strictEqual(readResult.bytesRead, length); + } + if (offset === undefined || offset === 0) { + assert.deepStrictEqual(writeBufCopy, readBufCopy); + } + assert.deepStrictEqual(writeResult.buffer, readResult.buffer); } (async () => { diff --git a/test/parallel/test-fs-write-optional-params.js b/test/parallel/test-fs-write-optional-params.js index 5072884b95c184..6f61f5b41864a2 100644 --- a/test/parallel/test-fs-write-optional-params.js +++ b/test/parallel/test-fs-write-optional-params.js @@ -29,22 +29,26 @@ function testValidCb(buffer, options, index, callback) { const length = options?.length; const offset = options?.offset; const dest = path.resolve(tmpdir.path, `rwopt_valid_${index}`); - fs.open(dest, 'w+', common.mustSucceed((fd) => { + fs.open(dest, 'w', common.mustSucceed((fd) => { fs.write(fd, buffer, options, common.mustSucceed((bytesWritten, bufferWritten) => { const writeBufCopy = Uint8Array.prototype.slice.call(bufferWritten); + fs.close(fd, common.mustSucceed(() => { + fs.open(dest, 'r', common.mustSucceed((fd) => { + fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => { + const readBufCopy = Uint8Array.prototype.slice.call(bufferRead); - fs.read(fd, buffer, options, common.mustSucceed((bytesRead, bufferRead) => { - const readBufCopy = Uint8Array.prototype.slice.call(bufferRead); - - assert.ok(bytesWritten >= bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(bytesWritten, length); - } - if (offset === undefined || offset === 0) { - assert.deepStrictEqual(writeBufCopy, readBufCopy); - } - assert.deepStrictEqual(bufferWritten, bufferRead); - fs.close(fd, common.mustSucceed(callback)); + assert.ok(bytesWritten >= bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(bytesWritten, length); + assert.strictEqual(bytesRead, length); + } + if (offset === undefined || offset === 0) { + assert.deepStrictEqual(writeBufCopy, readBufCopy); + } + assert.deepStrictEqual(bufferWritten, bufferRead); + fs.close(fd, common.mustSucceed(callback)); + })); + })); })); })); })); diff --git a/test/parallel/test-fs-write-sync-optional-params.js b/test/parallel/test-fs-write-sync-optional-params.js index eeaa9c2bbd5595..ac2c501f682242 100644 --- a/test/parallel/test-fs-write-sync-optional-params.js +++ b/test/parallel/test-fs-write-sync-optional-params.js @@ -32,19 +32,27 @@ function testInvalid(dest, expectedCode, ...bufferAndOptions) { function testValid(dest, buffer, options) { const length = options?.length; - let fd; + let fd, bytesWritten, bytesRead; + try { - fd = fs.openSync(dest, 'w+'); - const bytesWritten = fs.writeSync(fd, buffer, options); - const bytesRead = fs.readSync(fd, buffer, options); + fd = fs.openSync(dest, 'w'); + bytesWritten = fs.writeSync(fd, buffer, options); + } finally { + if (fd != null) fs.closeSync(fd); + } - assert.ok(bytesWritten >= bytesRead); - if (length !== undefined && length !== null) { - assert.strictEqual(bytesWritten, length); - } + try { + fd = fs.openSync(dest, 'r'); + bytesRead = fs.readSync(fd, buffer, options); } finally { if (fd != null) fs.closeSync(fd); } + + assert.ok(bytesWritten >= bytesRead); + if (length !== undefined && length !== null) { + assert.strictEqual(bytesWritten, length); + assert.strictEqual(bytesRead, length); + } } {