Skip to content

Commit

Permalink
fixup! test: fix writefile with fd
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkgoron committed May 26, 2021
1 parent 23bae8a commit 280e745
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions test/parallel/test-fs-writefile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
}
}
});

0 comments on commit 280e745

Please sign in to comment.