Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pytest to use correct interpreter from getActiveInterpreter #24233

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/client/common/process/pythonExecutionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/client/testing/testController/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -215,7 +216,11 @@ export interface ITestResultResolver {
export interface ITestDiscoveryAdapter {
// ** first line old method signature, second line new method signature
discoverTests(uri: Uri): Promise<DiscoveredTestPayload>;
discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>;
discoverTests(
uri: Uri,
executionFactory: IPythonExecutionFactory,
interpreter?: PythonEnvironment,
): Promise<DiscoveredTestPayload>;
}

// interface for execution/runner adapter
Expand Down
2 changes: 2 additions & 0 deletions src/client/testing/testController/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand All @@ -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.');
Expand Down
12 changes: 10 additions & 2 deletions src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,15 +37,19 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
private readonly envVarsService?: IEnvironmentVariablesProvider,
) {}

async discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
async discoverTests(
uri: Uri,
executionFactory?: IPythonExecutionFactory,
interpreter?: PythonEnvironment,
): Promise<DiscoveredTestPayload> {
const deferredTillEOT: Deferred<void> = createDeferred<void>();

const { name, dispose } = await startDiscoveryNamedPipe((data: DiscoveredTestPayload | EOTTestPayload) => {
this.resultResolver?.resolveDiscovery(data, deferredTillEOT);
});

try {
await this.runPytestDiscovery(uri, name, deferredTillEOT, executionFactory);
await this.runPytestDiscovery(uri, name, deferredTillEOT, executionFactory, interpreter);
} finally {
await deferredTillEOT.promise;
traceVerbose('deferredTill EOT resolved');
Expand All @@ -60,6 +65,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
discoveryPipeName: string,
deferredTillEOT: Deferred<void>,
executionFactory?: IPythonExecutionFactory,
interpreter?: PythonEnvironment,
): Promise<void> {
const relativePathToPytest = 'python_files';
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ 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(
public configSettings: IConfigurationService,
private readonly outputChannel: ITestOutputChannel,
private readonly resultResolver?: ITestResultResolver,
private readonly envVarsService?: IEnvironmentVariablesProvider,
private readonly interpreterService?: IInterpreterService,
) {}

async runTests(
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/client/testing/testController/workspaceTestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -115,6 +116,7 @@ export class WorkspaceTestAdapter {
testController: TestController,
token?: CancellationToken,
executionFactory?: IPythonExecutionFactory,
interpreter?: PythonEnvironment,
): Promise<void> {
sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider });

Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion src/test/testing/common/testingAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ suite('End to End Tests: test adapters', () => {
pythonExecFactory = serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory);
testController = serviceContainer.get<TestController>(ITestController);
envVarsService = serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider);

// create objects that were not injected

testOutputChannel = createTypeMoq<ITestOutputChannel>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ suite('pytest test discovery adapter', () => {
execService = typeMoq.Mock.ofType<IPythonExecutionService>();
execService.setup((p) => ((p as unknown) as any).then).returns(() => undefined);
outputChannel = typeMoq.Mock.ofType<ITestOutputChannel>();

const output = new Observable<Output<string>>(() => {
/* no op */
});
Expand Down