From e8a0e56e3e523ac4e237c94280692e1145b212f2 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 22 May 2018 11:45:52 -0700 Subject: [PATCH] Add a test when orphan file changes before it gets added back to project --- .../unittests/tsserverProjectSystem.ts | 107 ++++++++++++------ 1 file changed, 75 insertions(+), 32 deletions(-) diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 561375e30df..592cf4a9e4c 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -8416,52 +8416,95 @@ new C();` }); describe("document registry in project service", () => { - it("Caches the source file if script info is orphan", () => { - const projectRootPath = "/user/username/projects/project"; - const importModuleContent = `import {a} from "./module1"`; - const file: File = { - path: `${projectRootPath}/index.ts`, - content: importModuleContent - }; - const moduleFile: File = { - path: `${projectRootPath}/module1.d.ts`, - content: "export const a: number;" - }; - const configFile: File = { - path: `${projectRootPath}/tsconfig.json`, - content: JSON.stringify({ files: ["index.ts"] }) - }; + const projectRootPath = "/user/username/projects/project"; + const importModuleContent = `import {a} from "./module1"`; + const file: File = { + path: `${projectRootPath}/index.ts`, + content: importModuleContent + }; + const moduleFile: File = { + path: `${projectRootPath}/module1.d.ts`, + content: "export const a: number;" + }; + const configFile: File = { + path: `${projectRootPath}/tsconfig.json`, + content: JSON.stringify({ files: ["index.ts"] }) + }; + + function getProject(service: TestProjectService) { + return service.configuredProjects.get(configFile.path); + } + + function checkProject(service: TestProjectService, moduleIsOrphan: boolean) { + // Update the project + const project = getProject(service); + project.getLanguageService(); + checkProjectActualFiles(project, [file.path, libFile.path, configFile.path, ...(moduleIsOrphan ? [] : [moduleFile.path])]); + const moduleInfo = service.getScriptInfo(moduleFile.path); + assert.isDefined(moduleInfo); + assert.equal(moduleInfo.isOrphan(), moduleIsOrphan); + const key = service.documentRegistry.getKeyForCompilationSettings(project.getCompilationSettings()); + assert.deepEqual(service.documentRegistry.getLanguageServiceRefCounts(moduleInfo.path), [[key, moduleIsOrphan ? undefined : 1]]); + } + + function createServiceAndHost() { const host = createServerHost([file, moduleFile, libFile, configFile]); const service = createProjectService(host); service.openClientFile(file.path); - const project = service.configuredProjects.get(configFile.path); - checkProject(/*moduleIsOrphan*/ false); + checkProject(service, /*moduleIsOrphan*/ false); + return { host, service }; + } + + function changeFileToNotImportModule(service: TestProjectService) { + const info = service.getScriptInfo(file.path); + service.applyChangesToFile(info, [{ span: { start: 0, length: importModuleContent.length }, newText: "" }]); + checkProject(service, /*moduleIsOrphan*/ true); + } + + function changeFileToImportModule(service: TestProjectService) { + const info = service.getScriptInfo(file.path); + service.applyChangesToFile(info, [{ span: { start: 0, length: 0 }, newText: importModuleContent }]); + checkProject(service, /*moduleIsOrphan*/ false); + } + + it("Caches the source file if script info is orphan", () => { + const { service } = createServiceAndHost(); + const project = getProject(service); + const moduleInfo = service.getScriptInfo(moduleFile.path); const sourceFile = moduleInfo.cacheSourceFile.sourceFile; assert.equal(project.getSourceFile(moduleInfo.path), sourceFile); // edit file - const info = service.getScriptInfo(file.path); - service.applyChangesToFile(info, [{ span: { start: 0, length: importModuleContent.length }, newText: "" }]); - checkProject(/*moduleIsOrphan*/ true); + changeFileToNotImportModule(service); assert.equal(moduleInfo.cacheSourceFile.sourceFile, sourceFile); // write content back - service.applyChangesToFile(info, [{ span: { start: 0, length: 0 }, newText: importModuleContent }]); - checkProject(/*moduleIsOrphan*/ false); + changeFileToImportModule(service); assert.equal(moduleInfo.cacheSourceFile.sourceFile, sourceFile); assert.equal(project.getSourceFile(moduleInfo.path), sourceFile); + }); - function checkProject(moduleIsOrphan: boolean) { - // Update the project - project.getLanguageService(); - checkProjectActualFiles(project, [file.path, libFile.path, configFile.path, ...(moduleIsOrphan ? [] : [moduleFile.path])]); - const moduleInfo = service.getScriptInfo(moduleFile.path); - assert.isDefined(moduleInfo); - assert.equal(moduleInfo.isOrphan(), moduleIsOrphan); - const key = service.documentRegistry.getKeyForCompilationSettings(project.getCompilationSettings()); - assert.deepEqual(service.documentRegistry.getLanguageServiceRefCounts(moduleInfo.path), [[key, moduleIsOrphan ? undefined : 1]]); - } + it("Caches the source file if script info is orphan, and orphan script info changes", () => { + const { host, service } = createServiceAndHost(); + const project = getProject(service); + + const moduleInfo = service.getScriptInfo(moduleFile.path); + const sourceFile = moduleInfo.cacheSourceFile.sourceFile; + assert.equal(project.getSourceFile(moduleInfo.path), sourceFile); + + // edit file + changeFileToNotImportModule(service); + assert.equal(moduleInfo.cacheSourceFile.sourceFile, sourceFile); + + const updatedModuleContent = moduleFile.content + "\nexport const b: number;"; + host.writeFile(moduleFile.path, updatedModuleContent); + + // write content back + changeFileToImportModule(service); + assert.notEqual(moduleInfo.cacheSourceFile.sourceFile, sourceFile); + assert.equal(project.getSourceFile(moduleInfo.path), moduleInfo.cacheSourceFile.sourceFile); + assert.equal(moduleInfo.cacheSourceFile.sourceFile.text, updatedModuleContent); }); }); }