Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate tests to use node:test module for better test structure for FS #56031

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions test/parallel/test-filehandle-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
36 changes: 18 additions & 18 deletions test/parallel/test-fs-promises-file-handle-aggregate-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -45,27 +37,35 @@ 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));
await checkAggregateError((filePath) => writeFile(filePath, '123'));
if (common.isMacOS) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());
});
25 changes: 9 additions & 16 deletions test/parallel/test-fs-promises-file-handle-close-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -43,7 +35,7 @@ async function checkCloseError(op) {
throw closeError;
};
return originalFd.get.call(this);
}
},
});

await assert.rejects(op(filePath), {
Expand All @@ -55,12 +47,13 @@ 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));
await checkCloseError((filePath) => writeFile(filePath, '123'));
if (common.isMacOS) {
await checkCloseError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());
}).then(common.mustCall());
Loading