Skip to content

Commit 54a9b32

Browse files
committed
fix: FileHandle.truncate
1 parent d1ab512 commit 54a9b32

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,13 @@ export class FileHandle<P extends Path> {
223223
}
224224
}
225225

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+
}
229233
}
230234

231235
// FIXME: Missing FakeFS version

packages/yarnpkg-fslib/tests/patchedFs.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,19 @@ describe(`patchedFs`, () => {
468468
await fd.close();
469469
});
470470
});
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+
});
471486
});

0 commit comments

Comments
 (0)