@@ -100,23 +100,37 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
100
100
. catch ( throwNextTick ) ;
101
101
}
102
102
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 ) ;
106
107
107
- const buf = Buffer . from ( s , 'utf8' ) ;
108
+ const buf = Buffer . from ( s , 'utf8' ) ;
108
109
109
- fs . appendFile ( filename3 , buf , function ( e ) {
110
- assert . ifError ( e ) ;
110
+ fs . appendFile ( filename , buf , common . mustCall ( ( e ) => {
111
+ assert . ifError ( e ) ;
111
112
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
+ }
113
119
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
+ }
120
134
121
135
// test that appendFile accepts numbers.
122
136
const filename4 = join ( tmpdir . path , 'append4.txt' ) ;
@@ -177,9 +191,8 @@ assert.throws(
177
191
{ code : 'ERR_INVALID_CALLBACK' } ) ;
178
192
179
193
process . on ( 'exit' , function ( ) {
180
- assert . strictEqual ( ncallbacks , 8 ) ;
194
+ assert . strictEqual ( ncallbacks , 6 ) ;
181
195
182
- fs . unlinkSync ( filename3 ) ;
183
196
fs . unlinkSync ( filename4 ) ;
184
197
fs . unlinkSync ( filename5 ) ;
185
198
} ) ;
0 commit comments