diff --git a/src/client/common/process/pythonExecutionFactory.ts b/src/client/common/process/pythonExecutionFactory.ts index d8401a603d03..228e8fc1f9ef 100644 --- a/src/client/common/process/pythonExecutionFactory.ts +++ b/src/client/common/process/pythonExecutionFactory.ts @@ -115,6 +115,7 @@ export class PythonExecutionFactory implements IPythonExecutionFactory { pythonPath: options.interpreter ? options.interpreter.path : undefined, }); } + const pythonPath = options.interpreter ? options.interpreter.path : this.configService.getSettings(options.resource).pythonPath; diff --git a/src/client/testing/testController/common/types.ts b/src/client/testing/testController/common/types.ts index 0942d9d2588c..2e141461eed1 100644 --- a/src/client/testing/testController/common/types.ts +++ b/src/client/testing/testController/common/types.ts @@ -17,6 +17,7 @@ import { ITestDebugLauncher, TestDiscoveryOptions } from '../../common/types'; import { IPythonExecutionFactory } from '../../../common/process/types'; import { EnvironmentVariables } from '../../../common/variables/types'; import { Deferred } from '../../../common/utils/async'; +import { PythonEnvironment } from '../../../pythonEnvironments/info'; export type TestRunInstanceOptions = TestRunOptions & { exclude?: readonly TestItem[]; @@ -215,7 +216,11 @@ export interface ITestResultResolver { export interface ITestDiscoveryAdapter { // ** first line old method signature, second line new method signature discoverTests(uri: Uri): Promise; - discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise; + discoverTests( + uri: Uri, + executionFactory: IPythonExecutionFactory, + interpreter?: PythonEnvironment, + ): Promise; } // interface for execution/runner adapter diff --git a/src/client/testing/testController/controller.ts b/src/client/testing/testController/controller.ts index dd624078a534..2e6f44bca770 100644 --- a/src/client/testing/testController/controller.ts +++ b/src/client/testing/testController/controller.ts @@ -276,6 +276,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc this.testController, this.refreshCancellation.token, this.pythonExecFactory, + await this.interpreterService.getActiveInterpreter(workspace.uri), ); } else { traceError('Unable to find test adapter for workspace.'); @@ -297,6 +298,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc this.testController, this.refreshCancellation.token, this.pythonExecFactory, + await this.interpreterService.getActiveInterpreter(workspace.uri), ); } else { traceError('Unable to find test adapter for workspace.'); diff --git a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts index dd7bc9b21847..874a50b29b2c 100644 --- a/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts +++ b/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts @@ -24,6 +24,7 @@ import { hasSymlinkParent, } from '../common/utils'; import { IEnvironmentVariablesProvider } from '../../../common/variables/types'; +import { PythonEnvironment } from '../../../pythonEnvironments/info'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. #this seems incorrectly copied @@ -36,7 +37,11 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { private readonly envVarsService?: IEnvironmentVariablesProvider, ) {} - async discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise { + async discoverTests( + uri: Uri, + executionFactory?: IPythonExecutionFactory, + interpreter?: PythonEnvironment, + ): Promise { const deferredTillEOT: Deferred = createDeferred(); const { name, dispose } = await startDiscoveryNamedPipe((data: DiscoveredTestPayload | EOTTestPayload) => { @@ -44,7 +49,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { }); try { - await this.runPytestDiscovery(uri, name, deferredTillEOT, executionFactory); + await this.runPytestDiscovery(uri, name, deferredTillEOT, executionFactory, interpreter); } finally { await deferredTillEOT.promise; traceVerbose('deferredTill EOT resolved'); @@ -60,6 +65,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { discoveryPipeName: string, deferredTillEOT: Deferred, executionFactory?: IPythonExecutionFactory, + interpreter?: PythonEnvironment, ): Promise { const relativePathToPytest = 'python_files'; const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest); @@ -106,7 +112,9 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { allowEnvironmentFetchExceptions: false, resource: uri, + interpreter, }; + const execService = await executionFactory?.createActivatedEnvironment(creationOptions); // delete UUID following entire discovery finishing. const execArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs); diff --git a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index bfaaab9d6586..54344431dc8f 100644 --- a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -25,6 +25,7 @@ import { PYTEST_PROVIDER } from '../../common/constants'; import { EXTENSION_ROOT_DIR } from '../../../common/constants'; import * as utils from '../common/utils'; import { IEnvironmentVariablesProvider } from '../../../common/variables/types'; +import { IInterpreterService } from '../../../interpreter/contracts'; export class PytestTestExecutionAdapter implements ITestExecutionAdapter { constructor( @@ -32,6 +33,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { private readonly outputChannel: ITestOutputChannel, private readonly resultResolver?: ITestResultResolver, private readonly envVarsService?: IEnvironmentVariablesProvider, + private readonly interpreterService?: IInterpreterService, ) {} async runTests( @@ -131,10 +133,13 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { } const debugBool = profileKind && profileKind === TestRunProfileKind.Debug; + const interpreter = await this.interpreterService?.getActiveInterpreter(); + // Create the Python environment in which to execute the command. const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { allowEnvironmentFetchExceptions: false, resource: uri, + interpreter, }; // need to check what will happen in the exec service is NOT defined and is null const execService = await executionFactory?.createActivatedEnvironment(creationOptions); diff --git a/src/client/testing/testController/workspaceTestAdapter.ts b/src/client/testing/testController/workspaceTestAdapter.ts index a0e65cfb5061..6c86e35385f6 100644 --- a/src/client/testing/testController/workspaceTestAdapter.ts +++ b/src/client/testing/testController/workspaceTestAdapter.ts @@ -14,6 +14,7 @@ import { ITestDiscoveryAdapter, ITestExecutionAdapter, ITestResultResolver } fro import { IPythonExecutionFactory } from '../../common/process/types'; import { ITestDebugLauncher } from '../common/types'; import { buildErrorNodeOptions } from './common/utils'; +import { PythonEnvironment } from '../../pythonEnvironments/info'; /** * This class exposes a test-provider-agnostic way of discovering tests. @@ -115,6 +116,7 @@ export class WorkspaceTestAdapter { testController: TestController, token?: CancellationToken, executionFactory?: IPythonExecutionFactory, + interpreter?: PythonEnvironment, ): Promise { sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider }); @@ -130,7 +132,7 @@ export class WorkspaceTestAdapter { try { // ** execution factory only defined for new rewrite way if (executionFactory !== undefined) { - await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory); + await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory, interpreter); } else { await this.discoveryAdapter.discoverTests(this.workspaceUri); } diff --git a/src/test/testing/common/testingAdapter.test.ts b/src/test/testing/common/testingAdapter.test.ts index dcd45b2e56bc..d1731dff8d9a 100644 --- a/src/test/testing/common/testingAdapter.test.ts +++ b/src/test/testing/common/testingAdapter.test.ts @@ -109,7 +109,6 @@ suite('End to End Tests: test adapters', () => { pythonExecFactory = serviceContainer.get(IPythonExecutionFactory); testController = serviceContainer.get(ITestController); envVarsService = serviceContainer.get(IEnvironmentVariablesProvider); - // create objects that were not injected testOutputChannel = createTypeMoq(); diff --git a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts index 87b91f6ae2da..f365c3e7244a 100644 --- a/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts +++ b/src/test/testing/testController/pytest/pytestDiscoveryAdapter.unit.test.ts @@ -71,7 +71,6 @@ suite('pytest test discovery adapter', () => { execService = typeMoq.Mock.ofType(); execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined); outputChannel = typeMoq.Mock.ofType(); - const output = new Observable>(() => { /* no op */ });