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: fix writefile with fd #38820

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions test/parallel/test-fs-writefile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ const join = require('path').join;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const fdsToCloseOnExit = [];
{
/* writeFileSync() test. */
const filename = join(tmpdir.path, 'test.txt');

/* Open the file descriptor. */
const fd = fs.openSync(filename, 'w');
fdsToCloseOnExit.push(fd);

/* Write only five characters, so that the position moves to five. */
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5);
Expand All @@ -28,9 +30,6 @@ tmpdir.refresh();

/* New content should be written at position five, instead of zero. */
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');

/* Close the file descriptor. */
fs.closeSync(fd);
}

{
Expand All @@ -39,6 +38,7 @@ tmpdir.refresh();

/* Open the file descriptor. */
fs.open(file, 'w', common.mustSucceed((fd) => {
fdsToCloseOnExit.push(fd);
/* Write only five characters, so that the position moves to five. */
fs.write(fd, 'Hello', common.mustSucceed((bytes) => {
assert.strictEqual(bytes, 5);
Expand All @@ -48,9 +48,6 @@ tmpdir.refresh();
fs.writeFile(fd, 'World', common.mustSucceed(() => {
/* New content should be written at position five, instead of zero. */
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'HelloWorld');

/* Close the file descriptor. */
fs.closeSync(fd);
}));
}));
}));
Expand All @@ -65,6 +62,7 @@ tmpdir.refresh();
const file = join(tmpdir.path, 'test.txt');

fs.open(file, 'r', common.mustSucceed((fd) => {
fdsToCloseOnExit.push(fd);
fs.writeFile(fd, 'World', common.expectsError(expectedError));
}));
}
Expand All @@ -76,10 +74,21 @@ tmpdir.refresh();
const file = join(tmpdir.path, 'test.txt');

fs.open(file, 'w', common.mustSucceed((fd) => {
fdsToCloseOnExit.push(fd);
fs.writeFile(fd, 'World', { signal }, common.expectsError({
name: 'AbortError'
}));
}));

controller.abort();
}

process.on('beforeExit', () => {
for (const fd of fdsToCloseOnExit) {
try {
fs.closeSync(fd);
} catch {
// Failed to close, ignore
}
}
});