File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+ const common = require ( '../common' ) ;
3+ const assert = require ( 'assert' ) ;
4+ const path = require ( 'path' ) ;
5+ const fs = require ( 'fs' ) ;
6+ const tmp = common . tmpDir ;
7+
8+ common . refreshTmpDir ( ) ;
9+
10+ const filename = path . resolve ( tmp , 'truncate-sync-file.txt' ) ;
11+
12+ fs . writeFileSync ( filename , 'hello world' , 'utf8' ) ;
13+
14+ const fd = fs . openSync ( filename , 'r+' ) ;
15+
16+ fs . truncateSync ( fd , 5 ) ;
17+ assert ( fs . readFileSync ( fd ) . equals ( Buffer . from ( 'hello' ) ) ) ;
18+
19+ fs . closeSync ( fd ) ;
20+ fs . unlinkSync ( filename ) ;
Original file line number Diff line number Diff line change @@ -146,3 +146,14 @@ function testFtruncate(cb) {
146146 assert ( fs . readFileSync ( file4 ) . equals ( Buffer . from ( 'Hi\u0000\u0000' ) ) ) ;
147147 } ) ) ;
148148}
149+
150+ {
151+ const file5 = path . resolve ( tmp , 'truncate-file-5.txt' ) ;
152+ fs . writeFileSync ( file5 , 'Hi' ) ;
153+ const fd = fs . openSync ( file5 , 'r+' ) ;
154+ process . on ( 'exit' , ( ) => fs . closeSync ( fd ) ) ;
155+ fs . ftruncate ( fd , undefined , common . mustCall ( function ( err ) {
156+ assert . ifError ( err ) ;
157+ assert ( fs . readFileSync ( file5 ) . equals ( Buffer . from ( '' ) ) ) ;
158+ } ) ) ;
159+ }
You can’t perform that action at this time.
0 commit comments