From a281c60ed3f3799fe259d67ea3d2659f7f8611b6 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 4 Jun 2018 13:03:58 +0200 Subject: [PATCH 1/3] 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.) --- 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); }); From fef804af641ad8c7cff69b2572f92d8f42584f0f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 4 Jun 2018 13:52:58 +0200 Subject: [PATCH 2/3] test: apply promises API to fourth appendFile test Add tests for `fs.promises.appendFile()` to the fourth (of five) test case in `test-fs-access`. (The previous test cases already have promises API versions.) --- test/parallel/test-fs-append-file.js | 63 ++++++++++++++++++---------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js index 05dcee43e6cf5e..2d4c64863fb6ac 100644 --- a/test/parallel/test-fs-append-file.js +++ b/test/parallel/test-fs-append-file.js @@ -132,29 +132,51 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; .catch(throwNextTick); } -// test that appendFile accepts numbers. -const filename4 = join(tmpdir.path, 'append4.txt'); -fs.writeFileSync(filename4, currentFileData); +// test that appendFile accepts numbers (callback API) +{ + const filename = join(tmpdir.path, 'append-numbers.txt'); + fs.writeFileSync(filename, currentFileData); -const m = 0o600; -fs.appendFile(filename4, n, { mode: m }, function(e) { - assert.ifError(e); + const m = 0o600; + fs.appendFile(filename, n, { mode: m }, common.mustCall((e) => { + assert.ifError(e); - ncallbacks++; + // windows permissions aren't unix + if (!common.isWindows) { + const st = fs.statSync(filename); + assert.strictEqual(st.mode & 0o700, m); + } - // windows permissions aren't unix - if (!common.isWindows) { - const st = fs.statSync(filename4); - assert.strictEqual(st.mode & 0o700, m); - } + fs.readFile(filename, common.mustCall((e, buffer) => { + assert.ifError(e); + assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length, + buffer.length); + })); + })); +} - fs.readFile(filename4, function(e, buffer) { - assert.ifError(e); - ncallbacks++; - assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length, - buffer.length); - }); -}); +// test that appendFile accepts numbers (promises API) +{ + const filename = join(tmpdir.path, 'append-numbers-promises.txt'); + fs.writeFileSync(filename, currentFileData); + + const m = 0o600; + fs.promises.appendFile(filename, n, { mode: m }) + .then(common.mustCall(() => { + // windows permissions aren't unix + if (!common.isWindows) { + const st = fs.statSync(filename); + assert.strictEqual(st.mode & 0o700, m); + } + + return fs.promises.readFile(filename); + })) + .then((buffer) => { + assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length, + buffer.length); + }) + .catch(throwNextTick); +} // test that appendFile accepts file descriptors const filename5 = join(tmpdir.path, 'append5.txt'); @@ -191,8 +213,7 @@ assert.throws( { code: 'ERR_INVALID_CALLBACK' }); process.on('exit', function() { - assert.strictEqual(ncallbacks, 6); + assert.strictEqual(ncallbacks, 4); - fs.unlinkSync(filename4); fs.unlinkSync(filename5); }); From 82da1121d59a73d4fe138c8ee9d943ebdb4afc2f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 4 Jun 2018 14:12:08 +0200 Subject: [PATCH 3/3] 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.) --- test/parallel/test-fs-append-file.js | 66 +++++++++++++++------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js index 2d4c64863fb6ac..4d8f66682ed634 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); -});