Skip to content

Inactive pytest run command #20653

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

Merged
merged 5 commits into from
Feb 14, 2023
Merged
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
10 changes: 9 additions & 1 deletion src/client/testing/testController/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Uri,
WorkspaceFolder,
} from 'vscode';
// ** import { IPythonExecutionFactory } from '../../../common/process/types';
import { TestDiscoveryOptions } from '../../common/types';

export type TestRunInstanceOptions = TestRunOptions & {
Expand Down Expand Up @@ -179,12 +180,19 @@ export interface ITestServer {
export interface ITestDiscoveryAdapter {
// ** Uncomment second line and comment out first line to use the new discovery method.
discoverTests(uri: Uri): Promise<DiscoveredTestPayload>;
// discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>
// discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>;
}

// interface for execution/runner adapter
export interface ITestExecutionAdapter {
// ** Uncomment second line and comment out first line to use the new execution method.
runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload>;
// runTests(
// uri: Uri,
// testIds: string[],
// debugBool?: boolean,
// executionFactory?: IPythonExecutionFactory,
// ): Promise<ExecutionTestPayload>;
}

// Same types as in pythonFiles/unittestadapter/utils.py
Expand Down
17 changes: 15 additions & 2 deletions src/client/testing/testController/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
executionAdapter = new UnittestTestExecutionAdapter(this.pythonTestServer, this.configSettings);
testProvider = UNITTEST_PROVIDER;
} else {
discoveryAdapter = new PytestTestDiscoveryAdapter(this.pythonTestServer, { ...this.configSettings });
discoveryAdapter = new PytestTestDiscoveryAdapter(this.pythonTestServer, this.configSettings);
executionAdapter = new PytestTestExecutionAdapter(this.pythonTestServer, this.configSettings);
testProvider = PYTEST_PROVIDER;
}
Expand Down Expand Up @@ -372,6 +372,20 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
tool: 'pytest',
debugging: request.profile?.kind === TestRunProfileKind.Debug,
});
// ** new execution runner/adapter
// const testAdapter =
// this.testAdapters.get(workspace.uri) ||
// (this.testAdapters.values().next().value as WorkspaceTestAdapter);
// return testAdapter.executeTests(
// this.testController,
// runInstance,
// testItems,
// token,
// request.profile?.kind === TestRunProfileKind.Debug,
// this.pythonExecFactory,
// );

// below is old way of running pytest execution
return this.pytest.runTests(
{
includes: testItems,
Expand Down Expand Up @@ -402,7 +416,6 @@ export class PythonTestController implements ITestController, IExtensionSingleAc
// );

// below is old way of running unittest execution

return this.unittest.runTests(
{
includes: testItems,
Expand Down
100 changes: 62 additions & 38 deletions src/client/testing/testController/pytest/pytestExecutionAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as path from 'path';
import { Uri } from 'vscode';
import { IConfigurationService } from '../../../common/types';
import { createDeferred, Deferred } from '../../../common/utils/async';
import { EXTENSION_ROOT_DIR } from '../../../constants';
import {
DataReceivedEvent,
ExecutionTestPayload,
ITestExecutionAdapter,
ITestServer,
TestCommandOptions,
TestExecutionCommand,
} from '../common/types';
import { traceVerbose } from '../../../logging';
import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types';

/**
* Wrapper Class for unittest test execution. This is where we call `runTestCommand`?
* Wrapper Class for pytest test execution. This is where we call `runTestCommand`?
*/

export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
Expand All @@ -37,37 +29,69 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
}
}

public async runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload> {
if (!this.deferred) {
const settings = this.configSettings.getSettings(uri);
const { unittestArgs } = settings.testing;
// ** Old version of discover tests.
async runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload> {
traceVerbose(uri, testIds, debugBool);
this.deferred = createDeferred<ExecutionTestPayload>();
return this.deferred.promise;
}

const command = buildExecutionCommand(unittestArgs);
this.cwd = uri.fsPath;
// public async runTests(
// uri: Uri,
// testIds: string[],
// debugBool?: boolean,
// executionFactory?: IPythonExecutionFactory,
// ): Promise<ExecutionTestPayload> {
// if (!this.deferred) {
// this.deferred = createDeferred<ExecutionTestPayload>();
// const relativePathToPytest = 'pythonFiles';
// const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
// this.configSettings.isTestExecution();
// const uuid = this.testServer.createUUID(uri.fsPath);
// const settings = this.configSettings.getSettings(uri);
// const { pytestArgs } = settings.testing;

const options: TestCommandOptions = {
workspaceFolder: uri,
command,
cwd: this.cwd,
debugBool,
testIds,
};
// const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? [];
// const pythonPathCommand = [fullPluginPath, ...pythonPathParts].join(path.delimiter);

this.deferred = createDeferred<ExecutionTestPayload>();
// const spawnOptions: SpawnOptions = {
// cwd: uri.fsPath,
// throwOnStdErr: true,
// extraVariables: {
// PYTHONPATH: pythonPathCommand,
// TEST_UUID: uuid.toString(),
// TEST_PORT: this.testServer.getPort().toString(),
// },
// };

// send test command to server
// server fire onDataReceived event once it gets response
this.testServer.sendCommand(options);
}
return this.deferred.promise;
}
}
// // Create the Python environment in which to execute the command.
// const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
// allowEnvironmentFetchExceptions: false,
// resource: uri,
// };
// // need to check what will happen in the exec service is NOT defined and is null
// const execService = await executionFactory?.createActivatedEnvironment(creationOptions);

// const testIdsString = testIds.join(' ');
// console.debug('what to do with debug bool?', debugBool);
// try {
// execService?.exec(
// ['-m', 'pytest', '-p', 'vscode_pytest', testIdsString].concat(pytestArgs),
// spawnOptions,
// );
// } catch (ex) {
// console.error(ex);
// }
// }
// return this.deferred.promise;
// }
// }

function buildExecutionCommand(args: string[]): TestExecutionCommand {
const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py');
// function buildExecutionCommand(args: string[]): TestExecutionCommand {
// const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py');

return {
script: executionScript,
args: ['--udiscovery', ...args],
};
// return {
// script: executionScript,
// args: ['--udiscovery', ...args],
// };
}
13 changes: 12 additions & 1 deletion src/client/testing/testController/workspaceTestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class WorkspaceTestAdapter {
this.vsIdToRunId = new Map<string, string>();
}

// ** add executionFactory?: IPythonExecutionFactory, to the parameters
public async executeTests(
testController: TestController,
runInstance: TestRun,
Expand Down Expand Up @@ -100,8 +101,18 @@ export class WorkspaceTestAdapter {
}
});

// need to get the testItems runIds so that we can pass in here.
// ** First line is old way, section with if statement below is new way.
rawTestExecData = await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool);
// if (executionFactory !== undefined) {
// rawTestExecData = await this.executionAdapter.runTests(
// this.workspaceUri,
// testCaseIds,
// debugBool,
// executionFactory,
// );
// } else {
// traceVerbose('executionFactory is undefined');
// }
deferred.resolve();
} catch (ex) {
// handle token and telemetry here
Expand Down