Skip to content
Open
Show file tree
Hide file tree
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
59 changes: 57 additions & 2 deletions src/client/testing/testController/common/testItemUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,69 @@ export async function updateTestItemFromRawData(
item.busy = false;
}

export function getTestCaseNodes(testNode: TestItem, collection: TestItem[] = []): TestItem[] {
/**
* Checks if a test item or any of its ancestors is in the exclude set.
*/
export function isTestItemExcluded(item: TestItem, excludeSet: Set<TestItem> | undefined): boolean {
if (!excludeSet || excludeSet.size === 0) {
return false;
}
let current: TestItem | undefined = item;
while (current) {
if (excludeSet.has(current)) {
return true;
}
current = current.parent;
}
return false;
}

/**
* Expands an exclude set to include all descendants of excluded items.
* After expansion, checking if a node is excluded is O(1) - just check set membership.
*/
export function expandExcludeSet(excludeSet: Set<TestItem> | undefined): Set<TestItem> | undefined {
if (!excludeSet || excludeSet.size === 0) {
return excludeSet;
}
const expanded = new Set<TestItem>();
excludeSet.forEach((item) => {
addWithDescendants(item, expanded);
});
return expanded;
}

function addWithDescendants(item: TestItem, set: Set<TestItem>): void {
if (set.has(item)) {
return;
}
set.add(item);
item.children.forEach((child) => addWithDescendants(child, set));
}

export function getTestCaseNodes(
testNode: TestItem,
collection: TestItem[] = [],
visited?: Set<TestItem>,
excludeSet?: Set<TestItem>,
): TestItem[] {
if (visited?.has(testNode)) {
return collection;
}
visited?.add(testNode);

// Skip excluded nodes (excludeSet should be pre-expanded to include descendants)
if (excludeSet?.has(testNode)) {
return collection;
}

if (!testNode.canResolveChildren && testNode.tags.length > 0) {
collection.push(testNode);
}

testNode.children.forEach((c) => {
if (testNode.canResolveChildren) {
getTestCaseNodes(c, collection);
getTestCaseNodes(c, collection, visited, excludeSet);
} else {
collection.push(testNode);
}
Expand Down
7 changes: 5 additions & 2 deletions src/client/testing/testController/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { IEventNamePropertyMapping, sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { PYTEST_PROVIDER, UNITTEST_PROVIDER } from '../common/constants';
import { TestProvider } from '../types';
import { createErrorTestItem, DebugTestTag, getNodeByUri, RunTestTag } from './common/testItemUtilities';
import { createErrorTestItem, DebugTestTag, getNodeByUri, isTestItemExcluded, RunTestTag } from './common/testItemUtilities';
import { buildErrorNodeOptions } from './common/utils';
import {
ITestController,
Expand Down Expand Up @@ -507,12 +507,14 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
*/
private getTestItemsForWorkspace(workspace: WorkspaceFolder, request: TestRunRequest): TestItem[] {
const testItems: TestItem[] = [];
const excludeSet = request.exclude?.length ? new Set(request.exclude) : undefined;
// If the run request includes test items then collect only items that belong to
// `workspace`. If there are no items in the run request then just run the `workspace`
// root test node. Include will be `undefined` in the "run all" scenario.
// Exclusions are applied after inclusions per VS Code API contract.
(request.include ?? this.testController.items).forEach((i: TestItem) => {
const w = this.workspaceService.getWorkspaceFolder(i.uri);
if (w?.uri.fsPath === workspace.uri.fsPath) {
if (w?.uri.fsPath === workspace.uri.fsPath && !isTestItemExcluded(i, excludeSet)) {
testItems.push(i);
}
});
Expand Down Expand Up @@ -566,6 +568,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
request.profile?.kind,
this.debugLauncher,
await this.interpreterService.getActiveInterpreter(workspace.uri),
request.exclude,
);
}

Expand Down
21 changes: 12 additions & 9 deletions src/client/testing/testController/workspaceTestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { traceError } from '../../logging';
import { sendTelemetryEvent } from '../../telemetry';
import { EventName } from '../../telemetry/constants';
import { TestProvider } from '../types';
import { createErrorTestItem, getTestCaseNodes } from './common/testItemUtilities';
import { createErrorTestItem, expandExcludeSet, getTestCaseNodes } from './common/testItemUtilities';
import { ITestDiscoveryAdapter, ITestExecutionAdapter, ITestResultResolver } from './common/types';
import { IPythonExecutionFactory } from '../../common/process/types';
import { ITestDebugLauncher } from '../common/types';
Expand Down Expand Up @@ -47,6 +47,7 @@ export class WorkspaceTestAdapter {
profileKind?: boolean | TestRunProfileKind,
debugLauncher?: ITestDebugLauncher,
interpreter?: PythonEnvironment,
excludes?: readonly TestItem[],
): Promise<void> {
if (this.executing) {
traceError('Test execution already in progress, not starting a new one.');
Expand All @@ -57,22 +58,24 @@ export class WorkspaceTestAdapter {
this.executing = deferred;

const testCaseNodes: TestItem[] = [];
const testCaseIdsSet = new Set<string>();
const visitedNodes = new Set<TestItem>();
const rawExcludeSet = excludes?.length ? new Set(excludes) : undefined;
const excludeSet = expandExcludeSet(rawExcludeSet);
const testCaseIds: string[] = [];
try {
// first fetch all the individual test Items that we necessarily want
// Expand included items to leaf test nodes.
// getTestCaseNodes handles visited tracking and exclusion filtering.
includes.forEach((t) => {
const nodes = getTestCaseNodes(t);
testCaseNodes.push(...nodes);
getTestCaseNodes(t, testCaseNodes, visitedNodes, excludeSet);
});
// iterate through testItems nodes and fetch their unittest runID to pass in as argument
// Collect runIDs for the test nodes to execute.
testCaseNodes.forEach((node) => {
runInstance.started(node); // do the vscode ui test item start here before runtest
runInstance.started(node);
const runId = this.resultResolver.vsIdToRunId.get(node.id);
if (runId) {
testCaseIdsSet.add(runId);
testCaseIds.push(runId);
}
});
const testCaseIds = Array.from(testCaseIdsSet);
if (executionFactory === undefined) {
throw new Error('Execution factory is required for test execution');
}
Expand Down