@@ -27,8 +27,6 @@ const join = require('path').join;
27
27
28
28
const tmpdir = require ( '../common/tmpdir' ) ;
29
29
30
- const filename = join ( tmpdir . path , 'append.txt' ) ;
31
-
32
30
const currentFileData = 'ABCD' ;
33
31
34
32
const n = 220 ;
@@ -44,18 +42,33 @@ let ncallbacks = 0;
44
42
45
43
tmpdir . refresh ( ) ;
46
44
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 ; } ) ; } ;
50
46
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' ) ;
52
50
53
- fs . readFile ( filename , function ( e , buffer ) {
51
+ fs . appendFile ( filename , s , common . mustCall ( function ( e ) {
54
52
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
+ }
59
72
60
73
// test that appends data to a non empty file
61
74
const filename2 = join ( tmpdir . path , 'append2.txt' ) ;
@@ -151,9 +164,8 @@ assert.throws(
151
164
{ code : 'ERR_INVALID_CALLBACK' } ) ;
152
165
153
166
process . on ( 'exit' , function ( ) {
154
- assert . strictEqual ( 12 , ncallbacks ) ;
167
+ assert . strictEqual ( 10 , ncallbacks ) ;
155
168
156
- fs . unlinkSync ( filename ) ;
157
169
fs . unlinkSync ( filename2 ) ;
158
170
fs . unlinkSync ( filename3 ) ;
159
171
fs . unlinkSync ( filename4 ) ;
0 commit comments