test: add promise API test for appendFile()

Apply the second of five test cases in test-fs-append-fil to the
promise-based API in addition to the callback-based API.

PR-URL: https://github.com/nodejs/node/pull/20842
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Rich Trott 2018-05-19 21:04:12 -07:00 коммит произвёл Myles Borins
Родитель c1fe9b29b1
Коммит dc29a3b386
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 933B01F40B5CA946
1 изменённых файлов: 28 добавлений и 16 удалений

Просмотреть файл

@ -70,22 +70,35 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
.catch(throwNextTick); .catch(throwNextTick);
} }
// test that appends data to a non empty file // test that appends data to a non-empty file (callback API)
const filename2 = join(tmpdir.path, 'append2.txt'); {
fs.writeFileSync(filename2, currentFileData); const filename = join(tmpdir.path, 'append-non-empty.txt');
fs.writeFileSync(filename, currentFileData);
fs.appendFile(filename2, s, function(e) { fs.appendFile(filename, s, common.mustCall(function(e) {
assert.ifError(e);
ncallbacks++;
fs.readFile(filename2, function(e, buffer) {
assert.ifError(e); assert.ifError(e);
ncallbacks++;
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, fs.readFile(filename, common.mustCall(function(e, buffer) {
buffer.length); assert.ifError(e);
}); 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 // test that appendFile accepts buffers
const filename3 = join(tmpdir.path, 'append3.txt'); const filename3 = join(tmpdir.path, 'append3.txt');
@ -164,9 +177,8 @@ assert.throws(
{ code: 'ERR_INVALID_CALLBACK' }); { code: 'ERR_INVALID_CALLBACK' });
process.on('exit', function() { process.on('exit', function() {
assert.strictEqual(10, ncallbacks); assert.strictEqual(ncallbacks, 8);
fs.unlinkSync(filename2);
fs.unlinkSync(filename3); fs.unlinkSync(filename3);
fs.unlinkSync(filename4); fs.unlinkSync(filename4);
fs.unlinkSync(filename5); fs.unlinkSync(filename5);