Skip to content

Commit 5002965

Browse files
committed
avoid crashes in test controller
1 parent 70989e3 commit 5002965

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed

src/testController.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ export function configureTestController(
8484
const testFileUris = new WeakMap<vscode.TestItem, vscode.Uri>();
8585

8686
function getOrCreateWorkspaceFolderTestItem(uri: vscode.Uri) {
87-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
88-
let workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)!;
87+
let workspaceFolder = vscode.workspace.getWorkspaceFolder(uri);
88+
if (!workspaceFolder) {
89+
return undefined;
90+
}
8991
workspaceFolder =
9092
workspaceTracker.getOuterMostWorkspaceFolder(workspaceFolder);
9193

@@ -125,6 +127,9 @@ export function configureTestController(
125127
fileTestItem.range = new vscode.Range(0, 0, 0, 0);
126128

127129
const workspaceFolderTestItem = getOrCreateWorkspaceFolderTestItem(uri);
130+
if (!workspaceFolderTestItem) {
131+
return undefined;
132+
}
128133
workspaceFolderTestItem.children.add(fileTestItem);
129134

130135
testData.set(fileTestItem, ItemType.File);
@@ -170,18 +175,18 @@ export function configureTestController(
170175
}
171176

172177
async function parseTestsInFileContents(
173-
file: vscode.TestItem,
178+
file: vscode.TestItem | undefined,
174179
): Promise<void> {
180+
if (!file) {
181+
return;
182+
}
175183
if (!file.uri?.toString().endsWith(".exs")) {
176184
return;
177185
}
178186
// If a document is open, VS Code already knows its contents. If this is being
179187
// called from the resolveHandler when a document isn't open, we'll need to
180188
// read them from disk ourselves.
181-
const clientPromise = languageClientManager.getClientPromiseByUri(
182-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
183-
file.uri!,
184-
);
189+
const clientPromise = languageClientManager.getClientPromiseByUri(file.uri);
185190

186191
if (!clientPromise) {
187192
console.error(
@@ -228,8 +233,7 @@ export function configureTestController(
228233
const moduleTestItem = controller.createTestItem(
229234
moduleEntry.module,
230235
moduleEntry.module,
231-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
232-
file.uri!,
236+
file.uri,
233237
);
234238
moduleTestItem.range = new vscode.Range(
235239
moduleEntry.line,
@@ -245,8 +249,7 @@ export function configureTestController(
245249
const describeTestItem = controller.createTestItem(
246250
describeEntry.describe,
247251
describeEntry.describe,
248-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
249-
file.uri!,
252+
file.uri,
250253
);
251254
describeTestItem.range = new vscode.Range(
252255
describeEntry.line,
@@ -324,8 +327,7 @@ export function configureTestController(
324327
testItem.tags = testEntry.tags.map(
325328
(tag: string) => new vscode.TestTag(tag),
326329
);
327-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
328-
testFileUris.set(testItem, file.uri!);
330+
testFileUris.set(testItem, file.uri);
329331

330332
doctestGroupItem.children.add(testItem);
331333
}
@@ -489,8 +491,11 @@ export function configureTestController(
489491
// The `TestMessage` can contain extra information, like a failing location or
490492
// a diff output. But here we'll just give it a textual message.
491493
while (queue.length > 0 && !token.isCancellationRequested) {
492-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
493-
const test = queue.pop()!;
494+
const test = queue.pop();
495+
496+
if (!test || !test.uri) {
497+
continue;
498+
}
494499

495500
// Skip tests the user asked to exclude
496501
if (request.exclude?.includes(test)) {
@@ -500,11 +505,15 @@ export function configureTestController(
500505
const includeChildren = false;
501506
let runArgs: RunTestArgs;
502507

503-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
504-
const projectDir = workspaceTracker.getProjectDirForUri(test.uri!)!;
508+
const projectDir = workspaceTracker.getProjectDirForUri(test.uri);
509+
if (!projectDir) {
510+
continue;
511+
}
505512
const relativePath = test.uri?.fsPath.slice(projectDir.length + 1);
506-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
507-
const workspaceFolder = vscode.workspace.getWorkspaceFolder(test.uri!)!;
513+
const workspaceFolder = vscode.workspace.getWorkspaceFolder(test.uri);
514+
if (!workspaceFolder) {
515+
continue;
516+
}
508517

509518
// Note that we don't need to manually
510519
// set the state of parent tests; they'll be set automatically.
@@ -618,17 +627,24 @@ export function configureTestController(
618627

619628
case ItemType.TestCase:
620629
if (test.description === "doctest") {
621-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
622-
const testFileUri = testFileUris.get(test)!;
630+
const testFileUri = testFileUris.get(test);
631+
if (!testFileUri) {
632+
continue;
633+
}
623634
const projectDir =
624-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
625-
workspaceTracker.getProjectDirForUri(testFileUri)!;
635+
workspaceTracker.getProjectDirForUri(testFileUri);
636+
if (!projectDir) {
637+
continue;
638+
}
626639
const relativePath = testFileUri.fsPath.slice(
627640
projectDir.length + 1,
628641
);
629642
const workspaceFolder =
630-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
631-
vscode.workspace.getWorkspaceFolder(testFileUri)!;
643+
vscode.workspace.getWorkspaceFolder(testFileUri);
644+
645+
if (!workspaceFolder) {
646+
continue;
647+
}
632648

633649
runArgs = {
634650
cwd: projectDir,
@@ -733,8 +749,10 @@ export function configureTestController(
733749
RUN_TEST_FROM_CODELENS,
734750
async (args: RunArgs) => {
735751
const fileTestItemUri = vscode.Uri.file(args.filePath);
736-
// biome-ignore lint/style/noNonNullAssertion: <explanation>
737-
const projectDir = workspaceTracker.getProjectDirForUri(fileTestItemUri)!;
752+
const projectDir = workspaceTracker.getProjectDirForUri(fileTestItemUri);
753+
if (!projectDir) {
754+
throw `Unable to find project dir for ${fileTestItemUri}`;
755+
}
738756
await parseTestsInFileContents(
739757
getOrCreateFile(fileTestItemUri, projectDir),
740758
);

0 commit comments

Comments
 (0)