Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! test: fix writefile with fd
  • Loading branch information
Linkgoron committed May 26, 2021
commit 280e74593c34c6019e0856d552419c0c99fc8af4
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
}
}
});