1import * as ts from "../../_namespaces/ts"; 2 3describe("unittests:: tsserver:: searching for config file", () => { 4 it("should stop at projectRootPath if given", () => { 5 const f1 = { 6 path: "/a/file1.ts", 7 content: "" 8 }; 9 const configFile = { 10 path: "/tsconfig.json", 11 content: "{}" 12 }; 13 const host = ts.projectSystem.createServerHost([f1, configFile]); 14 const service = ts.projectSystem.createProjectService(host); 15 service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, "/a"); 16 17 ts.projectSystem.checkNumberOfConfiguredProjects(service, 0); 18 ts.projectSystem.checkNumberOfInferredProjects(service, 1); 19 20 service.closeClientFile(f1.path); 21 service.openClientFile(f1.path); 22 ts.projectSystem.checkNumberOfConfiguredProjects(service, 1); 23 ts.projectSystem.checkNumberOfInferredProjects(service, 0); 24 }); 25 26 it("should use projectRootPath when searching for inferred project again", () => { 27 const projectDir = "/a/b/projects/project"; 28 const configFileLocation = `${projectDir}/src`; 29 const f1 = { 30 path: `${configFileLocation}/file1.ts`, 31 content: "" 32 }; 33 const configFile = { 34 path: `${configFileLocation}/tsconfig.json`, 35 content: "{}" 36 }; 37 const configFile2 = { 38 path: "/a/b/projects/tsconfig.json", 39 content: "{}" 40 }; 41 const host = ts.projectSystem.createServerHost([f1, ts.projectSystem.libFile, configFile, configFile2]); 42 const service = ts.projectSystem.createProjectService(host, { logger: ts.projectSystem.createLoggerWithInMemoryLogs(host) }); 43 service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectDir); 44 45 // Delete config file - should create inferred project and not configured project 46 host.deleteFile(configFile.path); 47 host.runQueuedTimeoutCallbacks(); 48 ts.projectSystem.checkNumberOfProjects(service, { inferredProjects: 1 }); 49 ts.projectSystem.baselineTsserverLogs("configFileSearch", "should use projectRootPath when searching for inferred project again", service); 50 }); 51 52 it("should use projectRootPath when searching for inferred project again 2", () => { 53 const projectDir = "/a/b/projects/project"; 54 const configFileLocation = `${projectDir}/src`; 55 const f1 = { 56 path: `${configFileLocation}/file1.ts`, 57 content: "" 58 }; 59 const configFile = { 60 path: `${configFileLocation}/tsconfig.json`, 61 content: "{}" 62 }; 63 const configFile2 = { 64 path: "/a/b/projects/tsconfig.json", 65 content: "{}" 66 }; 67 const host = ts.projectSystem.createServerHost([f1, ts.projectSystem.libFile, configFile, configFile2]); 68 const service = ts.projectSystem.createProjectService(host, { 69 useSingleInferredProject: true, 70 useInferredProjectPerProjectRoot: true, 71 logger: ts.projectSystem.createLoggerWithInMemoryLogs(host), 72 }); 73 service.openClientFile(f1.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectDir); 74 75 // Delete config file - should create inferred project with project root path set 76 host.deleteFile(configFile.path); 77 host.runQueuedTimeoutCallbacks(); 78 ts.projectSystem.baselineTsserverLogs("configFileSearch", "should use projectRootPath when searching for inferred project again 2", service); 79 }); 80 81 describe("when the opened file is not from project root", () => { 82 const projectRoot = "/a/b/projects/project"; 83 const file: ts.projectSystem.File = { 84 path: `${projectRoot}/src/index.ts`, 85 content: "let y = 10" 86 }; 87 const tsconfig: ts.projectSystem.File = { 88 path: `${projectRoot}/tsconfig.json`, 89 content: "{}" 90 }; 91 function openClientFile(files: ts.projectSystem.File[]) { 92 const host = ts.projectSystem.createServerHost(files); 93 const projectService = ts.projectSystem.createProjectService(host, { logger: ts.projectSystem.createLoggerWithInMemoryLogs(host) }); 94 projectService.openClientFile(file.path, /*fileContent*/ undefined, /*scriptKind*/ undefined, "/a/b/projects/proj"); 95 return { host, projectService }; 96 } 97 98 it("tsconfig for the file exists", () => { 99 const { host, projectService } = openClientFile([file, ts.projectSystem.libFile, tsconfig]); 100 101 host.deleteFile(tsconfig.path); 102 host.runQueuedTimeoutCallbacks(); 103 104 host.writeFile(tsconfig.path, tsconfig.content); 105 host.runQueuedTimeoutCallbacks(); 106 107 ts.projectSystem.baselineTsserverLogs("configFileSearch", "tsconfig for the file exists", projectService); 108 }); 109 110 it("tsconfig for the file does not exist", () => { 111 const { host, projectService } = openClientFile([file, ts.projectSystem.libFile]); 112 113 host.writeFile(tsconfig.path, tsconfig.content); 114 host.runQueuedTimeoutCallbacks(); 115 116 host.deleteFile(tsconfig.path); 117 host.runQueuedTimeoutCallbacks(); 118 119 ts.projectSystem.baselineTsserverLogs("configFileSearch", "tsconfig for the file does not exist", projectService); 120 }); 121 }); 122 123 describe("should not search and watch config files from directories that cannot be watched", () => { 124 function verifyConfigFileWatch(scenario: string, projectRootPath: string | undefined) { 125 it(scenario, () => { 126 const path = `/root/teams/VSCode68/Shared Documents/General/jt-ts-test-workspace/x.js`; 127 const host = ts.projectSystem.createServerHost([ts.projectSystem.libFile, { path, content: "const x = 10" }], { useCaseSensitiveFileNames: true }); 128 const service = ts.projectSystem.createProjectService(host, { logger: ts.projectSystem.createLoggerWithInMemoryLogs(host) }); 129 service.openClientFile(path, /*fileContent*/ undefined, /*scriptKind*/ undefined, projectRootPath); 130 ts.projectSystem.baselineTsserverLogs("configFileSearch", scenario, service); 131 }); 132 } 133 verifyConfigFileWatch("when projectRootPath is not present", /*projectRootPath*/ undefined); 134 verifyConfigFileWatch("when projectRootPath is present but file is not from project root", "/a/b"); 135 }); 136}); 137