Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Build test items in hierarchy if it's supported #914

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/testController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export class TestController {
this.testCommands.delete(test);
});

const groupIdMap: { [key: string]: vscode.TestItem } = {};
let classTest: vscode.TestItem;

const uri = vscode.Uri.from({
scheme: "file",
path: response[0].command!.arguments![0],
Expand Down Expand Up @@ -121,16 +123,40 @@ export class TestController {
new vscode.Position(location.end_line, location.end_column),
);

// Add test methods as children to the test class so it appears nested in Test explorer
// and running the test class will run all of the test methods

if (testItem.tags.find((tag) => tag.id === "example")) {
testItem.tags = [...testItem.tags, this.debugTag];
classTest.children.add(testItem);
// If the test has a group_id, the server supports code lens hierarchy
if ("group_id" in res.data) {
// If it has an id, it's a group
if (res.data?.id) {
// Add group to the map
groupIdMap[res.data.id] = testItem;
testItem.canResolveChildren = true;

if (res.data.group_id) {
// Add nested group to its parent group
groupIdMap[res.data.group_id].children.add(testItem);
} else {
// Or add it to the top-level
this.testController.items.add(testItem);
}
// Otherwise, it's a test
} else {
// Add test to its parent group
groupIdMap[res.data.group_id].children.add(testItem);
testItem.tags = [...testItem.tags, this.debugTag];
}
// If the server doesn't support code lens hierarchy, all groups are top-level
} else {
classTest = testItem;
classTest.canResolveChildren = true;
this.testController.items.add(testItem);
// Add test methods as children to the test class so it appears nested in Test explorer
// and running the test class will run all of the test methods
// eslint-disable-next-line no-lonely-if
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel blending the 2 versions of implementation together makes things harder to understand and also harder to remove the fallback. So I want to separate them with simple if/else condition and simply remove the fallback when we're ready to do so.

if (testItem.tags.find((tag) => tag.id === "example")) {
testItem.tags = [...testItem.tags, this.debugTag];
classTest.children.add(testItem);
} else {
classTest = testItem;
classTest.canResolveChildren = true;
this.testController.items.add(testItem);
}
}
});
}
Expand Down