diff options
author | Nitzan Uziely <linkgoron@gmail.com> | 2021-05-26 18:44:53 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-05-28 15:37:56 +0300 |
commit | 0222a10ae66566e0bd6cffd6137c8d7fed72d9d9 (patch) | |
tree | 0ffe9795babe101a5af7974a9e2b46da52915084 /test | |
parent | ef4bf8f0a968400fa9fdb26972730cb73177e88e (diff) |
test: fix writefile with fd
fix writefile with fd so that it'll close the fds
that is uses during the test.
PR-URL: https://github.com/nodejs/node/pull/38820
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-writefile-with-fd.js | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/test/parallel/test-fs-writefile-with-fd.js b/test/parallel/test-fs-writefile-with-fd.js index 2af5b39adbc..4cabdf347ba 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -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); @@ -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); })); })); })); @@ -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)); })); } @@ -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' })); |