summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2018-06-04 14:12:08 +0200
committerRich Trott <rtrott@gmail.com>2018-06-06 21:00:54 -0700
commit8530b58493c1da6c7bf21b4bb59824eda45521b2 (patch)
treefa752ca04228260bbe4734ca23ee0fa5365fa1fe
parent97a4ba3d23b44d8e91980ebd641269572f56871a (diff)
downloadandroid-node-v8-8530b58493c1da6c7bf21b4bb59824eda45521b2.tar.gz
android-node-v8-8530b58493c1da6c7bf21b4bb59824eda45521b2.tar.bz2
android-node-v8-8530b58493c1da6c7bf21b4bb59824eda45521b2.zip
test: apply promises API to fourth appendFile test
Add tests for `fs.promises.appendFile()` to the last test case in `test-fs-access`. (The previous 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>
-rw-r--r--test/parallel/test-fs-append-file.js66
1 files changed, 36 insertions, 30 deletions
diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js
index 2d4c64863f..4d8f66682e 100644
--- a/test/parallel/test-fs-append-file.js
+++ b/test/parallel/test-fs-append-file.js
@@ -38,8 +38,6 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';
-let ncallbacks = 0;
-
tmpdir.refresh();
const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
@@ -178,42 +176,50 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
.catch(throwNextTick);
}
-// test that appendFile accepts file descriptors
-const filename5 = join(tmpdir.path, 'append5.txt');
-fs.writeFileSync(filename5, currentFileData);
-
-fs.open(filename5, 'a+', function(e, fd) {
- assert.ifError(e);
-
- ncallbacks++;
+// test that appendFile accepts file descriptors (callback API)
+{
+ const filename = join(tmpdir.path, 'append-descriptors.txt');
+ fs.writeFileSync(filename, currentFileData);
- fs.appendFile(fd, s, function(e) {
+ fs.open(filename, 'a+', common.mustCall((e, fd) => {
assert.ifError(e);
- ncallbacks++;
-
- fs.close(fd, function(e) {
+ fs.appendFile(fd, s, common.mustCall((e) => {
assert.ifError(e);
- ncallbacks++;
-
- fs.readFile(filename5, function(e, buffer) {
+ fs.close(fd, common.mustCall((e) => {
assert.ifError(e);
- ncallbacks++;
- assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
- buffer.length);
- });
- });
- });
-});
+ fs.readFile(filename, common.mustCall((e, buffer) => {
+ assert.ifError(e);
+ assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
+ buffer.length);
+ }));
+ }));
+ }));
+ }));
+}
+
+// test that appendFile accepts file descriptors (promises API)
+{
+ const filename = join(tmpdir.path, 'append-descriptors-promises.txt');
+ fs.writeFileSync(filename, currentFileData);
+
+ let fd;
+ fs.promises.open(filename, 'a+')
+ .then(common.mustCall((fileDescriptor) => {
+ fd = fileDescriptor;
+ return fs.promises.appendFile(fd, s);
+ }))
+ .then(common.mustCall(() => fd.close()))
+ .then(common.mustCall(() => fs.promises.readFile(filename)))
+ .then(common.mustCall((buffer) => {
+ assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
+ buffer.length);
+ }))
+ .catch(throwNextTick);
+}
assert.throws(
() => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log),
{ code: 'ERR_INVALID_CALLBACK' });
-
-process.on('exit', function() {
- assert.strictEqual(ncallbacks, 4);
-
- fs.unlinkSync(filename5);
-});