diff --git a/src/__tests__/promises.test.ts b/src/__tests__/promises.test.ts index 39225e30a..e49002799 100644 --- a/src/__tests__/promises.test.ts +++ b/src/__tests__/promises.test.ts @@ -193,6 +193,8 @@ describe('Promises API', () => { const fileHandle = await promises.open('/foo', 'r+'); await fileHandle.truncate(5); expect(vol.readFileSync('/foo').toString()).toEqual('01234'); + await fileHandle.truncate(7); + expect(vol.readFileSync('/foo').toString()).toEqual('01234\0\0'); await fileHandle.close(); }); it('Reject when the file handle was closed', async () => { @@ -592,6 +594,8 @@ describe('Promises API', () => { it('Truncate an existing file', async () => { await promises.truncate('/foo', 5); expect(vol.readFileSync('/foo').toString()).toEqual('01234'); + await promises.truncate('/foo', 7); + expect(vol.readFileSync('/foo').toString()).toEqual('01234\0\0'); }); it('Reject when file does not exist', () => { return expect(promises.truncate('/bar')).rejects.toBeInstanceOf(Error); diff --git a/src/__tests__/volume.test.ts b/src/__tests__/volume.test.ts index 53a1f8933..9950c2473 100644 --- a/src/__tests__/volume.test.ts +++ b/src/__tests__/volume.test.ts @@ -955,6 +955,13 @@ describe('volume', () => { vol.truncateSync('/1', 2); expect(vol.readFileSync('/1', 'utf8')).toBe('12'); }); + it('Larger truncate', () => { + const fd = vol.openSync('/2', 'w'); + vol.writeFileSync(fd, '12345'); + expect(vol.readFileSync('/2', 'utf8')).toBe('12345'); + vol.truncateSync('/2', 10); + expect(vol.readFileSync('/2', 'utf8')).toBe('12345\0\0\0\0\0'); + }); }); describe('.truncate(path[, len], callback)', () => { xit('...', () => {}); diff --git a/src/node.ts b/src/node.ts index df8f1c107..30faafaa3 100644 --- a/src/node.ts +++ b/src/node.ts @@ -143,9 +143,10 @@ export class Node extends EventEmitter { if (len <= this.buf.length) { this.buf = this.buf.slice(0, len); } else { - const buf = bufferAllocUnsafe(0); + const buf = bufferAllocUnsafe(len); this.buf.copy(buf); - buf.fill(0, len); + buf.fill(0, this.buf.length); + this.buf = buf; } }