|
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
4 | 4 | import { Location, MarkdownString, TestItem } from 'vscode'; |
5 | | -import { IRunTestContext } from '../../java-test-runner.api'; |
| 5 | +import { dataCache, ITestItemData } from '../../controller/testItemDataCache'; |
| 6 | +import { IRunTestContext, TestLevel, TestResultState } from '../../java-test-runner.api'; |
6 | 7 | import { processStackTraceLine } from '../utils'; |
7 | 8 |
|
8 | 9 | export abstract class RunnerResultAnalyzer { |
| 10 | + // Track parent test item states to update them when all children complete |
| 11 | + protected parentStates: Map<TestItem, ParentItemState> = new Map(); |
| 12 | + |
9 | 13 | constructor(protected testContext: IRunTestContext) { } |
10 | 14 |
|
11 | 15 | public abstract analyzeData(data: string): void; |
@@ -36,4 +40,95 @@ export abstract class RunnerResultAnalyzer { |
36 | 40 | return stacktrace.includes(s); |
37 | 41 | }); |
38 | 42 | } |
| 43 | + |
| 44 | + /** |
| 45 | + * Initialize parent state tracking for a test item. |
| 46 | + * Counts how many method-level children are being tested. |
| 47 | + */ |
| 48 | + protected initializeParentState(item: TestItem, triggeredTestsMapping: Map<string, TestItem>): void { |
| 49 | + const parent: TestItem | undefined = item.parent; |
| 50 | + if (!parent) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const parentData: ITestItemData | undefined = dataCache.get(parent); |
| 55 | + if (!parentData || parentData.testLevel !== TestLevel.Class) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (!this.parentStates.has(parent)) { |
| 60 | + // Count how many method-level children are being tested (only count triggered tests) |
| 61 | + let childCount: number = 0; |
| 62 | + parent.children.forEach((child: TestItem) => { |
| 63 | + const childData: ITestItemData | undefined = dataCache.get(child); |
| 64 | + if (childData?.testLevel === TestLevel.Method && triggeredTestsMapping.has(child.id)) { |
| 65 | + childCount++; |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + this.parentStates.set(parent, { |
| 70 | + started: false, |
| 71 | + childrenTotal: childCount, |
| 72 | + childrenCompleted: 0, |
| 73 | + hasFailure: false, |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Update parent test item when a child test starts. |
| 80 | + * Marks the parent as "started" when the first child starts. |
| 81 | + */ |
| 82 | + protected updateParentOnChildStart(item: TestItem): void { |
| 83 | + const parent: TestItem | undefined = item.parent; |
| 84 | + if (!parent) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const parentState: ParentItemState | undefined = this.parentStates.get(parent); |
| 89 | + if (parentState && !parentState.started) { |
| 90 | + parentState.started = true; |
| 91 | + this.testContext.testRun.started(parent); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Update parent test item when a child test completes. |
| 97 | + * Marks the parent as "passed" or "failed" when all children complete. |
| 98 | + */ |
| 99 | + protected updateParentOnChildComplete(item: TestItem, childState: TestResultState): void { |
| 100 | + const parent: TestItem | undefined = item.parent; |
| 101 | + if (!parent) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const parentState: ParentItemState | undefined = this.parentStates.get(parent); |
| 106 | + if (!parentState) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Consider failed or errored tests as failures for the parent |
| 111 | + if (childState === TestResultState.Failed || |
| 112 | + childState === TestResultState.Errored) { |
| 113 | + parentState.hasFailure = true; |
| 114 | + } |
| 115 | + |
| 116 | + parentState.childrenCompleted++; |
| 117 | + |
| 118 | + // Check if all children have completed |
| 119 | + if (parentState.childrenCompleted >= parentState.childrenTotal && parentState.childrenTotal > 0) { |
| 120 | + if (parentState.hasFailure) { |
| 121 | + this.testContext.testRun.failed(parent, []); |
| 122 | + } else { |
| 123 | + this.testContext.testRun.passed(parent); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +interface ParentItemState { |
| 130 | + started: boolean; |
| 131 | + childrenTotal: number; |
| 132 | + childrenCompleted: number; |
| 133 | + hasFailure: boolean; |
39 | 134 | } |
0 commit comments