Skip to content

Commit

Permalink
test: fix writefile with fd
Browse files Browse the repository at this point in the history
fix writefile with fd so that it'll close the fds
that is uses during the test.

PR-URL: #38820
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
Linkgoron authored and targos committed May 28, 2021
1 parent ef4bf8f commit 0222a10
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions test/parallel/test-fs-writefile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@ tmpdir.refresh();

/* Open the file descriptor. */
const fd = fs.openSync(filename, 'w');
try {
/* Write only five characters, so that the position moves to five. */
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5);
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello');

/* Write only five characters, so that the position moves to five. */
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5);
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello');

/* Write some more with writeFileSync(). */
fs.writeFileSync(fd, 'World');

/* New content should be written at position five, instead of zero. */
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
/* Write some more with writeFileSync(). */
fs.writeFileSync(fd, 'World');

/* Close the file descriptor. */
fs.closeSync(fd);
/* New content should be written at position five, instead of zero. */
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
} finally {
fs.closeSync(fd);
}
}

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

{
/* writeFile() test. */
const file = join(tmpdir.path, 'test1.txt');

/* 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 +60,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 +74,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,6 +86,7 @@ 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'
}));
Expand Down

0 comments on commit 0222a10

Please sign in to comment.