Skip to content

Commit 14a017c

Browse files
Trotttargos
authored andcommitted
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: #21131 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent aa9dbf6 commit 14a017c

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

test/parallel/test-fs-append-file.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家
3838
'历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' +
3939
'它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n';
4040

41-
let ncallbacks = 0;
42-
4341
tmpdir.refresh();
4442

4543
const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
@@ -178,42 +176,50 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
178176
.catch(throwNextTick);
179177
}
180178

181-
// 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);
189183

190-
fs.appendFile(fd, s, function(e) {
184+
fs.open(filename, 'a+', common.mustCall((e, fd) => {
191185
assert.ifError(e);
192186

193-
ncallbacks++;
194-
195-
fs.close(fd, function(e) {
187+
fs.appendFile(fd, s, common.mustCall((e) => {
196188
assert.ifError(e);
197189

198-
ncallbacks++;
199-
200-
fs.readFile(filename5, function(e, buffer) {
190+
fs.close(fd, common.mustCall((e) => {
201191
assert.ifError(e);
202192

203-
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+
}
210222

211223
assert.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-
});

0 commit comments

Comments
 (0)