From 384aad6906d0abf6fb689ca09d082259f9b6a082 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 6 Feb 2018 10:50:32 -0800 Subject: [PATCH] Add test case for file change happening as part of file create and delete --- src/harness/unittests/tscWatchMode.ts | 32 +++++++++++++++++++++++ src/harness/virtualFileSystemWithWatch.ts | 18 ++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index b6d3099dc77..45cabc2be0e 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -1719,6 +1719,38 @@ namespace ts.tscWatch { return [files[0]]; } }); + + it("file is deleted and created as part of change", () => { + const projectLocation = "/home/username/project"; + const file: FileOrFolder = { + path: `${projectLocation}/app/file.ts`, + content: "var a = 10;" + }; + const fileJs = `${projectLocation}/app/file.js`; + const configFile: FileOrFolder = { + path: `${projectLocation}/tsconfig.json`, + content: JSON.stringify({ + "include": [ + "app/**/*.ts" + ] + }) + }; + const files = [file, configFile, libFile]; + const host = createWatchedSystem(files, { currentDirectory: projectLocation, useCaseSensitiveFileNames: true }); + createWatchOfConfigFile("tsconfig.json", host); + verifyProgram(); + + file.content += "var b = 10;"; + + host.reloadFS(files, { invokeFileDeleteCreateAsPartInsteadOfChange: true }); + host.runQueuedTimeoutCallbacks(); + verifyProgram(); + + function verifyProgram() { + assert.isTrue(host.fileExists(fileJs)); + assert.equal(host.readFile(fileJs), file.content + "\n"); + } + }); }); describe("tsc-watch module resolution caching", () => { diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index 381917d71c7..8545e193ae4 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -246,8 +246,12 @@ interface Array {}` } export interface ReloadWatchInvokeOptions { + /** Invokes the directory watcher for the parent instead of the file changed */ invokeDirectoryWatcherInsteadOfFileChanged: boolean; + /** When new file is created, do not invoke watches for it */ ignoreWatchInvokedWithTriggerAsFileCreate: boolean; + /** Invoke the file delete, followed by create instead of file changed */ + invokeFileDeleteCreateAsPartInsteadOfChange: boolean; } export class TestServerHost implements server.ServerHost, FormatDiagnosticsHost, ModuleResolutionHost { @@ -315,12 +319,18 @@ interface Array {}` if (isString(fileOrDirectory.content)) { // Update file if (currentEntry.content !== fileOrDirectory.content) { - currentEntry.content = fileOrDirectory.content; - if (options && options.invokeDirectoryWatcherInsteadOfFileChanged) { - this.invokeDirectoryWatcher(getDirectoryPath(currentEntry.fullPath), currentEntry.fullPath); + if (options && options.invokeFileDeleteCreateAsPartInsteadOfChange) { + this.removeFileOrFolder(currentEntry, returnFalse); + this.ensureFileOrFolder(fileOrDirectory); } else { - this.invokeFileWatcher(currentEntry.fullPath, FileWatcherEventKind.Changed); + currentEntry.content = fileOrDirectory.content; + if (options && options.invokeDirectoryWatcherInsteadOfFileChanged) { + this.invokeDirectoryWatcher(getDirectoryPath(currentEntry.fullPath), currentEntry.fullPath); + } + else { + this.invokeFileWatcher(currentEntry.fullPath, FileWatcherEventKind.Changed); + } } } }