test: apply promises API to fourth appendFile test · nodejs/node@14a017c
@@ -38,8 +38,6 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家
3838'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
3939'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';
404041-let ncallbacks = 0;
42-4341tmpdir.refresh();
44424543const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
@@ -178,42 +176,50 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
178176.catch(throwNextTick);
179177}
180178181-// test that appendFile accepts file descriptors
182-const filename5 = join(tmpdir.path, 'append5.txt');
183-fs.writeFileSync(filename5, currentFileData);
184-185-fs.open(filename5, 'a+', function(e, fd) {
186-assert.ifError(e);
187-188-ncallbacks++;
179+// test that appendFile accepts file descriptors (callback API)
180+{
181+const filename = join(tmpdir.path, 'append-descriptors.txt');
182+fs.writeFileSync(filename, currentFileData);
189183190-fs.appendFile(fd, s, function(e) {
184+fs.open(filename, 'a+', common.mustCall((e, fd) => {
191185assert.ifError(e);
192186193-ncallbacks++;
194-195-fs.close(fd, function(e) {
187+fs.appendFile(fd, s, common.mustCall((e) => {
196188assert.ifError(e);
197189198-ncallbacks++;
199-200-fs.readFile(filename5, function(e, buffer) {
190+fs.close(fd, common.mustCall((e) => {
201191assert.ifError(e);
202192203-ncallbacks++;
204-assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
205-buffer.length);
206-});
207-});
208-});
209-});
193+fs.readFile(filename, common.mustCall((e, buffer) => {
194+assert.ifError(e);
195+assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
196+buffer.length);
197+}));
198+}));
199+}));
200+}));
201+}
202+203+// test that appendFile accepts file descriptors (promises API)
204+{
205+const filename = join(tmpdir.path, 'append-descriptors-promises.txt');
206+fs.writeFileSync(filename, currentFileData);
207+208+let fd;
209+fs.promises.open(filename, 'a+')
210+.then(common.mustCall((fileDescriptor) => {
211+fd = fileDescriptor;
212+return fs.promises.appendFile(fd, s);
213+}))
214+.then(common.mustCall(() => fd.close()))
215+.then(common.mustCall(() => fs.promises.readFile(filename)))
216+.then(common.mustCall((buffer) => {
217+assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
218+buffer.length);
219+}))
220+.catch(throwNextTick);
221+}
210222211223assert.throws(
212224() => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log),
213225{ code: 'ERR_INVALID_CALLBACK' });
214-215-process.on('exit', function() {
216-assert.strictEqual(ncallbacks, 4);
217-218-fs.unlinkSync(filename5);
219-});