Skip to content

Commit 9e432ca

Browse files
TrottMylesBorins
authored andcommitted
test: add promise API test for appendFile()
Apply the first of five test cases in test-fs-append-file to the promise-based API in addition to the callback-based API. PR-URL: #20739 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 740bf78 commit 9e432ca

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

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

+25-13
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const join = require('path').join;
2727

2828
const tmpdir = require('../common/tmpdir');
2929

30-
const filename = join(tmpdir.path, 'append.txt');
31-
3230
const currentFileData = 'ABCD';
3331

3432
const n = 220;
@@ -44,18 +42,33 @@ let ncallbacks = 0;
4442

4543
tmpdir.refresh();
4644

47-
// test that empty file will be created and have content added
48-
fs.appendFile(filename, s, function(e) {
49-
assert.ifError(e);
45+
const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
5046

51-
ncallbacks++;
47+
// test that empty file will be created and have content added (callback API)
48+
{
49+
const filename = join(tmpdir.path, 'append.txt');
5250

53-
fs.readFile(filename, function(e, buffer) {
51+
fs.appendFile(filename, s, common.mustCall(function(e) {
5452
assert.ifError(e);
55-
ncallbacks++;
56-
assert.strictEqual(Buffer.byteLength(s), buffer.length);
57-
});
58-
});
53+
54+
fs.readFile(filename, common.mustCall(function(e, buffer) {
55+
assert.ifError(e);
56+
assert.strictEqual(Buffer.byteLength(s), buffer.length);
57+
}));
58+
}));
59+
}
60+
61+
// test that empty file will be created and have content added (promise API)
62+
{
63+
const filename = join(tmpdir.path, 'append-promise.txt');
64+
65+
fs.promises.appendFile(filename, s)
66+
.then(common.mustCall(() => fs.promises.readFile(filename)))
67+
.then((buffer) => {
68+
assert.strictEqual(Buffer.byteLength(s), buffer.length);
69+
})
70+
.catch(throwNextTick);
71+
}
5972

6073
// test that appends data to a non empty file
6174
const filename2 = join(tmpdir.path, 'append2.txt');
@@ -151,9 +164,8 @@ assert.throws(
151164
{ code: 'ERR_INVALID_CALLBACK' });
152165

153166
process.on('exit', function() {
154-
assert.strictEqual(12, ncallbacks);
167+
assert.strictEqual(10, ncallbacks);
155168

156-
fs.unlinkSync(filename);
157169
fs.unlinkSync(filename2);
158170
fs.unlinkSync(filename3);
159171
fs.unlinkSync(filename4);

0 commit comments

Comments
 (0)