Skip to content

Commit bebc571

Browse files
committed
more tests
1 parent a9cd516 commit bebc571

3 files changed

Lines changed: 75 additions & 31 deletions

File tree

src/server/editorServices.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ namespace ts.server {
7272
/**
7373
* a path to directory watcher map that detects added tsconfig files
7474
**/
75-
private directoryWatchersForTsconfig: Map<FileWatcher> = {};
75+
private readonly directoryWatchersForTsconfig: Map<FileWatcher> = {};
7676
/**
7777
* count of how many projects are using the directory watcher.
7878
* If the number becomes 0 for a watcher, then we should close it.
7979
**/
80-
private directoryWatchersRefCount: Map<number> = {};
80+
private readonly directoryWatchersRefCount: Map<number> = {};
8181

8282
constructor(private readonly projectService: ProjectService) {
8383
}
@@ -208,7 +208,7 @@ namespace ts.server {
208208
private updateProjectGraphs(projects: Project[]) {
209209
let shouldRefreshInferredProjects = false;
210210
for (const p of projects) {
211-
if (p.updateGraph()) {
211+
if (!p.updateGraph()) {
212212
shouldRefreshInferredProjects = true;
213213
}
214214
}
@@ -231,7 +231,6 @@ namespace ts.server {
231231
if (info && (!info.isOpen)) {
232232
// file has been changed which might affect the set of referenced files in projects that include
233233
// this file and set of inferred projects
234-
// TODO: add tests
235234
info.reloadFromFile();
236235
this.updateProjectGraphs(info.containingProjects);
237236
}

src/server/project.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ namespace ts.server {
190190
this.projectStateVersion++;
191191
}
192192

193+
/**
194+
* Updates set of files that contribute to this project
195+
* @returns: true if set of files in the project stays the same and false - otherwise.
196+
*/
193197
updateGraph(): boolean {
194198
if (!this.languageServiceEnabled) {
195199
return true;

tests/cases/unittests/tsserverProjectSystem.ts

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,34 @@ namespace ts {
2121
};
2222

2323
const { content: libFileContent } = Harness.getDefaultLibraryFile(Harness.IO);
24+
const libFile: FileOrFolder = {
25+
path: "/a/lib/lib.d.ts",
26+
content: libFileContent
27+
};
2428

25-
function getExecutingFilePathFromLibFile(libFile: FileOrFolder): string {
29+
function getExecutingFilePathFromLibFile(libFilePath: string): string {
2630
return combinePaths(getDirectoryPath(libFile.path), "tsc.js");
2731
}
2832

2933
interface TestServerHostCreationParameters {
30-
fileOrFolderList: FileOrFolder[];
3134
useCaseSensitiveFileNames?: boolean;
3235
executingFilePath?: string;
3336
libFile?: FileOrFolder;
3437
currentDirectory?: string;
3538
}
3639

37-
function createServerHost(params: TestServerHostCreationParameters): TestServerHost {
40+
function createServerHost(fileOrFolderList: FileOrFolder[],
41+
params?: TestServerHostCreationParameters,
42+
libFilePath: string = libFile.path): TestServerHost {
43+
44+
if (!params) {
45+
params = {};
46+
}
3847
return new TestServerHost(
3948
params.useCaseSensitiveFileNames !== undefined ? params.useCaseSensitiveFileNames : false,
40-
params.executingFilePath || getExecutingFilePathFromLibFile(params.libFile),
49+
params.executingFilePath || getExecutingFilePathFromLibFile(libFilePath),
4150
params.currentDirectory || "/",
42-
params.fileOrFolderList);
51+
fileOrFolderList);
4352
}
4453

4554
interface FileOrFolder {
@@ -365,10 +374,6 @@ namespace ts {
365374
path: "/a/b/commonFile2.ts",
366375
content: "let y = 1"
367376
};
368-
const libFile: FileOrFolder = {
369-
path: "/a/lib/lib.d.ts",
370-
content: libFileContent
371-
};
372377

373378
it("create inferred project", () => {
374379
const appFile: FileOrFolder = {
@@ -383,7 +388,7 @@ namespace ts {
383388
path: "/a/b/c/module.d.ts",
384389
content: `export let x: number`
385390
};
386-
const host = createServerHost({ fileOrFolderList: [appFile, moduleFile, libFile], libFile });
391+
const host = createServerHost([appFile, moduleFile, libFile]);
387392
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
388393
const { configFileName } = projectService.openClientFile(appFile.path);
389394

@@ -421,8 +426,7 @@ namespace ts {
421426
content: "let z = 1"
422427
};
423428

424-
425-
const host = createServerHost({ fileOrFolderList: [configFile, libFile, file1, file2, file3], libFile });
429+
const host = createServerHost([configFile, libFile, file1, file2, file3]);
426430
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
427431
const { configFileName, configFileErrors } = projectService.openClientFile(file1.path);
428432

@@ -447,7 +451,7 @@ namespace ts {
447451
}`
448452
};
449453
const filesWithoutConfig = [libFile, commonFile1, commonFile2];
450-
const host = createServerHost({ fileOrFolderList: filesWithoutConfig, libFile });
454+
const host = createServerHost(filesWithoutConfig);
451455

452456
const filesWithConfig = [libFile, commonFile1, commonFile2, configFile];
453457
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
@@ -480,7 +484,7 @@ namespace ts {
480484
path: "/a/b/tsconfig.json",
481485
content: `{}`
482486
};
483-
const host = createServerHost({ fileOrFolderList: [commonFile1, libFile, configFile], libFile });
487+
const host = createServerHost([commonFile1, libFile, configFile]);
484488
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
485489
projectService.openClientFile(commonFile1.path);
486490
checkWatchedDirectories(host, ["/a/b"]);
@@ -508,7 +512,7 @@ namespace ts {
508512
]
509513
}`
510514
};
511-
const host = createServerHost({ fileOrFolderList: [commonFile1, commonFile2, configFile], libFile });
515+
const host = createServerHost([commonFile1, commonFile2, configFile]);
512516
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
513517
projectService.openClientFile(commonFile1.path);
514518
projectService.openClientFile(commonFile2.path);
@@ -524,7 +528,7 @@ namespace ts {
524528
path: "/a/b/tsconfig.json",
525529
content: `{}`
526530
};
527-
const host = createServerHost({ fileOrFolderList: [commonFile1, commonFile2, configFile], libFile });
531+
const host = createServerHost([commonFile1, commonFile2, configFile]);
528532
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
529533
projectService.openClientFile(commonFile1.path);
530534

@@ -554,7 +558,7 @@ namespace ts {
554558
}`
555559
};
556560
const files = [commonFile1, commonFile2, configFile];
557-
const host = createServerHost({ fileOrFolderList: files, libFile });
561+
const host = createServerHost(files);
558562
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
559563
projectService.openClientFile(commonFile1.path);
560564

@@ -587,7 +591,7 @@ namespace ts {
587591
content: `let t = 1;`
588592
};
589593

590-
const host = createServerHost({ fileOrFolderList: [commonFile1, commonFile2, excludedFile1, configFile], libFile });
594+
const host = createServerHost([commonFile1, commonFile2, excludedFile1, configFile]);
591595
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
592596

593597
projectService.openClientFile(commonFile1.path);
@@ -621,7 +625,7 @@ namespace ts {
621625
}`
622626
};
623627
const files = [file1, nodeModuleFile, classicModuleFile, configFile];
624-
const host = createServerHost({ fileOrFolderList: files, libFile });
628+
const host = createServerHost(files);
625629
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
626630
projectService.openClientFile(file1.path);
627631
projectService.openClientFile(nodeModuleFile.path);
@@ -662,7 +666,7 @@ namespace ts {
662666
"files": [ "main.ts" ]
663667
}`
664668
};
665-
const host = createServerHost({ fileOrFolderList: [file1, file2, configFile], libFile });
669+
const host = createServerHost([file1, file2, configFile]);
666670
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
667671
projectService.openClientFile(file1.path);
668672
projectService.closeClientFile(file1.path);
@@ -689,7 +693,7 @@ namespace ts {
689693
"files": [ "main.ts" ]
690694
}`
691695
};
692-
const host = createServerHost({ fileOrFolderList: [file1, file2, configFile], libFile });
696+
const host = createServerHost([file1, file2, configFile]);
693697
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
694698
projectService.openClientFile(file1.path);
695699
projectService.closeClientFile(file1.path);
@@ -722,7 +726,7 @@ namespace ts {
722726
content: "let x =1;"
723727
};
724728

725-
const host = createServerHost({ fileOrFolderList: [file1, file2, file3, libFile], libFile });
729+
const host = createServerHost([file1, file2, file3, libFile]);
726730
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ true);
727731
projectService.openClientFile(file1.path);
728732
projectService.openClientFile(file2.path);
@@ -755,7 +759,7 @@ namespace ts {
755759
"files": [ "main.ts" ]
756760
}`
757761
};
758-
const host = createServerHost({ fileOrFolderList: [file1, configFile, libFile], libFile });
762+
const host = createServerHost([file1, configFile, libFile]);
759763
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ true);
760764
projectService.openClientFile(file1.path);
761765
checkNumberOfConfiguredProjects(projectService, 1);
@@ -774,7 +778,7 @@ namespace ts {
774778
content: "let y =1;"
775779
};
776780
const externalProjectName = "externalproject";
777-
const host = createServerHost({ fileOrFolderList: [file1, file2], libFile });
781+
const host = createServerHost([file1, file2]);
778782
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
779783
projectService.openExternalProject({
780784
rootFiles: [ file1.path, file2.path ],
@@ -832,7 +836,7 @@ namespace ts {
832836
content: "let z =1;"
833837
};
834838
const externalProjectName = "externalproject";
835-
const host = createServerHost({ fileOrFolderList: [file1, file2, file3, config1, config2], libFile });
839+
const host = createServerHost([file1, file2, file3, config1, config2]);
836840
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
837841
projectService.openExternalProject({
838842
rootFiles: [ config1.path, config2.path, file3.path ],
@@ -882,7 +886,7 @@ namespace ts {
882886
content: JSON.stringify({ compilerOptions: {} })
883887
};
884888
const externalProjectName = "externalproject";
885-
const host = createServerHost({ fileOrFolderList: [file1, configFile], libFile });
889+
const host = createServerHost([file1, configFile]);
886890
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
887891

888892
projectService.openClientFile(file1.path);
@@ -922,7 +926,7 @@ namespace ts {
922926
content: JSON.stringify({ compilerOptions: {} })
923927
};
924928
const externalProjectName = "externalproject";
925-
const host = createServerHost({ fileOrFolderList: [file1, configFile], libFile });
929+
const host = createServerHost([file1, configFile]);
926930
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useOneInferredProject*/ false);
927931

928932
projectService.openClientFile(file1.path);
@@ -952,5 +956,42 @@ namespace ts {
952956
checkNumberOfConfiguredProjects(projectService, 0);
953957
checkNumberOfInferredProjects(projectService, 0);
954958
});
959+
960+
it("changes in closed files are reflected in project structure", () => {
961+
const file1 = {
962+
path: "/a/b/f1.ts",
963+
content: `export * from "./f2"`
964+
};
965+
const file2 = {
966+
path: "/a/b/f2.ts",
967+
content: `export let x = 1`
968+
};
969+
const file3 = {
970+
path: "/a/c/f3.ts",
971+
content: `export let y = 1;`
972+
};
973+
const host = createServerHost([file1, file2, file3]);
974+
const projectService = new server.ProjectService(host, nullLogger, nullCancellationToken, /*useSingleInferredProject*/ false);
975+
976+
projectService.openClientFile(file1.path);
977+
978+
checkNumberOfInferredProjects(projectService, 1);
979+
checkProjectActualFiles(projectService.inferredProjects[0], [ file1.path, file2.path ]);
980+
981+
projectService.openClientFile(file3.path);
982+
checkNumberOfInferredProjects(projectService, 2);
983+
checkProjectActualFiles(projectService.inferredProjects[1], [ file3.path ]);
984+
985+
const modifiedFile2 = {
986+
path: file2.path,
987+
content: `export * from "../c/f3"` // now inferred project should inclule file3
988+
};
989+
990+
host.reloadFS([file1, modifiedFile2, file3]);
991+
host.triggerFileWatcherCallback(modifiedFile2.path, /*removed*/ false);
992+
993+
checkNumberOfInferredProjects(projectService, 1);
994+
checkProjectActualFiles(projectService.inferredProjects[0], [ file1.path, modifiedFile2.path, file3.path ]);
995+
});
955996
});
956997
}

0 commit comments

Comments
 (0)