From 5d563792a7f59084258c41dab6c26baa87ed1738 Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Mon, 4 Nov 2024 13:36:13 -0800 Subject: [PATCH] error msg for adapter unalignment issue (#24385) resolves https://github.com/microsoft/vscode-python/issues/23234 as it surfaces readable error --- .../testing/testController/controller.ts | 37 ++++++++++++++++++- .../testController/workspaceTestAdapter.ts | 9 +++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index f969760c45b6..fde51955c681 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -18,6 +18,7 @@ import { TextDocument, FileCoverageDetail, TestRun, + MarkdownString, } from 'vscode'; import { IExtensionSingleActivationService } from '../../activation/types'; import { ICommandManager, IWorkspaceService } from '../../common/application/types'; @@ -32,8 +33,8 @@ import { IEventNamePropertyMapping, sendTelemetryEvent } from '../../telemetry'; import { EventName } from '../../telemetry/constants'; import { PYTEST_PROVIDER, UNITTEST_PROVIDER } from '../common/constants'; import { TestProvider } from '../types'; -import { DebugTestTag, getNodeByUri, RunTestTag } from './common/testItemUtilities'; -import { pythonTestAdapterRewriteEnabled } from './common/utils'; +import { createErrorTestItem, DebugTestTag, getNodeByUri, RunTestTag } from './common/testItemUtilities'; +import { buildErrorNodeOptions, pythonTestAdapterRewriteEnabled } from './common/utils'; import { ITestController, ITestDiscoveryAdapter, @@ -275,6 +276,16 @@ export class PythonTestController implements ITestController, IExtensionSingleAc if (workspace && workspace.uri) { const testAdapter = this.testAdapters.get(workspace.uri); if (testAdapter) { + const testProviderInAdapter = testAdapter.getTestProvider(); + if (testProviderInAdapter !== 'pytest') { + traceError('Test provider in adapter is not pytest. Please reload window.'); + this.surfaceErrorNode( + workspace.uri, + 'Test provider types are not aligned, please reload your VS Code window.', + 'pytest', + ); + return Promise.resolve(); + } await testAdapter.discoverTests( this.testController, this.refreshCancellation.token, @@ -297,6 +308,16 @@ export class PythonTestController implements ITestController, IExtensionSingleAc if (workspace && workspace.uri) { const testAdapter = this.testAdapters.get(workspace.uri); if (testAdapter) { + const testProviderInAdapter = testAdapter.getTestProvider(); + if (testProviderInAdapter !== 'unittest') { + traceError('Test provider in adapter is not unittest. Please reload window.'); + this.surfaceErrorNode( + workspace.uri, + 'Test provider types are not aligned, please reload your VS Code window.', + 'unittest', + ); + return Promise.resolve(); + } await testAdapter.discoverTests( this.testController, this.refreshCancellation.token, @@ -598,4 +619,16 @@ export class PythonTestController implements ITestController, IExtensionSingleAc this.triggerTypes.push(trigger); } } + + private surfaceErrorNode(workspaceUri: Uri, message: string, testProvider: TestProvider): void { + let errorNode = this.testController.items.get(`DiscoveryError:${workspaceUri.fsPath}`); + if (errorNode === undefined) { + const options = buildErrorNodeOptions(workspaceUri, message, testProvider); + errorNode = createErrorTestItem(this.testController, options); + this.testController.items.add(errorNode); + } + const errorNodeLabel: MarkdownString = new MarkdownString(message); + errorNodeLabel.isTrusted = true; + errorNode.error = errorNodeLabel; + } } diff --git a/src/client/testing/testController/workspaceTestAdapter.ts b/src/client/testing/testController/workspaceTestAdapter.ts index 81641ee5125c..d8d6cb53d835 100644 --- a/src/client/testing/testController/workspaceTestAdapter.ts +++ b/src/client/testing/testController/workspaceTestAdapter.ts @@ -167,4 +167,13 @@ export class WorkspaceTestAdapter { sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: false }); return Promise.resolve(); } + + /** + * Retrieves the current test provider instance. + * + * @returns {TestProvider} The instance of the test provider. + */ + public getTestProvider(): TestProvider { + return this.testProvider; + } }