Skip to content

Commit 185b9e4

Browse files
Trotttargos
authored andcommitted
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.) PR-URL: #21131 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent cbdc1fd commit 185b9e4

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

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

+28-15
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,37 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
100100
.catch(throwNextTick);
101101
}
102102

103-
// test that appendFile accepts buffers
104-
const filename3 = join(tmpdir.path, 'append3.txt');
105-
fs.writeFileSync(filename3, currentFileData);
103+
// test that appendFile accepts buffers (callback API)
104+
{
105+
const filename = join(tmpdir.path, 'append-buffer.txt');
106+
fs.writeFileSync(filename, currentFileData);
106107

107-
const buf = Buffer.from(s, 'utf8');
108+
const buf = Buffer.from(s, 'utf8');
108109

109-
fs.appendFile(filename3, buf, function(e) {
110-
assert.ifError(e);
110+
fs.appendFile(filename, buf, common.mustCall((e) => {
111+
assert.ifError(e);
111112

112-
ncallbacks++;
113+
fs.readFile(filename, common.mustCall((e, buffer) => {
114+
assert.ifError(e);
115+
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
116+
}));
117+
}));
118+
}
113119

114-
fs.readFile(filename3, function(e, buffer) {
115-
assert.ifError(e);
116-
ncallbacks++;
117-
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
118-
});
119-
});
120+
// test that appendFile accepts buffers (promises API)
121+
{
122+
const filename = join(tmpdir.path, 'append-buffer-promises.txt');
123+
fs.writeFileSync(filename, currentFileData);
124+
125+
const buf = Buffer.from(s, 'utf8');
126+
127+
fs.promises.appendFile(filename, buf)
128+
.then(common.mustCall(() => fs.promises.readFile(filename)))
129+
.then((buffer) => {
130+
assert.strictEqual(buf.length + currentFileData.length, buffer.length);
131+
})
132+
.catch(throwNextTick);
133+
}
120134

121135
// test that appendFile accepts numbers.
122136
const filename4 = join(tmpdir.path, 'append4.txt');
@@ -177,9 +191,8 @@ assert.throws(
177191
{ code: 'ERR_INVALID_CALLBACK' });
178192

179193
process.on('exit', function() {
180-
assert.strictEqual(ncallbacks, 8);
194+
assert.strictEqual(ncallbacks, 6);
181195

182-
fs.unlinkSync(filename3);
183196
fs.unlinkSync(filename4);
184197
fs.unlinkSync(filename5);
185198
});

0 commit comments

Comments
 (0)