File tree 2 files changed +22
-3
lines changed
2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -223,9 +223,13 @@ export class FileHandle<P extends Path> {
223
223
}
224
224
}
225
225
226
- // FIXME: Missing FakeFS version
227
- truncate ( len ?: number ) : Promise < void > {
228
- throw new Error ( `Method not implemented.` ) ;
226
+ async truncate ( len ?: number ) : Promise < void > {
227
+ try {
228
+ this [ kRef ] ( this . truncate ) ;
229
+ return await this [ kBaseFs ] . ftruncatePromise ( this . fd , len ) ;
230
+ } finally {
231
+ this [ kUnref ] ( ) ;
232
+ }
229
233
}
230
234
231
235
// FIXME: Missing FakeFS version
Original file line number Diff line number Diff line change @@ -468,4 +468,19 @@ describe(`patchedFs`, () => {
468
468
await fd . close ( ) ;
469
469
} ) ;
470
470
} ) ;
471
+
472
+ it ( `should support FileHandle.truncate` , async ( ) => {
473
+ const patchedFs = extendFs ( fs , new PosixFS ( new NodeFS ( ) ) ) ;
474
+
475
+ await xfs . mktempPromise ( async dir => {
476
+ const filepath = npath . join ( npath . fromPortablePath ( dir ) , `foo.txt` ) ;
477
+ await patchedFs . promises . writeFile ( filepath , `foo` ) ;
478
+
479
+ const fd = await patchedFs . promises . open ( filepath , `r+` ) ;
480
+ await fd . truncate ( 1 ) ;
481
+ await fd . close ( ) ;
482
+
483
+ await expect ( patchedFs . promises . readFile ( filepath , `utf8` ) ) . resolves . toEqual ( `f` ) ;
484
+ } ) ;
485
+ } ) ;
471
486
} ) ;
You can’t perform that action at this time.
0 commit comments