Skip to content

Commit

Permalink
Workspace & resolver tests (microsoft#21441)
Browse files Browse the repository at this point in the history
This PR
- moves populateTestTree to utils
- adds tests for execution adapters (pytest and unittest)
- resultResolver tests
- workspaceTestAdapater tests
  • Loading branch information
eleanorjboyd committed Jun 21, 2023
1 parent b84fb74 commit 932dc39
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/client/testing/testController/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,69 @@ export function populateTestTree(
function isTestItem(test: DiscoveredTestNode | DiscoveredTestItem): test is DiscoveredTestItem {
return test.type_ === 'test';
}

export function buildErrorNodeOptions(uri: Uri, message: string, testType: string): ErrorTestItemOptions {
const labelText = testType === 'pytest' ? 'Pytest Discovery Error' : 'Unittest Discovery Error';
return {
id: `DiscoveryError:${uri.fsPath}`,
label: `${labelText} [${path.basename(uri.fsPath)}]`,
error: message,
};
}

export function populateTestTree(
testController: TestController,
testTreeData: DiscoveredTestNode,
testRoot: TestItem | undefined,
resultResolver: ITestResultResolver,
token?: CancellationToken,
): void {
// If testRoot is undefined, use the info of the root item of testTreeData to create a test item, and append it to the test controller.
if (!testRoot) {
testRoot = testController.createTestItem(testTreeData.path, testTreeData.name, Uri.file(testTreeData.path));

testRoot.canResolveChildren = true;
testRoot.tags = [RunTestTag, DebugTestTag];

testController.items.add(testRoot);
}

// Recursively populate the tree with test data.
testTreeData.children.forEach((child) => {
if (!token?.isCancellationRequested) {
if (isTestItem(child)) {
const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path));
testItem.tags = [RunTestTag, DebugTestTag];

const range = new Range(
new Position(Number(child.lineno) - 1, 0),
new Position(Number(child.lineno), 0),
);
testItem.canResolveChildren = false;
testItem.range = range;
testItem.tags = [RunTestTag, DebugTestTag];

testRoot!.children.add(testItem);
// add to our map
resultResolver.runIdToTestItem.set(child.runID, testItem);
resultResolver.runIdToVSid.set(child.runID, child.id_);
resultResolver.vsIdToRunId.set(child.id_, child.runID);
} else {
let node = testController.items.get(child.path);

if (!node) {
node = testController.createTestItem(child.id_, child.name, Uri.file(child.path));

node.canResolveChildren = true;
node.tags = [RunTestTag, DebugTestTag];
testRoot!.children.add(node);
}
populateTestTree(testController, child, node, resultResolver, token);
}
}
});
}

function isTestItem(test: DiscoveredTestNode | DiscoveredTestItem): test is DiscoveredTestItem {
return test.type_ === 'test';
}

0 comments on commit 932dc39

Please sign in to comment.