test: apply promises API to third appendFile test

Add tests for `fs.promises.appendFile()` to the third (of five) test
case in `test-fs-access`. (First and second test cases already have
promises API versions.)

PR-URL: https://github.com/nodejs/node/pull/21131
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Rich Trott 2018-06-04 13:03:58 +02:00
parent cbc3dd997e
commit 16d63e629d

View File

@ -100,23 +100,37 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
.catch(throwNextTick); .catch(throwNextTick);
} }
// test that appendFile accepts buffers // test that appendFile accepts buffers (callback API)
const filename3 = join(tmpdir.path, 'append3.txt'); {
fs.writeFileSync(filename3, currentFileData); const filename = join(tmpdir.path, 'append-buffer.txt');
fs.writeFileSync(filename, currentFileData);
const buf = Buffer.from(s, 'utf8'); const buf = Buffer.from(s, 'utf8');
fs.appendFile(filename3, buf, function(e) { fs.appendFile(filename, buf, common.mustCall((e) => {
assert.ifError(e);
ncallbacks++;
fs.readFile(filename3, function(e, buffer) {
assert.ifError(e); assert.ifError(e);
ncallbacks++;
assert.strictEqual(buf.length + currentFileData.length, buffer.length); fs.readFile(filename, common.mustCall((e, buffer) => {
}); assert.ifError(e);
}); assert.strictEqual(buf.length + currentFileData.length, buffer.length);
}));
}));
}
// test that appendFile accepts buffers (promises API)
{
const filename = join(tmpdir.path, 'append-buffer-promises.txt');
fs.writeFileSync(filename, currentFileData);
const buf = Buffer.from(s, 'utf8');
fs.promises.appendFile(filename, buf)
.then(common.mustCall(() => fs.promises.readFile(filename)))
.then((buffer) => {
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
})
.catch(throwNextTick);
}
// test that appendFile accepts numbers. // test that appendFile accepts numbers.
const filename4 = join(tmpdir.path, 'append4.txt'); const filename4 = join(tmpdir.path, 'append4.txt');
@ -177,9 +191,8 @@ assert.throws(
{ code: 'ERR_INVALID_CALLBACK' }); { code: 'ERR_INVALID_CALLBACK' });
process.on('exit', function() { process.on('exit', function() {
assert.strictEqual(ncallbacks, 8); assert.strictEqual(ncallbacks, 6);
fs.unlinkSync(filename3);
fs.unlinkSync(filename4); fs.unlinkSync(filename4);
fs.unlinkSync(filename5); fs.unlinkSync(filename5);
}); });