Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c492ba

Browse filesBrowse files
Linkgorondanielleadams
authored andcommitted
test: fix writefile with fd
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>
1 parent 24cca7c commit 3c492ba
Copy full SHA for 3c492ba

File tree

Expand file treeCollapse file tree

1 file changed

+25
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-14
lines changed
Open diff view settings
Collapse file

‎test/parallel/test-fs-writefile-with-fd.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-writefile-with-fd.js
+25-14Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,39 @@ tmpdir.refresh();
1818

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

22-
/* Write only five characters, so that the position moves to five. */
23-
assert.deepStrictEqual(fs.writeSync(fd, 'Hello'), 5);
24-
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello');
25-
26-
/* Write some more with writeFileSync(). */
27-
fs.writeFileSync(fd, 'World');
28-
29-
/* New content should be written at position five, instead of zero. */
30-
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
26+
/* Write some more with writeFileSync(). */
27+
fs.writeFileSync(fd, 'World');
3128

32-
/* Close the file descriptor. */
33-
fs.closeSync(fd);
29+
/* New content should be written at position five, instead of zero. */
30+
assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'HelloWorld');
31+
} finally {
32+
fs.closeSync(fd);
33+
}
3434
}
3535

36+
const fdsToCloseOnExit = [];
37+
process.on('beforeExit', common.mustCall(() => {
38+
for (const fd of fdsToCloseOnExit) {
39+
try {
40+
fs.closeSync(fd);
41+
} catch {
42+
// Failed to close, ignore
43+
}
44+
}
45+
}));
46+
3647
{
3748
/* writeFile() test. */
3849
const file = join(tmpdir.path, 'test1.txt');
3950

4051
/* Open the file descriptor. */
4152
fs.open(file, 'w', common.mustSucceed((fd) => {
53+
fdsToCloseOnExit.push(fd);
4254
/* Write only five characters, so that the position moves to five. */
4355
fs.write(fd, 'Hello', common.mustSucceed((bytes) => {
4456
assert.strictEqual(bytes, 5);
@@ -48,9 +60,6 @@ tmpdir.refresh();
4860
fs.writeFile(fd, 'World', common.mustSucceed(() => {
4961
/* New content should be written at position five, instead of zero. */
5062
assert.deepStrictEqual(fs.readFileSync(file).toString(), 'HelloWorld');
51-
52-
/* Close the file descriptor. */
53-
fs.closeSync(fd);
5463
}));
5564
}));
5665
}));
@@ -65,6 +74,7 @@ tmpdir.refresh();
6574
const file = join(tmpdir.path, 'test.txt');
6675

6776
fs.open(file, 'r', common.mustSucceed((fd) => {
77+
fdsToCloseOnExit.push(fd);
6878
fs.writeFile(fd, 'World', common.expectsError(expectedError));
6979
}));
7080
}
@@ -76,6 +86,7 @@ tmpdir.refresh();
7686
const file = join(tmpdir.path, 'test.txt');
7787

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.