Skip to content

Commit 24286c4

Browse files
TrottMylesBorins
authored andcommitted
test: add promise API test for appendFile()
Apply the second of five test cases in test-fs-append-fil to the promise-based API in addition to the callback-based API. PR-URL: #20842 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent b9848bf commit 24286c4

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,35 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
7070
.catch(throwNextTick);
7171
}
7272

73-
// test that appends data to a non empty file
74-
const filename2 = join(tmpdir.path, 'append2.txt');
75-
fs.writeFileSync(filename2, currentFileData);
73+
// test that appends data to a non-empty file (callback API)
74+
{
75+
const filename = join(tmpdir.path, 'append-non-empty.txt');
76+
fs.writeFileSync(filename, currentFileData);
7677

77-
fs.appendFile(filename2, s, function(e) {
78-
assert.ifError(e);
78+
fs.appendFile(filename, s, common.mustCall(function(e) {
79+
assert.ifError(e);
7980

80-
ncallbacks++;
81+
fs.readFile(filename, common.mustCall(function(e, buffer) {
82+
assert.ifError(e);
83+
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
84+
buffer.length);
85+
}));
86+
}));
87+
}
8188

82-
fs.readFile(filename2, function(e, buffer) {
83-
assert.ifError(e);
84-
ncallbacks++;
85-
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
86-
buffer.length);
87-
});
88-
});
89+
// test that appends data to a non-empty file (promise API)
90+
{
91+
const filename = join(tmpdir.path, 'append-non-empty-promise.txt');
92+
fs.writeFileSync(filename, currentFileData);
93+
94+
fs.promises.appendFile(filename, s)
95+
.then(common.mustCall(() => fs.promises.readFile(filename)))
96+
.then((buffer) => {
97+
assert.strictEqual(Buffer.byteLength(s) + currentFileData.length,
98+
buffer.length);
99+
})
100+
.catch(throwNextTick);
101+
}
89102

90103
// test that appendFile accepts buffers
91104
const filename3 = join(tmpdir.path, 'append3.txt');
@@ -164,9 +177,8 @@ assert.throws(
164177
{ code: 'ERR_INVALID_CALLBACK' });
165178

166179
process.on('exit', function() {
167-
assert.strictEqual(10, ncallbacks);
180+
assert.strictEqual(ncallbacks, 8);
168181

169-
fs.unlinkSync(filename2);
170182
fs.unlinkSync(filename3);
171183
fs.unlinkSync(filename4);
172184
fs.unlinkSync(filename5);

0 commit comments

Comments
 (0)