Skip to content

Commit ff65066

Browse files
anthonykim1karthiknadig
authored andcommitted
Allow pytest to use correct interpreter from getActiveInterpreter (#24250)
Resolves: #24122 Related: #24190, #24127 I think the culprit was we were not passing in interpreter when we call createActivatedEnvironment.
1 parent 7513198 commit ff65066

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

src/client/testing/testController/common/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { ITestDebugLauncher, TestDiscoveryOptions } from '../../common/types';
1717
import { IPythonExecutionFactory } from '../../../common/process/types';
1818
import { EnvironmentVariables } from '../../../common/variables/types';
19+
import { PythonEnvironment } from '../../../pythonEnvironments/info';
1920

2021
export type TestRunInstanceOptions = TestRunOptions & {
2122
exclude?: readonly TestItem[];
@@ -206,7 +207,11 @@ export interface ITestResultResolver {
206207
export interface ITestDiscoveryAdapter {
207208
// ** first line old method signature, second line new method signature
208209
discoverTests(uri: Uri): Promise<DiscoveredTestPayload>;
209-
discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>;
210+
discoverTests(
211+
uri: Uri,
212+
executionFactory: IPythonExecutionFactory,
213+
interpreter?: PythonEnvironment,
214+
): Promise<DiscoveredTestPayload>;
210215
}
211216

212217
// interface for execution/runner adapter
@@ -220,6 +225,7 @@ export interface ITestExecutionAdapter {
220225
runInstance?: TestRun,
221226
executionFactory?: IPythonExecutionFactory,
222227
debugLauncher?: ITestDebugLauncher,
228+
interpreter?: PythonEnvironment,
223229
): Promise<ExecutionTestPayload>;
224230
}
225231

src/client/testing/testController/controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
276276
this.testController,
277277
this.refreshCancellation.token,
278278
this.pythonExecFactory,
279+
await this.interpreterService.getActiveInterpreter(workspace.uri),
279280
);
280281
} else {
281282
traceError('Unable to find test adapter for workspace.');
@@ -297,6 +298,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
297298
this.testController,
298299
this.refreshCancellation.token,
299300
this.pythonExecFactory,
301+
await this.interpreterService.getActiveInterpreter(workspace.uri),
300302
);
301303
} else {
302304
traceError('Unable to find test adapter for workspace.');
@@ -455,6 +457,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
455457
request.profile?.kind,
456458
this.pythonExecFactory,
457459
this.debugLauncher,
460+
await this.interpreterService.getActiveInterpreter(workspace.uri),
458461
);
459462
}
460463
return this.pytest.runTests(
@@ -483,6 +486,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
483486
request.profile?.kind,
484487
this.pythonExecFactory,
485488
this.debugLauncher,
489+
await this.interpreterService.getActiveInterpreter(workspace.uri),
486490
);
487491
}
488492
// below is old way of running unittest execution

src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
hasSymlinkParent,
2424
} from '../common/utils';
2525
import { IEnvironmentVariablesProvider } from '../../../common/variables/types';
26+
import { PythonEnvironment } from '../../../pythonEnvironments/info';
2627

