Skip to content

Commit e60b51c

Browse files
author
Kartik Raj
authored
Do not trigger test discovery if an invalid interpreter is selected for the workspace (#19470)
1 parent 566f9b7 commit e60b51c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/client/testing/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ export class UnitTestManagementService implements IExtensionActivationService {
146146
if (!wkspace) {
147147
return;
148148
}
149+
const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
150+
const commandManager = this.serviceContainer.get<ICommandManager>(ICommandManager);
151+
if (!(await interpreterService.getActiveInterpreter(wkspace))) {
152+
commandManager.executeCommand('python.triggerEnvSelection', wkspace);
153+
return;
154+
}
149155
const configurationService = this.serviceContainer.get<ITestConfigurationService>(ITestConfigurationService);
150156
await configurationService.promptToEnableAndConfigureTestFramework(wkspace!);
151157
}

src/client/testing/testController/controller.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717
EventEmitter,
1818
} from 'vscode';
1919
import { IExtensionSingleActivationService } from '../../activation/types';
20-
import { IWorkspaceService } from '../../common/application/types';
20+
import { ICommandManager, IWorkspaceService } from '../../common/application/types';
2121
import * as constants from '../../common/constants';
2222
import { IPythonExecutionFactory } from '../../common/process/types';
2323
import { IConfigurationService, IDisposableRegistry, Resource } from '../../common/types';
2424
import { DelayedTrigger, IDelayedTrigger } from '../../common/utils/delayTrigger';
25-
import { traceVerbose } from '../../logging';
25+
import { noop } from '../../common/utils/misc';
26+
import { IInterpreterService } from '../../interpreter/contracts';
27+
import { traceError, traceVerbose } from '../../logging';
2628
import { IEventNamePropertyMapping, sendTelemetryEvent } from '../../telemetry';
2729
import { EventName } from '../../telemetry/constants';
2830
import { PYTEST_PROVIDER, UNITTEST_PROVIDER } from '../common/constants';
@@ -77,6 +79,8 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
7779
@inject(ITestFrameworkController) @named(UNITTEST_PROVIDER) private readonly unittest: ITestFrameworkController,
7880
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
7981
@inject(IPythonExecutionFactory) private readonly pythonExecFactory: IPythonExecutionFactory,
82+
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
83+
@inject(ICommandManager) private readonly commandManager: ICommandManager,
8084
) {
8185
this.refreshCancellation = new CancellationTokenSource();
8286

@@ -248,7 +252,17 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
248252
} else {
249253
traceVerbose('Testing: Refreshing all test data');
250254
const workspaces: readonly WorkspaceFolder[] = this.workspaceService.workspaceFolders || [];
251-
await Promise.all(workspaces.map((workspace) => this.refreshTestDataInternal(workspace.uri)));
255+
await Promise.all(
256+
workspaces.map(async (workspace) => {
257+
if (!(await this.interpreterService.getActiveInterpreter(workspace.uri))) {
258+
this.commandManager
259+
.executeCommand('python.triggerEnvSelection', workspace.uri)
260+
.then(noop, noop);
261+
return;
262+
}
263+
await this.refreshTestDataInternal(workspace.uri);
264+
}),
265+
);
252266
}
253267
this.refreshingCompletedEvent.fire();
254268
return Promise.resolve();
@@ -268,7 +282,15 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
268282
traceVerbose('Testing: Refreshing all test data');
269283
this.sendTriggerTelemetry('auto');
270284
const workspaces: readonly WorkspaceFolder[] = this.workspaceService.workspaceFolders || [];
271-
await Promise.all(workspaces.map((workspace) => this.refreshTestDataInternal(workspace.uri)));
285+
await Promise.all(
286+
workspaces.map(async (workspace) => {
287+
if (!(await this.interpreterService.getActiveInterpreter(workspace.uri))) {
288+
traceError('Cannot trigger test discovery as a valid interpreter is not selected');
289+
return;
290+
}
291+
await this.refreshTestDataInternal(workspace.uri);
292+
}),
293+
);
272294
}
273295
return Promise.resolve();
274296
}
@@ -296,7 +318,13 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
296318
const unconfiguredWorkspaces: WorkspaceFolder[] = [];
297319
try {
298320
await Promise.all(
299-
workspaces.map((workspace) => {
321+
workspaces.map(async (workspace) => {
322+
if (!(await this.interpreterService.getActiveInterpreter(workspace.uri))) {
323+
this.commandManager
324+
.executeCommand('python.triggerEnvSelection', workspace.uri)
325+
.then(noop, noop);
326+
return undefined;
327+
}
300328
const testItems: TestItem[] = [];
301329
// If the run request includes test items then collect only items that belong to
302330
// `workspace`. If there are no items in the run request then just run the `workspace`

0 commit comments

Comments
 (0)