Skip to content

Commit

Permalink
fix: FileHandle.truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed May 24, 2022
1 parent d1ab512 commit 54a9b32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/yarnpkg-fslib/sources/patchFs/FileHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,13 @@ export class FileHandle<P extends Path> {
}
}

// FIXME: Missing FakeFS version
truncate(len?: number): Promise<void> {
throw new Error(`Method not implemented.`);
async truncate(len?: number): Promise<void> {
try {
this[kRef](this.truncate);
return await this[kBaseFs].ftruncatePromise(this.fd, len);
} finally {
this[kUnref]();
}
}

// FIXME: Missing FakeFS version
Expand Down
15 changes: 15 additions & 0 deletions packages/yarnpkg-fslib/tests/patchedFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,19 @@ describe(`patchedFs`, () => {
await fd.close();
});
});

it(`should support FileHandle.truncate`, async () => {
const patchedFs = extendFs(fs, new PosixFS(new NodeFS()));

await xfs.mktempPromise(async dir => {
const filepath = npath.join(npath.fromPortablePath(dir), `foo.txt`);
await patchedFs.promises.writeFile(filepath, `foo`);

const fd = await patchedFs.promises.open(filepath, `r+`);
await fd.truncate(1);
await fd.close();

await expect(patchedFs.promises.readFile(filepath, `utf8`)).resolves.toEqual(`f`);
});
});
});

0 comments on commit 54a9b32

Please sign in to comment.