From 9e432ca79c20620fa423724e2b1cd76613f72163 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 14 May 2018 21:02:32 -0700 Subject: [PATCH] test: add promise API test for appendFile() Apply the first of five test cases in test-fs-append-file to the promise-based API in addition to the callback-based API. PR-URL: https://github.com/nodejs/node/pull/20739 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- test/parallel/test-fs-append-file.js | 38 ++++++++++++++++++---------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js index 29ae1b2f3b9e9a..0b0cf5cdb2f119 100644 --- a/test/parallel/test-fs-append-file.js +++ b/test/parallel/test-fs-append-file.js @@ -27,8 +27,6 @@ const join = require('path').join; const tmpdir = require('../common/tmpdir'); -const filename = join(tmpdir.path, 'append.txt'); - const currentFileData = 'ABCD'; const n = 220; @@ -44,18 +42,33 @@ let ncallbacks = 0; tmpdir.refresh(); -// test that empty file will be created and have content added -fs.appendFile(filename, s, function(e) { - assert.ifError(e); +const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; - ncallbacks++; +// test that empty file will be created and have content added (callback API) +{ + const filename = join(tmpdir.path, 'append.txt'); - fs.readFile(filename, function(e, buffer) { + fs.appendFile(filename, s, common.mustCall(function(e) { assert.ifError(e); - ncallbacks++; - assert.strictEqual(Buffer.byteLength(s), buffer.length); - }); -}); + + fs.readFile(filename, common.mustCall(function(e, buffer) { + assert.ifError(e); + assert.strictEqual(Buffer.byteLength(s), buffer.length); + })); + })); +} + +// test that empty file will be created and have content added (promise API) +{ + const filename = join(tmpdir.path, 'append-promise.txt'); + + fs.promises.appendFile(filename, s) + .then(common.mustCall(() => fs.promises.readFile(filename))) + .then((buffer) => { + assert.strictEqual(Buffer.byteLength(s), buffer.length); + }) + .catch(throwNextTick); +} // test that appends data to a non empty file const filename2 = join(tmpdir.path, 'append2.txt'); @@ -151,9 +164,8 @@ assert.throws( { code: 'ERR_INVALID_CALLBACK' }); process.on('exit', function() { - assert.strictEqual(12, ncallbacks); + assert.strictEqual(10, ncallbacks); - fs.unlinkSync(filename); fs.unlinkSync(filename2); fs.unlinkSync(filename3); fs.unlinkSync(filename4);