2728
/**
2829
* Wrapper class for unittest test discovery. This is where we call `runTestCommand`. #this seems incorrectly copied
@@ -35,13 +36,17 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
3536
private readonly envVarsService?: IEnvironmentVariablesProvider,
3637
) {}
3738

38-
async discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
39+
async discoverTests(
40+
uri: Uri,
41+
executionFactory?: IPythonExecutionFactory,
42+
interpreter?: PythonEnvironment,
43+
): Promise<DiscoveredTestPayload> {
3944
const { name, dispose } = await startDiscoveryNamedPipe((data: DiscoveredTestPayload) => {
4045
this.resultResolver?.resolveDiscovery(data);
4146
});
4247

4348
try {
44-
await this.runPytestDiscovery(uri, name, executionFactory);
49+
await this.runPytestDiscovery(uri, name, executionFactory, interpreter);
4550
} finally {
4651
dispose();
4752
}
@@ -54,6 +59,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
5459
uri: Uri,
5560
discoveryPipeName: string,
5661
executionFactory?: IPythonExecutionFactory,
62+
interpreter?: PythonEnvironment,
5763
): Promise<void> {
5864
const relativePathToPytest = 'python_files';
5965
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
@@ -100,6 +106,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
100106
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
101107
allowEnvironmentFetchExceptions: false,
102108
resource: uri,
109+
interpreter,
103110
};
104111
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
105112
// delete UUID following entire discovery finishing.

src/client/testing/testController/pytest/pytestExecutionAdapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { PYTEST_PROVIDER } from '../../common/constants';
1919
import { EXTENSION_ROOT_DIR } from '../../../common/constants';
2020
import * as utils from '../common/utils';
2121
import { IEnvironmentVariablesProvider } from '../../../common/variables/types';
22+
import { PythonEnvironment } from '../../../pythonEnvironments/info';
2223

2324
export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
2425
constructor(
@@ -35,6 +36,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
3536
runInstance?: TestRun,
3637
executionFactory?: IPythonExecutionFactory,
3738
debugLauncher?: ITestDebugLauncher,
39+
interpreter?: PythonEnvironment,
3840
): Promise<ExecutionTestPayload> {
3941
const deferredTillServerClose: Deferred<void> = utils.createTestingDeferred();
4042

@@ -74,6 +76,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
7476
profileKind,
7577
executionFactory,
7678
debugLauncher,
79+
interpreter,
7780
);
7881
} finally {
7982
await deferredTillServerClose.promise;
@@ -98,6 +101,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
98101
profileKind?: TestRunProfileKind,
99102
executionFactory?: IPythonExecutionFactory,
100103
debugLauncher?: ITestDebugLauncher,
104+
interpreter?: PythonEnvironment,
101105
): Promise<ExecutionTestPayload> {
102106
const relativePathToPytest = 'python_files';
103107
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
@@ -122,6 +126,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
122126
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
123127
allowEnvironmentFetchExceptions: false,
124128
resource: uri,
129+
interpreter,
125130
};
126131
// need to check what will happen in the exec service is NOT defined and is null
127132
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);

src/client/testing/testController/workspaceTestAdapter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ITestDiscoveryAdapter, ITestExecutionAdapter, ITestResultResolver } fro
1414
import { IPythonExecutionFactory } from '../../common/process/types';
1515
import { ITestDebugLauncher } from '../common/types';
1616
import { buildErrorNodeOptions } from './common/utils';
17+
import { PythonEnvironment } from '../../pythonEnvironments/info';
1718

1819
/**
1920
* This class exposes a test-provider-agnostic way of discovering tests.
@@ -45,6 +46,7 @@ export class WorkspaceTestAdapter {
4546
profileKind?: boolean | TestRunProfileKind,
4647
executionFactory?: IPythonExecutionFactory,
4748
debugLauncher?: ITestDebugLauncher,
49+
interpreter?: PythonEnvironment,
4850
): Promise<void> {
4951
if (this.executing) {
5052
traceError('Test execution already in progress, not starting a new one.');
@@ -80,6 +82,7 @@ export class WorkspaceTestAdapter {
8082
runInstance,
8183
executionFactory,
8284
debugLauncher,
85+
interpreter,
8386
);
8487
} else {
8588
await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, profileKind);
@@ -115,6 +118,7 @@ export class WorkspaceTestAdapter {
115118
testController: TestController,
116119
token?: CancellationToken,
117120
executionFactory?: IPythonExecutionFactory,
121+
interpreter?: PythonEnvironment,
118122
): Promise<void> {
119123
sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider });
120124

@@ -130,7 +134,7 @@ export class WorkspaceTestAdapter {
130134
try {
131135
// ** execution factory only defined for new rewrite way
132136
if (executionFactory !== undefined) {
133-
await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory);
137+
await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory, interpreter);
134138
} else {
135139
await this.discoveryAdapter.discoverTests(this.workspaceUri);
136140
}

0 commit comments

Comments
 (0)