From 4a9223c0d37e0f91c081a0d7e0c71b7edba5ca57 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 11 Jul 2019 08:33:41 +0200 Subject: [PATCH] files - properly add stream support to create/write --- src/vs/platform/files/common/files.ts | 4 ++-- .../services/files/common/fileService.ts | 4 ++-- .../files/test/node/diskFileService.test.ts | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/vs/platform/files/common/files.ts b/src/vs/platform/files/common/files.ts index 69b1fb785d4..adc1bf41767 100644 --- a/src/vs/platform/files/common/files.ts +++ b/src/vs/platform/files/common/files.ts @@ -105,7 +105,7 @@ export interface IFileService { /** * Updates the content replacing its previous value. */ - writeFile(resource: URI, bufferOrReadable: VSBuffer | VSBufferReadable, options?: IWriteFileOptions): Promise; + writeFile(resource: URI, bufferOrReadableOrStream: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: IWriteFileOptions): Promise; /** * Moves the file/folder to a new path identified by the resource. @@ -127,7 +127,7 @@ export interface IFileService { * * The optional parameter content can be used as value to fill into the new file. */ - createFile(resource: URI, bufferOrReadable?: VSBuffer | VSBufferReadable, options?: ICreateFileOptions): Promise; + createFile(resource: URI, bufferOrReadableOrStream?: VSBuffer | VSBufferReadable | VSBufferReadableStream, options?: ICreateFileOptions): Promise; /** * Creates a new folder with the given path. The returned promise diff --git a/src/vs/workbench/services/files/common/fileService.ts b/src/vs/workbench/services/files/common/fileService.ts index a2ad60609c6..0b436e9e75b 100644 --- a/src/vs/workbench/services/files/common/fileService.ts +++ b/src/vs/workbench/services/files/common/fileService.ts @@ -264,7 +264,7 @@ export class FileService extends Disposable implements IFileService { //#region File Reading/Writing - async createFile(resource: URI, bufferOrReadable: VSBuffer | VSBufferReadable = VSBuffer.fromString(''), options?: ICreateFileOptions): Promise { + async createFile(resource: URI, bufferOrReadableOrStream: VSBuffer | VSBufferReadable | VSBufferReadableStream = VSBuffer.fromString(''), options?: ICreateFileOptions): Promise { // validate overwrite const overwrite = !!(options && options.overwrite); @@ -273,7 +273,7 @@ export class FileService extends Disposable implements IFileService { } // do write into file (this will create it too) - const fileStat = await this.writeFile(resource, bufferOrReadable); + const fileStat = await this.writeFile(resource, bufferOrReadableOrStream); // events this._onAfterOperation.fire(new FileOperationEvent(resource, FileOperation.CREATE, fileStat)); diff --git a/src/vs/workbench/services/files/test/node/diskFileService.test.ts b/src/vs/workbench/services/files/test/node/diskFileService.test.ts index fb39acd397d..f5394917d84 100644 --- a/src/vs/workbench/services/files/test/node/diskFileService.test.ts +++ b/src/vs/workbench/services/files/test/node/diskFileService.test.ts @@ -20,7 +20,7 @@ import { NullLogService } from 'vs/platform/log/common/log'; import { isLinux, isWindows } from 'vs/base/common/platform'; import { DisposableStore } from 'vs/base/common/lifecycle'; import { isEqual } from 'vs/base/common/resources'; -import { VSBuffer, VSBufferReadable, writeableBufferStream, VSBufferReadableStream } from 'vs/base/common/buffer'; +import { VSBuffer, VSBufferReadable, writeableBufferStream, VSBufferReadableStream, bufferToReadable, bufferToStream } from 'vs/base/common/buffer'; function getByName(root: IFileStat, name: string): IFileStat | null { if (root.children === undefined) { @@ -1336,12 +1336,24 @@ suite('Disk File Service', () => { }); test('createFile', async () => { + assertCreateFile(contents => VSBuffer.fromString(contents)); + }); + + test('createFile (readable)', async () => { + assertCreateFile(contents => bufferToReadable(VSBuffer.fromString(contents))); + }); + + test('createFile (stream)', async () => { + assertCreateFile(contents => bufferToStream(VSBuffer.fromString(contents))); + }); + + async function assertCreateFile(converter: (content: string) => VSBuffer | VSBufferReadable | VSBufferReadableStream): Promise { let event: FileOperationEvent; disposables.add(service.onAfterOperation(e => event = e)); const contents = 'Hello World'; const resource = URI.file(join(testDir, 'test.txt')); - const fileStat = await service.createFile(resource, VSBuffer.fromString(contents)); + const fileStat = await service.createFile(resource, converter(contents)); assert.equal(fileStat.name, 'test.txt'); assert.equal(existsSync(fileStat.resource.fsPath), true); assert.equal(readFileSync(fileStat.resource.fsPath), contents); @@ -1350,7 +1362,7 @@ suite('Disk File Service', () => { assert.equal(event!.resource.fsPath, resource.fsPath); assert.equal(event!.operation, FileOperation.CREATE); assert.equal(event!.target!.resource.fsPath, resource.fsPath); - }); + } test('createFile (does not overwrite by default)', async () => { const contents = 'Hello World'; -- GitLab