From 639d0561fad7303da84d993bd9718b5caab9c608 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 4 Jun 2018 13:03:58 +0200 Subject: [PATCH] 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 Reviewed-By: Trivikram Kamat --- test/parallel/test-fs-append-file.js | 43 ++++++++++++++++++---------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js index 9ab36a67767946..05dcee43e6cf5e 100644 --- a/test/parallel/test-fs-append-file.js +++ b/test/parallel/test-fs-append-file.js @@ -100,23 +100,37 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; .catch(throwNextTick); } -// test that appendFile accepts buffers -const filename3 = join(tmpdir.path, 'append3.txt'); -fs.writeFileSync(filename3, currentFileData); +// test that appendFile accepts buffers (callback API) +{ + 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) { - assert.ifError(e); + fs.appendFile(filename, buf, common.mustCall((e) => { + assert.ifError(e); - ncallbacks++; + fs.readFile(filename, common.mustCall((e, buffer) => { + assert.ifError(e); + assert.strictEqual(buf.length + currentFileData.length, buffer.length); + })); + })); +} - fs.readFile(filename3, function(e, buffer) { - assert.ifError(e); - ncallbacks++; - 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. const filename4 = join(tmpdir.path, 'append4.txt'); @@ -177,9 +191,8 @@ assert.throws( { code: 'ERR_INVALID_CALLBACK' }); process.on('exit', function() { - assert.strictEqual(ncallbacks, 8); + assert.strictEqual(ncallbacks, 6); - fs.unlinkSync(filename3); fs.unlinkSync(filename4); fs.unlinkSync(filename5); });