Skip to content

Commit e9f9c08

Browse files
committed
Some projectInfo tests
1 parent b1b41b3 commit e9f9c08

File tree

28 files changed

+4176
-446
lines changed

28 files changed

+4176
-446
lines changed

src/testRunner/unittests/helpers/tsserver.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,19 @@ export function openFilesForSession(
462462
}
463463
}
464464

465+
export function projectInfoForSession(
466+
session: TestSession,
467+
file: string | File,
468+
) {
469+
return session.executeCommandSeq<ts.server.protocol.ProjectInfoRequest>({
470+
command: ts.server.protocol.CommandTypes.ProjectInfo,
471+
arguments: {
472+
file: ts.isString(file) ? file : file.path,
473+
needFileNameList: false,
474+
},
475+
}).response as ts.server.protocol.ProjectInfo;
476+
}
477+
465478
export function closeFilesForSession(files: readonly (File | string)[], session: TestSession): void {
466479
for (const file of files) {
467480
session.executeCommandSeq<ts.server.protocol.CloseRequest>({

src/testRunner/unittests/tsserver/cancellationToken.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ts from "../../_namespaces/ts.js";
22
import { jsonToReadableText } from "../helpers.js";
33
import {
44
baselineTsserverLogs,
5+
projectInfoForSession,
56
TestSession,
67
TestSessionRequest,
78
} from "../helpers/tsserver.js";
@@ -88,10 +89,7 @@ describe("unittests:: tsserver:: cancellationToken::", () => {
8889
});
8990

9091
// run new request
91-
session.executeCommandSeq<ts.server.protocol.ProjectInfoRequest>({
92-
command: ts.server.protocol.CommandTypes.ProjectInfo,
93-
arguments: { file: f1.path, needFileNameList: false },
94-
});
92+
projectInfoForSession(session, f1);
9593

9694
// cancel previously issued Geterr
9795
session.serverCancellationToken.setRequestToCancel(getErrId);

src/testRunner/unittests/tsserver/configuredProjects.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
baselineTsserverLogs,
77
closeFilesForSession,
88
openFilesForSession,
9+
projectInfoForSession,
910
TestSession,
1011
verifyGetErrRequest,
1112
} from "../helpers/tsserver.js";
@@ -152,6 +153,7 @@ describe("unittests:: tsserver:: configuredProjects::", () => {
152153
const { host, session, commonFile1, commonFile2, configFile } = setup(parentOrSiblingConfigFile);
153154

154155
openFilesForSession([commonFile1], session);
156+
projectInfoForSession(session, commonFile1);
155157

156158
session.logger.log("1: When config file is deleted and then another file is opened");
157159
// remove the tsconfig file
@@ -236,13 +238,35 @@ describe("unittests:: tsserver:: configuredProjects::", () => {
236238
openFilesForSession([commonFile1, commonFile2], session);
237239
closeFilesForSession([commonFile2], session);
238240
openFilesForSession([{ file: "/user/username/projects/random/random.ts", content: "export const y = 10;" }], session);
241+
closeFilesForSession(["/user/username/projects/random/random.ts"], session);
242+
243+
session.logger.log("7: When config file is deleted and then another file is opened and projectInfo");
244+
// remove the tsconfig file
245+
host.deleteFile(configFile.path);
246+
openFilesForSession([commonFile2], session);
247+
248+
projectInfoForSession(session, commonFile1);
249+
projectInfoForSession(session, commonFile2);
250+
251+
// Add a tsconfig file
252+
host.writeFile(configFile.path, configFile.content);
253+
host.runQueuedTimeoutCallbacks();
254+
255+
session.logger.log("8: When both files are open and config file is deleted and projectInfo");
256+
// remove the tsconfig file
257+
host.deleteFile(configFile.path);
258+
host.runQueuedTimeoutCallbacks();
259+
260+
projectInfoForSession(session, commonFile1);
261+
projectInfoForSession(session, commonFile2);
239262

240263
baselineTsserverLogs("configuredProjects", `add and then remove a config file ${scenario}`, session);
241264
});
242265

243266
it(`add and then remove a config file ${scenario} and file from first config is not open`, () => {
244267
const { host, session, commonFile2, configFile } = setup(parentOrSiblingConfigFile);
245268
openFilesForSession([commonFile2], session);
269+
projectInfoForSession(session, commonFile2);
246270

247271
session.logger.log("1: When config file is deleted");
248272
// remove the tsconfig file
@@ -271,6 +295,27 @@ describe("unittests:: tsserver:: configuredProjects::", () => {
271295
closeFilesForSession([commonFile2], session);
272296
openFilesForSession([{ file: "/user/username/projects/random/random.ts", content: "export const y = 10;" }], session);
273297

298+
// Add a tsconfig file
299+
host.writeFile(configFile.path, configFile.content);
300+
host.runQueuedTimeoutCallbacks();
301+
closeFilesForSession(["/user/username/projects/random/random.ts"], session);
302+
openFilesForSession([commonFile2], session);
303+
304+
session.logger.log("3: When config file is deleted and projectInfo");
305+
// remove the tsconfig file
306+
host.deleteFile(configFile.path);
307+
host.runQueuedTimeoutCallbacks();
308+
projectInfoForSession(session, commonFile2);
309+
310+
// Add a tsconfig file
311+
host.writeFile(configFile.path, configFile.content);
312+
host.runQueuedTimeoutCallbacks();
313+
314+
session.logger.log("4: Check when file is closed when config file is deleted and projectInfo");
315+
// remove the tsconfig file
316+
host.deleteFile(configFile.path);
317+
projectInfoForSession(session, commonFile2);
318+
274319
baselineTsserverLogs("configuredProjects", `add and then remove a config file ${scenario} and file from first config is not open`, session);
275320
});
276321
}
@@ -995,7 +1040,7 @@ foo();`,
9951040
endOffset: 1,
9961041
},
9971042
});
998-
session.logger.log(`Default project for file: ${fooDts}: ${session.getProjectService().tryGetDefaultProjectForFile(ts.server.toNormalizedPath(fooDts))?.projectName}`);
1043+
projectInfoForSession(session, fooDts);
9991044
baselineTsserverLogs("configuredProjects", "when default configured project does not contain the file", session);
10001045
});
10011046

src/testRunner/unittests/tsserver/projectReferences.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
closeFilesForSession,
88
createHostWithSolutionBuild,
99
openFilesForSession,
10+
projectInfoForSession,
1011
protocolFileLocationFromSubstring,
1112
protocolLocationFromSubstring,
1213
TestSession,
@@ -23,7 +24,10 @@ function logDefaultProjectAndDefaultConfiguredProject(session: TestSession, file
2324
const defaultProject = session.getProjectService().tryGetDefaultProjectForFile(file.path as ts.server.NormalizedPath);
2425
const defaultConfiguredProject = info && session.getProjectService().findDefaultConfiguredProject(info);
2526
session.logger.info(`File: ${file.path}:\n\tgetDefaultProjectForFile:\n\t\t${defaultProject?.projectName}\n\tfindDefaultConfiguredProject:\n\t\t${defaultConfiguredProject?.projectName}`);
26-
return { defaultProject, defaultConfiguredProject };
27+
if (info) {
28+
const projectInfo = projectInfoForSession(session, file);
29+
return session.getProjectService().findProject(projectInfo.configFileName);
30+
}
2731
}
2832

2933
describe("unittests:: tsserver:: with projectReferences:: and tsbuild", () => {
@@ -1080,7 +1084,7 @@ export function bar() {}`,
10801084

10811085
function verifySolutionScenario(input: Setup) {
10821086
const { session, host } = setup(input);
1083-
const { defaultProject } = logDefaultProjectAndDefaultConfiguredProject(session, main);
1087+
const defaultProject = logDefaultProjectAndDefaultConfiguredProject(session, main);
10841088

10851089
// Verify errors
10861090
verifyGetErrRequest({ session, files: [main] });

0 commit comments

Comments
 (0)