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 <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 740bf78 commit 9e432ca

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

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

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

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

30-
constfilename=join(tmpdir.path,'append.txt');
31-
3230
constcurrentFileData='ABCD';
3331

3432
constn=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+
constthrowNextTick=(e)=>{process.nextTick(()=>{throwe;});};
5046

51-
ncallbacks++;
47+
// test that empty file will be created and have content added (callback API)
48+
{
49+
constfilename=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+
constfilename=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
constfilename2=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)