From 23bae8a6700216e2cd23050cc799507f80dfd8b6 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Wed, 26 May 2021 18:44:53 +0300 Subject: [PATCH 1/2] test: fix writefile with fd fix writefile with fd so that it'll close the fds that is uses during the test. --- test/parallel/test-fs-writefile-with-fd.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-fs-writefile-with-fd.js b/test/parallel/test-fs-writefile-with-fd.js index 2af5b39adbc14a..e86ffb8ff2fd9a 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -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); @@ -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); } { @@ -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); @@ -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); })); })); })); @@ -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)); })); } @@ -76,6 +74,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' })); @@ -83,3 +82,13 @@ tmpdir.refresh(); controller.abort(); } + +process.on('beforeExit', () => { + for (const fd of fdsToCloseOnExit) { + try { + fs.closeSync(fd); + } catch { + // Failed to close, ignore + } + } +}); From 280e74593c34c6019e0856d552419c0c99fc8af4 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Wed, 26 May 2021 19:06:25 +0300 Subject: [PATCH 2/2] fixup! test: fix writefile with fd --- test/parallel/test-fs-writefile-with-fd.js | 42 +++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/test/parallel/test-fs-writefile-with-fd.js b/test/parallel/test-fs-writefile-with-fd.js index e86ffb8ff2fd9a..4cabdf347ba3ea 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -12,26 +12,38 @@ 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); - assert.deepStrictEqual(fs.readFileSync(filename).toString(), 'Hello'); + 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 some more with writeFileSync(). */ - fs.writeFileSync(fd, 'World'); + /* 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'); + /* 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'); @@ -82,13 +94,3 @@ const fdsToCloseOnExit = []; controller.abort(); } - -process.on('beforeExit', () => { - for (const fd of fdsToCloseOnExit) { - try { - fs.closeSync(fd); - } catch { - // Failed to close, ignore - } - } -});