diff --git a/test/parallel/test-fs-append-file.js b/test/parallel/test-fs-append-file.js index 9ab36a67767946..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; }); }; @@ -100,86 +98,128 @@ 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); - -const buf = Buffer.from(s, 'utf8'); - -fs.appendFile(filename3, buf, function(e) { - assert.ifError(e); +// test that appendFile accepts buffers (callback API) +{ + const filename = join(tmpdir.path, 'append-buffer.txt'); + fs.writeFileSync(filename, currentFileData); - ncallbacks++; + const buf = Buffer.from(s, 'utf8'); - fs.readFile(filename3, function(e, buffer) { + fs.appendFile(filename, buf, common.mustCall((e) => { assert.ifError(e); - ncallbacks++; - assert.strictEqual(buf.length + currentFileData.length, buffer.length); - }); -}); -// test that appendFile accepts numbers. -const filename4 = join(tmpdir.path, 'append4.txt'); -fs.writeFileSync(filename4, currentFileData); + fs.readFile(filename, common.mustCall((e, buffer) => { + assert.ifError(e); + 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 m = 0o600; -fs.appendFile(filename4, n, { mode: m }, function(e) { - assert.ifError(e); + const buf = Buffer.from(s, 'utf8'); - ncallbacks++; + fs.promises.appendFile(filename, buf) + .then(common.mustCall(() => fs.promises.readFile(filename))) + .then((buffer) => { + assert.strictEqual(buf.length + currentFileData.length, buffer.length); + }) + .catch(throwNextTick); +} - // windows permissions aren't unix - if (!common.isWindows) { - const st = fs.statSync(filename4); - assert.strictEqual(st.mode & 0o700, m); - } +// test that appendFile accepts numbers (callback API) +{ + const filename = join(tmpdir.path, 'append-numbers.txt'); + fs.writeFileSync(filename, currentFileData); - fs.readFile(filename4, function(e, buffer) { + const m = 0o600; + fs.appendFile(filename, n, { mode: m }, common.mustCall((e) => { assert.ifError(e); - ncallbacks++; - assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length, - buffer.length); - }); -}); -// test that appendFile accepts file descriptors -const filename5 = join(tmpdir.path, 'append5.txt'); -fs.writeFileSync(filename5, currentFileData); + // windows permissions aren't unix + if (!common.isWindows) { + const st = fs.statSync(filename); + assert.strictEqual(st.mode & 0o700, m); + } -fs.open(filename5, 'a+', function(e, fd) { - assert.ifError(e); + fs.readFile(filename, common.mustCall((e, buffer) => { + assert.ifError(e); + assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length, + buffer.length); + })); + })); +} - ncallbacks++; +// test that appendFile accepts numbers (promises API) +{ + const filename = join(tmpdir.path, 'append-numbers-promises.txt'); + fs.writeFileSync(filename, currentFileData); - fs.appendFile(fd, s, function(e) { - assert.ifError(e); + 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); +} - ncallbacks++; +// test that appendFile accepts file descriptors (callback API) +{ + const filename = join(tmpdir.path, 'append-descriptors.txt'); + fs.writeFileSync(filename, currentFileData); - fs.close(fd, function(e) { - assert.ifError(e); + fs.open(filename, 'a+', common.mustCall((e, fd) => { + assert.ifError(e); - ncallbacks++; + fs.appendFile(fd, s, common.mustCall((e) => { + assert.ifError(e); - 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, 8); - - fs.unlinkSync(filename3); - fs.unlinkSync(filename4); - fs.unlinkSync(filename5); -});