From d66e411264d43da003d89a700b480d0b011fadd3 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 20 Sep 2016 22:56:02 -0700 Subject: [PATCH 1/2] added unittest for watching @types --- Jakefile.js | 2 +- .../unittests/tsserverProjectSystem.ts | 51 ++++++++++++++++++- src/services/services.ts | 14 ++--- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index 86b79463bdbfa..4641df7f7b4a0 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -937,7 +937,7 @@ desc("Runs the tests using the built run.js file like 'jake runtests'. Syntax is task("runtests-browser", ["tests", "browserify", builtLocalDirectory, servicesFileInBrowserTest], function () { cleanTestDirs(); host = "node"; - browser = process.env.browser || process.env.b || "IE"; + browser = process.env.browser || process.env.b || (os.platform() === "linux" ? "chrome" : "IE"); tests = process.env.test || process.env.tests || process.env.t; var light = process.env.light || false; var testConfigFile = 'test.config'; diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index f34dfa31fc0c6..08384c6eae66d 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -1939,7 +1939,7 @@ namespace ts.projectSystem { content: JSON.stringify({ compilerOptions: { allowJs: true }, exclude: ["node_modules"] }) }; const host = createServerHost([f1, barjs, barTypings, config]); - const projectService = createProjectService(host, { typingsInstaller: new TestTypingsInstaller(typingsCacheLocation, host) }); + const projectService = createProjectService(host, { typingsInstaller: new TestTypingsInstaller(typingsCacheLocation, /*throttleLimit*/ 5, host) }); projectService.openClientFile(f1.path); projectService.checkNumberOfProjects({ configuredProjects: 1 }); @@ -1987,4 +1987,53 @@ namespace ts.projectSystem { assert.deepEqual(s3, newPerFileSettings, "file settings should still be the same with per-file settings"); }); }); + + describe("watching @types", () => { + it("works correctly when typings are added or removed", () => { + const f1 = { + path: "/a/b/app.ts", + content: "let x = 1;" + }; + const t1 = { + path: "/a/b/node_modules/@types/lib1/index.d.ts", + content: "export let a: number" + }; + const t2 = { + path: "/a/b/node_modules/@types/lib2/index.d.ts", + content: "export let b: number" + }; + const tsconfig = { + path: "/a/b/tsconfig.json", + content: JSON.stringify({ + compilerOptions: {}, + exclude: ["node_modules"] + }) + }; + debugger + const host = createServerHost([f1, t1, tsconfig]); + const projectService = createProjectService(host); + + projectService.openClientFile(f1.path); + projectService.checkNumberOfProjects({ configuredProjects: 1 }); + checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, t1.path]); + + // delete t1 + host.reloadFS([f1, tsconfig]); + host.triggerDirectoryWatcherCallback("/a/b/node_modules/@types", "lib1"); + // run throttled operation + host.runQueuedTimeoutCallbacks(); + + projectService.checkNumberOfProjects({ configuredProjects: 1 }); + checkProjectActualFiles(projectService.configuredProjects[0], [f1.path]); + + // create t2 + host.reloadFS([f1, tsconfig, t2]); + host.triggerDirectoryWatcherCallback("/a/b/node_modules/@types", "lib2"); + // run throttled operation + host.runQueuedTimeoutCallbacks(); + + projectService.checkNumberOfProjects({ configuredProjects: 1 }); + checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, t2.path]); + }) + }) } \ No newline at end of file diff --git a/src/services/services.ts b/src/services/services.ts index 4b6e27e6c63c9..c59fb53282ec1 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -3154,6 +3154,13 @@ namespace ts { } } + const typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; + if (lastTypesRootVersion !== typeRootsVersion) { + log("TypeRoots version has changed; provide new program"); + program = undefined; + lastTypesRootVersion = typeRootsVersion; + } + // Get a fresh cache of the host information let hostCache = new HostCache(host, getCanonicalFileName); @@ -3221,13 +3228,6 @@ namespace ts { }; } - const typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; - if (lastTypesRootVersion !== typeRootsVersion) { - log("TypeRoots version has changed; provide new program"); - program = undefined; - lastTypesRootVersion = typeRootsVersion; - } - const documentRegistryBucketKey = documentRegistry.getKeyForCompilationSettings(newSettings); const newProgram = createProgram(hostCache.getRootFileNames(), newSettings, compilerHost, program); From 15ccbecba376f9efd872e9680b533d91bced537a Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 20 Sep 2016 23:05:01 -0700 Subject: [PATCH 2/2] remove debugger statement, fix linter issues --- src/harness/unittests/tsserverProjectSystem.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 08384c6eae66d..2940360c218ff 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -2009,7 +2009,6 @@ namespace ts.projectSystem { exclude: ["node_modules"] }) }; - debugger const host = createServerHost([f1, t1, tsconfig]); const projectService = createProjectService(host); @@ -2034,6 +2033,6 @@ namespace ts.projectSystem { projectService.checkNumberOfProjects({ configuredProjects: 1 }); checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, t2.path]); - }) - }) + }); + }); } \ No newline at end of file