Skip to content

Commit 96c66a6

Browse files
committed
Add test case to verify reload works without open project
1 parent b16e6d4 commit 96c66a6

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,6 +3893,96 @@ namespace ts.projectSystem {
38933893
assert.equal(snap2.getText(0, snap2.getLength()), f1.content, "content should be equal to the content of original file");
38943894

38953895
});
3896+
3897+
it("should work when script info doesnt have any project open", () => {
3898+
const f1 = {
3899+
path: "/a/b/app.ts",
3900+
content: "let x = 1"
3901+
};
3902+
const tmp = {
3903+
path: "/a/b/app.tmp",
3904+
content: "const y = 42"
3905+
};
3906+
const host = createServerHost([f1, tmp, libFile]);
3907+
const session = createSession(host);
3908+
const openContent = "let z = 1";
3909+
// send open request
3910+
session.executeCommandSeq(<server.protocol.OpenRequest>{
3911+
command: server.protocol.CommandTypes.Open,
3912+
arguments: { file: f1.path, fileContent: openContent }
3913+
});
3914+
3915+
const projectService = session.getProjectService();
3916+
checkNumberOfProjects(projectService, { inferredProjects: 1 });
3917+
const info = projectService.getScriptInfo(f1.path);
3918+
assert.isDefined(info);
3919+
checkScriptInfoContents(openContent, "contents set during open request");
3920+
3921+
// send close request
3922+
session.executeCommandSeq(<server.protocol.CloseRequest>{
3923+
command: server.protocol.CommandTypes.Close,
3924+
arguments: { file: f1.path }
3925+
});
3926+
checkScriptInfoAndProjects(0, f1.content, "contents of closed file");
3927+
3928+
// Can reload contents of the file when its not open and has no project
3929+
// reload from temp file
3930+
session.executeCommandSeq(<server.protocol.ReloadRequest>{
3931+
command: server.protocol.CommandTypes.Reload,
3932+
arguments: { file: f1.path, tmpfile: tmp.path }
3933+
});
3934+
checkScriptInfoAndProjects(0, tmp.content, "contents of temp file");
3935+
3936+
// reload from own file
3937+
session.executeCommandSeq(<server.protocol.ReloadRequest>{
3938+
command: server.protocol.CommandTypes.Reload,
3939+
arguments: { file: f1.path }
3940+
});
3941+
checkScriptInfoAndProjects(0, f1.content, "contents of closed file");
3942+
3943+
// Open file again without setting its content
3944+
session.executeCommandSeq(<server.protocol.OpenRequest>{
3945+
command: server.protocol.CommandTypes.Open,
3946+
arguments: { file: f1.path }
3947+
});
3948+
checkScriptInfoAndProjects(1, f1.content, "contents of file when opened without specifying contents");
3949+
const snap = info.getSnapshot();
3950+
3951+
// send close request
3952+
session.executeCommandSeq(<server.protocol.CloseRequest>{
3953+
command: server.protocol.CommandTypes.Close,
3954+
arguments: { file: f1.path }
3955+
});
3956+
checkScriptInfoAndProjects(0, f1.content, "contents of closed file");
3957+
assert.strictEqual(info.getSnapshot(), snap);
3958+
3959+
// reload from temp file
3960+
session.executeCommandSeq(<server.protocol.ReloadRequest>{
3961+
command: server.protocol.CommandTypes.Reload,
3962+
arguments: { file: f1.path, tmpfile: tmp.path }
3963+
});
3964+
checkScriptInfoAndProjects(0, tmp.content, "contents of temp file");
3965+
assert.notStrictEqual(info.getSnapshot(), snap);
3966+
3967+
// reload from own file
3968+
session.executeCommandSeq(<server.protocol.ReloadRequest>{
3969+
command: server.protocol.CommandTypes.Reload,
3970+
arguments: { file: f1.path }
3971+
});
3972+
checkScriptInfoAndProjects(0, f1.content, "contents of closed file");
3973+
assert.notStrictEqual(info.getSnapshot(), snap);
3974+
3975+
function checkScriptInfoAndProjects(inferredProjects: number, contentsOfInfo: string, captionForContents: string) {
3976+
checkNumberOfProjects(projectService, { inferredProjects });
3977+
assert.strictEqual(projectService.getScriptInfo(f1.path), info);
3978+
checkScriptInfoContents(contentsOfInfo, captionForContents);
3979+
}
3980+
3981+
function checkScriptInfoContents(contentsOfInfo: string, captionForContents: string) {
3982+
const snap = info.getSnapshot();
3983+
assert.equal(snap.getText(0, snap.getLength()), contentsOfInfo, "content should be equal to " + captionForContents);
3984+
}
3985+
});
38963986
});
38973987

38983988
describe("Inferred projects", () => {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7227,7 +7227,6 @@ declare namespace ts.server {
72277227
getScriptInfo(uncheckedFileName: string): ScriptInfo;
72287228
filesToString(writeProjectFileNames: boolean): string;
72297229
setCompilerOptions(compilerOptions: CompilerOptions): void;
7230-
reloadScript(filename: NormalizedPath, tempFileName?: NormalizedPath): boolean;
72317230
protected removeRoot(info: ScriptInfo): void;
72327231
}
72337232
/**

0 commit comments

Comments
 (0)