From fdd44be5149f7c236ad38ec8a22b60fe0fabbfa9 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Wed, 25 May 2022 10:39:24 -0700 Subject: [PATCH] Try to improve diff further --- src/harness/virtualFileSystemWithWatch.ts | 175 +++++++++++----------- 1 file changed, 87 insertions(+), 88 deletions(-) diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index dd83741e8f4..2a5d7da0a56 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -42,6 +42,13 @@ interface Array { length: number; [n: number]: T; }` withSafeList?: boolean; } + export function createVirtualServerHost(params: VirtualServerHostCreationParameters): VirtualServerHost { + const host = new VirtualServerHost(params); + // Just like sys, patch the host to use writeFile + patchWriteFileEnsuringDirectory(host); + return host; + } + export function createWatchedSystem(fileOrFolderList: readonly FileOrFolderOrSymLink[], params?: TestServerHostCreationParameters): TestServerHost { return new TestServerHost(/*withSafelist*/ false, fileOrFolderList, params); } @@ -53,13 +60,6 @@ interface Array { length: number; [n: number]: T; }` return host; } - export function createVirtualServerHost(params: VirtualServerHostCreationParameters): VirtualServerHost { - const host = new VirtualServerHost(params); - // Just like sys, patch the host to use writeFile - patchWriteFileEnsuringDirectory(host); - return host; - } - interface CallbackData { cb: TimeOutCallback; args: any[]; @@ -393,32 +393,6 @@ interface Array { length: number; [n: number]: T; }` } } - protected override fsWatch( - fileOrDirectory: string, - _entryKind: FileSystemEntryKind, - cb: FsWatchCallback, - recursive: boolean, - fallbackPollingInterval: PollingInterval, - fallbackOptions: WatchOptions | undefined): FileWatcher { - return this.runWithFallbackPolling ? - this.watchFile( - fileOrDirectory, - createFileWatcherCallback(cb), - fallbackPollingInterval, - fallbackOptions - ) : - createWatcher( - recursive ? this.fsWatchesRecursive : this.fsWatches, - this.toFullPath(fileOrDirectory), - { - directoryName: fileOrDirectory, - cb, - fallbackPollingInterval, - fallbackOptions - } - ); - } - // Output is pretty writeOutputIsTTY() { return true; @@ -429,60 +403,6 @@ interface Array { length: number; [n: number]: T; }` return new Date(this.time); } - // TODO: record and invoke callbacks to simulate timer events - setTimeout(callback: TimeOutCallback, ms: number, ...args: any[]) { - return this.timeoutCallbacks.register(callback, args, ms); - } - - getNextTimeoutId() { - return this.timeoutCallbacks.getNextId(); - } - - clearTimeout(timeoutId: any): void { - this.timeoutCallbacks.unregister(timeoutId); - } - - runQueuedTimeoutCallbacks(timeoutId?: number) { - try { - this.timeoutCallbacks.invoke(timeoutId); - } - catch (e) { - if (e.message === this.exitMessage) { - return; - } - throw e; - } - } - - runQueuedImmediateCallbacks(checkCount?: number) { - if (checkCount !== undefined) { - assert.equal(this.immediateCallbacks.count(), checkCount); - } - this.immediateCallbacks.invoke(); - } - - setImmediate(callback: TimeOutCallback, ...args: any[]) { - return this.immediateCallbacks.register(callback, args); - } - - clearImmediate(timeoutId: any): void { - this.immediateCallbacks.unregister(timeoutId); - } - - clearScreen(): void { - this.screenClears.push(this.output.length); - } - - checkTimeoutQueueLengthAndRun(expected: number) { - this.checkTimeoutQueueLength(expected); - this.runQueuedTimeoutCallbacks(); - } - - checkTimeoutQueueLength(expected: number) { - const callbacksCount = this.timeoutCallbacks.count(); - assert.equal(callbacksCount, expected, `expected ${expected} timeout callbacks queued but found ${callbacksCount}.`); - } - renameFile(fileName: string, newFileName: string) { const fullPath = getNormalizedAbsolutePath(fileName, this.currentDirectory); const path = this.toPath(fullPath); @@ -562,6 +482,32 @@ interface Array { length: number; [n: number]: T; }` this.removeFileOrFolder(currentEntry, /*isRemoveableLeafFolder*/ false); } + protected override fsWatch( + fileOrDirectory: string, + _entryKind: FileSystemEntryKind, + cb: FsWatchCallback, + recursive: boolean, + fallbackPollingInterval: PollingInterval, + fallbackOptions: WatchOptions | undefined): FileWatcher { + return this.runWithFallbackPolling ? + this.watchFile( + fileOrDirectory, + createFileWatcherCallback(cb), + fallbackPollingInterval, + fallbackOptions + ) : + createWatcher( + recursive ? this.fsWatchesRecursive : this.fsWatches, + this.toFullPath(fileOrDirectory), + { + directoryName: fileOrDirectory, + cb, + fallbackPollingInterval, + fallbackOptions + } + ); + } + readFile(s: string): string | undefined { const fsEntry = this.getRealFile(this.toFullPath(s)); return fsEntry ? fsEntry.content : undefined; @@ -584,6 +530,60 @@ interface Array { length: number; [n: number]: T; }` return sys.createSHA256Hash!(s); } + // TODO: record and invoke callbacks to simulate timer events + setTimeout(callback: TimeOutCallback, ms: number, ...args: any[]) { + return this.timeoutCallbacks.register(callback, args, ms); + } + + getNextTimeoutId() { + return this.timeoutCallbacks.getNextId(); + } + + clearTimeout(timeoutId: any): void { + this.timeoutCallbacks.unregister(timeoutId); + } + + clearScreen(): void { + this.screenClears.push(this.output.length); + } + + checkTimeoutQueueLengthAndRun(expected: number) { + this.checkTimeoutQueueLength(expected); + this.runQueuedTimeoutCallbacks(); + } + + checkTimeoutQueueLength(expected: number) { + const callbacksCount = this.timeoutCallbacks.count(); + assert.equal(callbacksCount, expected, `expected ${expected} timeout callbacks queued but found ${callbacksCount}.`); + } + + runQueuedImmediateCallbacks(checkCount?: number) { + if (checkCount !== undefined) { + assert.equal(this.immediateCallbacks.count(), checkCount); + } + this.immediateCallbacks.invoke(); + } + + runQueuedTimeoutCallbacks(timeoutId?: number) { + try { + this.timeoutCallbacks.invoke(timeoutId); + } + catch (e) { + if (e.message === this.exitMessage) { + return; + } + throw e; + } + } + + setImmediate(callback: TimeOutCallback, ...args: any[]) { + return this.immediateCallbacks.register(callback, args); + } + + clearImmediate(timeoutId: any): void { + this.immediateCallbacks.unregister(timeoutId); + } + prependFile(path: string, content: string, options?: Partial): void { this.modifyFile(path, content + this.readFile(path), options); } @@ -683,7 +683,6 @@ interface Array { length: number; [n: number]: T; }` }; return host; } - export const tsbuildProjectsLocation = "/user/username/projects"; export function getTsBuildProjectFilePath(project: string, file: string) { return `${tsbuildProjectsLocation}/${project}/${file}`;