From d066e1d295f4c5c250b5bce43122c9fbe16e666b Mon Sep 17 00:00:00 2001 From: Mert Can Altin Date: Wed, 27 Nov 2024 21:36:46 +0300 Subject: [PATCH] test: migrate tests to use node:test module for better test structure --- test/parallel/test-filehandle-close.js | 20 ++++++----- ...s-promises-file-handle-aggregate-errors.js | 36 +++++++++---------- ...st-fs-promises-file-handle-close-errors.js | 25 +++++-------- 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/test/parallel/test-filehandle-close.js b/test/parallel/test-filehandle-close.js index 6e55f3f06d19e7..2feb21afde4dbe 100644 --- a/test/parallel/test-filehandle-close.js +++ b/test/parallel/test-filehandle-close.js @@ -2,16 +2,18 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); +const { test } = require('node:test'); -// Test that using FileHandle.close to close an already-closed fd fails -// with EBADF. - -(async function() { +test('FileHandle.close should fail with EBADF when closing an already closed fd', async () => { const fh = await fs.promises.open(__filename); fs.closeSync(fh.fd); - await assert.rejects(() => fh.close(), { - code: 'EBADF', - syscall: 'close' - }); -})().then(common.mustCall()); + // Test that closing an already closed fd results in EBADF + await assert.rejects( + () => fh.close(), + { + code: 'EBADF', + syscall: 'close' + } + ); +}).then(common.mustCall()); diff --git a/test/parallel/test-fs-promises-file-handle-aggregate-errors.js b/test/parallel/test-fs-promises-file-handle-aggregate-errors.js index f53ce1eeaf0df3..b93079b30f414b 100644 --- a/test/parallel/test-fs-promises-file-handle-aggregate-errors.js +++ b/test/parallel/test-fs-promises-file-handle-aggregate-errors.js @@ -4,23 +4,15 @@ const common = require('../common'); const tmpdir = require('../common/tmpdir'); -// The following tests validate aggregate errors are thrown correctly -// when both an operation and close throw. +const { test } = require('node:test'); +const { readFile, writeFile, truncate, lchmod } = require('node:fs/promises'); +const { FileHandle } = require('internal/fs/promises'); -const { - readFile, - writeFile, - truncate, - lchmod, -} = require('fs/promises'); -const { - FileHandle, -} = require('internal/fs/promises'); - -const assert = require('assert'); +const assert = require('node:assert'); const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd'); let count = 0; + async function createFile() { const filePath = tmpdir.resolve(`aggregate_errors_${++count}.txt`); await writeFile(filePath, 'content'); @@ -45,22 +37,30 @@ async function checkAggregateError(op) { const opError = new Error('INTERNAL_ERROR'); opError.code = 123; throw opError; - } + }, }); - await assert.rejects(op(filePath), common.mustCall((err) => { + // Perform the operation and check the aggregate error + await assert.rejects(op(filePath), (err) => { assert.strictEqual(err.name, 'AggregateError'); assert.strictEqual(err.code, 123); assert.strictEqual(err.errors.length, 2); + + // Check the individual errors assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR'); + assert.strictEqual(err.errors[0].code, 123); + assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR'); + assert.strictEqual(err.errors[1].code, 456); + return true; - })); + }); } finally { Object.defineProperty(FileHandle.prototype, 'fd', originalFd); } } -(async function() { + +test('Test aggregate errors for file operations', async () => { tmpdir.refresh(); await checkAggregateError((filePath) => truncate(filePath)); await checkAggregateError((filePath) => readFile(filePath)); @@ -68,4 +68,4 @@ async function checkAggregateError(op) { if (common.isMacOS) { await checkAggregateError((filePath) => lchmod(filePath, 0o777)); } -})().then(common.mustCall()); +}); diff --git a/test/parallel/test-fs-promises-file-handle-close-errors.js b/test/parallel/test-fs-promises-file-handle-close-errors.js index 8d0a1bad460552..b07b192d1ae220 100644 --- a/test/parallel/test-fs-promises-file-handle-close-errors.js +++ b/test/parallel/test-fs-promises-file-handle-close-errors.js @@ -4,23 +4,15 @@ const common = require('../common'); const tmpdir = require('../common/tmpdir'); -// The following tests validate aggregate errors are thrown correctly -// when both an operation and close throw. +const { test } = require('node:test'); +const { readFile, writeFile, truncate, lchmod } = require('fs/promises'); +const { FileHandle } = require('internal/fs/promises'); -const { - readFile, - writeFile, - truncate, - lchmod, -} = require('fs/promises'); -const { - FileHandle, -} = require('internal/fs/promises'); - -const assert = require('assert'); +const assert = require('node:assert'); const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd'); let count = 0; + async function createFile() { const filePath = tmpdir.resolve(`close_errors_${++count}.txt`); await writeFile(filePath, 'content'); @@ -43,7 +35,7 @@ async function checkCloseError(op) { throw closeError; }; return originalFd.get.call(this); - } + }, }); await assert.rejects(op(filePath), { @@ -55,7 +47,8 @@ async function checkCloseError(op) { Object.defineProperty(FileHandle.prototype, 'fd', originalFd); } } -(async function() { + +test('File operations with close errors', async () => { tmpdir.refresh(); await checkCloseError((filePath) => truncate(filePath)); await checkCloseError((filePath) => readFile(filePath)); @@ -63,4 +56,4 @@ async function checkCloseError(op) { if (common.isMacOS) { await checkCloseError((filePath) => lchmod(filePath, 0o777)); } -})().then(common.mustCall()); +}).then(common.mustCall());