summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2018-05-19 21:04:12 -0700
committerAnatoli Papirovski <apapirovski@mac.com>2018-05-22 12:31:17 +0400
commita3d174c525980cd5ad7a040fcacbe9de9f024905 (patch)
tree483920cb18f334368eeb8cbf58508ad4a98e923d
parent3654cd4cdaf2ab84d0e6a190c3b9e89e86726a88 (diff)
downloadandroid-node-v8-a3d174c525980cd5ad7a040fcacbe9de9f024905.tar.gz
android-node-v8-a3d174c525980cd5ad7a040fcacbe9de9f024905.tar.bz2
android-node-v8-a3d174c525980cd5ad7a040fcacbe9de9f024905.zip
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>
-rw-r--r--test/parallel/test-fs-append-file.js42
1 files changed, 27 insertions, 15 deletions
diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js
index 0b0cf5cdb2..9ab36a6776 100644
--- a/test/parallel/test-fs-append-file.js
+++ b/test/parallel/test-fs-append-file.js
@@ -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');
@@ -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);