Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 54a9b32

Browse files
committedMay 24, 2022
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

+7-3
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

+15
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)
Please sign in to comment.