From ab17600e8610e81bc093bc8c544d1b9c17bf9501 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 30 Nov 2017 10:35:28 -0800 Subject: [PATCH] Improve test to verify the count of callbacks for the watched directories through watchFile --- src/harness/unittests/tscWatchMode.ts | 2 +- .../unittests/tsserverProjectSystem.ts | 20 +++++++++---------- src/harness/virtualFileSystemWithWatch.ts | 12 +++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/harness/unittests/tscWatchMode.ts b/src/harness/unittests/tscWatchMode.ts index afc000f8ed9..7b64e043eb6 100644 --- a/src/harness/unittests/tscWatchMode.ts +++ b/src/harness/unittests/tscWatchMode.ts @@ -2210,7 +2210,7 @@ declare module "fs" { checkWatchedDirectories(host, emptyArray, /*recursive*/ true); // Watching config file, file, lib file and directories - checkWatchedFiles(host, expectedWatchedFiles); + ts.TestFSWithWatch.checkMultiMapEachKeyWithCount("watchedFiles", host.watchedFiles, expectedWatchedFiles, 1); checkProgramActualFiles(watch(), programFiles.map(f => f.path)); checkOutputErrors(host, emptyArray); diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index dc1dca302fb..fdaebc4b054 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -5322,16 +5322,11 @@ namespace ts.projectSystem { } function verifyCalledOnEachEntry(callback: CalledMaps, expectedKeys: Map) { - const calledMap = calledMaps[callback]; - ts.TestFSWithWatch.verifyMapSize(callback, calledMap, arrayFrom(expectedKeys.keys())); - expectedKeys.forEach((called, name) => { - assert.isTrue(calledMap.has(name), `${callback} is expected to contain ${name}, actual keys: ${arrayFrom(calledMap.keys())}`); - assert.equal(calledMap.get(name).length, called, `${callback} is expected to be called ${called} times with ${name}. Actual entry: ${calledMap.get(name)}`); - }); + ts.TestFSWithWatch.checkMultiMapKeyCount(callback, calledMaps[callback], expectedKeys); } function verifyCalledOnEachEntryNTimes(callback: CalledMaps, expectedKeys: string[], nTimes: number) { - return verifyCalledOnEachEntry(callback, zipToMap(expectedKeys, expectedKeys.map(() => nTimes))); + ts.TestFSWithWatch.checkMultiMapEachKeyWithCount(callback, calledMaps[callback], expectedKeys, nTimes); } function verifyNoHostCalls() { @@ -6605,7 +6600,11 @@ namespace ts.projectSystem { const files = [index, file1, configFile, libFile]; const fileNames = files.map(file => file.path); // All closed files(files other than index), project folder, project/src folder and project/node_modules/@types folder - const expectedWatchedFiles = fileNames.slice(1).concat(projectFolder, projectSrcFolder, `${projectFolder}/${nodeModulesAtTypes}`); + const expectedWatchedFiles = arrayToMap(fileNames.slice(1).concat(`${projectFolder}/${nodeModulesAtTypes}`), s => s, () => 1); + // For failed resolution lookup and tsconfig files + expectedWatchedFiles.set(projectFolder, 2); + // Through above recursive watches + expectedWatchedFiles.set(projectSrcFolder, 2); const expectedCompletions = ["file1"]; const completionPosition = index.content.lastIndexOf('"'); const environmentVariables = createMap(); @@ -6625,7 +6624,7 @@ namespace ts.projectSystem { }; files.push(file2); fileNames.push(file2.path); - expectedWatchedFiles.push(file2.path); + expectedWatchedFiles.set(file2.path, 1); expectedCompletions.push("file2"); host.reloadFS(files); host.runQueuedTimeoutCallbacks(); @@ -6635,7 +6634,8 @@ namespace ts.projectSystem { function verifyProjectAndCompletions() { checkWatchedDirectories(host, emptyArray, /*recursive*/ false); checkWatchedDirectories(host, emptyArray, /*recursive*/ true); - checkWatchedFiles(host, expectedWatchedFiles); + + ts.TestFSWithWatch.checkMultiMapKeyCount("watchedFiles", host.watchedFiles, expectedWatchedFiles); checkProjectActualFiles(project, fileNames); const completions = project.getLanguageService().getCompletionsAtPosition(index.path, completionPosition, { includeExternalModuleExports: false, includeInsertTextCompletions: false }); diff --git a/src/harness/virtualFileSystemWithWatch.ts b/src/harness/virtualFileSystemWithWatch.ts index 58f6371be84..bb7fa128c79 100644 --- a/src/harness/virtualFileSystemWithWatch.ts +++ b/src/harness/virtualFileSystemWithWatch.ts @@ -147,6 +147,18 @@ interface Array {}` } } + export function checkMultiMapKeyCount(caption: string, actual: MultiMap, expectedKeys: Map) { + verifyMapSize(caption, actual, arrayFrom(expectedKeys.keys())); + expectedKeys.forEach((count, name) => { + assert.isTrue(actual.has(name), `${caption}: expected to contain ${name}, actual keys: ${arrayFrom(actual.keys())}`); + assert.equal(actual.get(name).length, count, `${caption}: Expected to be have ${count} entries for ${name}. Actual entry: ${JSON.stringify(actual.get(name))}`); + }); + } + + export function checkMultiMapEachKeyWithCount(caption: string, actual: MultiMap, expectedKeys: ReadonlyArray, count: number) { + return checkMultiMapKeyCount(caption, actual, arrayToMap(expectedKeys, s => s, () => count)); + } + export function checkArray(caption: string, actual: ReadonlyArray, expected: ReadonlyArray) { assert.equal(actual.length, expected.length, `${caption}: incorrect actual number of files, expected:\r\n${expected.join("\r\n")}\r\ngot: ${actual.join("\r\n")}`); for (const f of expected) {