Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add promise API test for appendFile() #20842

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
42 changes: 27 additions & 15 deletions test/parallel/test-fs-append-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,35 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
.catch(throwNextTick);
}

// test that appends data to a non empty file
const filename2 = join(tmpdir.path, 'append2.txt');
fs.writeFileSync(filename2, currentFileData);
// test that appends data to a non-empty file (callback API)
{
const filename = join(tmpdir.path, 'append-non-empty.txt');
fs.writeFileSync(filename, currentFileData);

fs.appendFile(filename2, s, function(e) {
assert.ifError(e);
fs.appendFile(filename, s, common.mustCall(function(e) {
assert.ifError(e);

ncallbacks++;
fs.readFile(filename, common.mustCall(function(e, buffer) {
assert.ifError(e);
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
buffer.length);
}));
}));
}

fs.readFile(filename2, function(e, buffer) {
assert.ifError(e);
ncallbacks++;
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
buffer.length);
});
});
// test that appends data to a non-empty file (promise API)
{
const filename = join(tmpdir.path, 'append-non-empty-promise.txt');
fs.writeFileSync(filename, currentFileData);

fs.promises.appendFile(filename, s)
.then(common.mustCall(() => fs.promises.readFile(filename)))
.then((buffer) => {
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
buffer.length);
})
.catch(throwNextTick);
}

// test that appendFile accepts buffers
const filename3 = join(tmpdir.path, 'append3.txt');
Expand Down Expand Up @@ -164,9 +177,8 @@ assert.throws(
{ code: 'ERR_INVALID_CALLBACK' });

process.on('exit', function() {
assert.strictEqual(10, ncallbacks);
assert.strictEqual(ncallbacks, 8);

fs.unlinkSync(filename2);
fs.unlinkSync(filename3);
fs.unlinkSync(filename4);
fs.unlinkSync(filename5);
Expand Down