|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { CancellationToken, TestController, Uri, MarkdownString } from 'vscode'; |
| 5 | +import * as util from 'util'; |
| 6 | +import { DiscoveredTestPayload } from './types'; |
| 7 | +import { TestProvider } from '../../types'; |
| 8 | +import { traceError } from '../../../logging'; |
| 9 | +import { Testing } from '../../../common/utils/localize'; |
| 10 | +import { createErrorTestItem } from './testItemUtilities'; |
| 11 | +import { buildErrorNodeOptions, populateTestTree } from './utils'; |
| 12 | +import { TestItemIndex } from './testItemIndex'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Stateless handler for processing discovery payloads and building/updating the TestItem tree. |
| 16 | + * This handler is shared across all workspaces and contains no instance state. |
| 17 | + */ |
| 18 | +export class TestDiscoveryHandler { |
| 19 | + /** |
| 20 | + * Process discovery payload and update test tree |
| 21 | + * Pure function - no instance state used |
| 22 | + */ |
| 23 | + public processDiscovery( |
| 24 | + payload: DiscoveredTestPayload, |
| 25 | + testController: TestController, |
| 26 | + testItemIndex: TestItemIndex, |
| 27 | + workspaceUri: Uri, |
| 28 | + testProvider: TestProvider, |
| 29 | + token?: CancellationToken, |
| 30 | + ): void { |
| 31 | + if (!payload) { |
| 32 | + // No test data is available |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const workspacePath = workspaceUri.fsPath; |
| 37 | + const rawTestData = payload as DiscoveredTestPayload; |
| 38 | + |
| 39 | + // Check if there were any errors in the discovery process. |
| 40 | + if (rawTestData.status === 'error') { |
| 41 | + this.createErrorNode(testController, workspaceUri, rawTestData.error, testProvider); |
| 42 | + } else { |
| 43 | + // remove error node only if no errors exist. |
| 44 | + testController.items.delete(`DiscoveryError:${workspacePath}`); |
| 45 | + } |
| 46 | + |
| 47 | + if (rawTestData.tests || rawTestData.tests === null) { |
| 48 | + // if any tests exist, they should be populated in the test tree, regardless of whether there were errors or not. |
| 49 | + // parse and insert test data. |
| 50 | + |
| 51 | + // Clear existing mappings before rebuilding test tree |
| 52 | + testItemIndex.clear(); |
| 53 | + |
| 54 | + // If the test root for this folder exists: Workspace refresh, update its children. |
| 55 | + // Otherwise, it is a freshly discovered workspace, and we need to create a new test root and populate the test tree. |
| 56 | + // Note: populateTestTree will call testItemIndex.registerTestItem() for each discovered test |
| 57 | + populateTestTree( |
| 58 | + testController, |
| 59 | + rawTestData.tests, |
| 60 | + undefined, |
| 61 | + { |
| 62 | + runIdToTestItem: testItemIndex.runIdToTestItemMap, |
| 63 | + runIdToVSid: testItemIndex.runIdToVSidMap, |
| 64 | + vsIdToRunId: testItemIndex.vsIdToRunIdMap, |
| 65 | + } as any, |
| 66 | + token, |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create an error node for discovery failures |
| 73 | + */ |
| 74 | + public createErrorNode( |
| 75 | + testController: TestController, |
| 76 | + workspaceUri: Uri, |
| 77 | + error: string[] | undefined, |
| 78 | + testProvider: TestProvider, |
| 79 | + ): void { |
| 80 | + const workspacePath = workspaceUri.fsPath; |
| 81 | + const testingErrorConst = |
| 82 | + testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery; |
| 83 | + |
| 84 | + traceError(testingErrorConst, 'for workspace: ', workspacePath, '\r\n', error?.join('\r\n\r\n') ?? ''); |
| 85 | + |
| 86 | + let errorNode = testController.items.get(`DiscoveryError:${workspacePath}`); |
| 87 | + const message = util.format( |
| 88 | + `${testingErrorConst} ${Testing.seePythonOutput}\r\n`, |
| 89 | + error?.join('\r\n\r\n') ?? '', |
| 90 | + ); |
| 91 | + |
| 92 | + if (errorNode === undefined) { |
| 93 | + const options = buildErrorNodeOptions(workspaceUri, message, testProvider); |
| 94 | + errorNode = createErrorTestItem(testController, options); |
| 95 | + testController.items.add(errorNode); |
| 96 | + } |
| 97 | + |
| 98 | + const errorNodeLabel: MarkdownString = new MarkdownString( |
| 99 | + `[Show output](command:python.viewOutput) to view error logs`, |
| 100 | + ); |
| 101 | + errorNodeLabel.isTrusted = true; |
| 102 | + errorNode.error = errorNodeLabel; |
| 103 | + } |
| 104 | +} |
0 commit comments