Skip to content

Commit cf31e81

Browse files
committed
uncommenting and overloading
1 parent b19126e commit cf31e81

File tree

5 files changed

+191
-182
lines changed

5 files changed

+191
-182
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from 'vscode';
1515
// ** import { IPythonExecutionFactory } from '../../../common/process/types';
1616
import { TestDiscoveryOptions } from '../../common/types';
17+
import { IPythonExecutionFactory } from '../../../common/process/types';
1718

1819
export type TestRunInstanceOptions = TestRunOptions & {
1920
exclude?: readonly TestItem[];
@@ -179,21 +180,21 @@ export interface ITestServer {
179180
}
180181

181182
export interface ITestDiscoveryAdapter {
182-
// ** Uncomment second line and comment out first line to use the new discovery method.
183+
// ** first line old method signature, second line new method signature
183184
discoverTests(uri: Uri): Promise<DiscoveredTestPayload>;
184-
// discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>;
185+
discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload>;
185186
}
186187

187188
// interface for execution/runner adapter
188189
export interface ITestExecutionAdapter {
189-
// ** Uncomment second line and comment out first line to use the new execution method.
190+
// ** first line old method signature, second line new method signature
190191
runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload>;
191-
// runTests(
192-
// uri: Uri,
193-
// testIds: string[],
194-
// debugBool?: boolean,
195-
// executionFactory?: IPythonExecutionFactory,
196-
// ): Promise<ExecutionTestPayload>;
192+
runTests(
193+
uri: Uri,
194+
testIds: string[],
195+
debugBool?: boolean,
196+
executionFactory?: IPythonExecutionFactory,
197+
): Promise<ExecutionTestPayload>;
197198
}
198199

199200
// Same types as in pythonFiles/unittestadapter/utils.py

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
3838
}
3939

4040
// ** Old version of discover tests.
41-
discoverTests(uri: Uri): Promise<DiscoveredTestPayload> {
41+
discoverTests(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
42+
if (executionFactory !== undefined) {
43+
const settings = this.configSettings.getSettings(uri);
44+
const { pytestArgs } = settings.testing;
45+
traceVerbose(pytestArgs);
46+
return this.runPytestDiscovery(uri, executionFactory);
47+
}
48+
// if executionFactory is undefined, we are using the old version of discover tests.
4249
traceVerbose(uri);
4350
this.deferred = createDeferred<DiscoveredTestPayload>();
4451
return this.deferred.promise;
4552
}
46-
// Uncomment this version of the function discoverTests to use the new discovery method.
47-
// public async discoverTests(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
48-
// const settings = this.configSettings.getSettings(uri);
49-
// const { pytestArgs } = settings.testing;
50-
// traceVerbose(pytestArgs);
51-
52-
// return this.runPytestDiscovery(uri, executionFactory);
53-
// }
5453

5554
async runPytestDiscovery(uri: Uri, executionFactory: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
5655
const deferred = createDeferred<DiscoveredTestPayload>();

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

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
// Licensed under the MIT License.
33

44
import { Uri } from 'vscode';
5+
import path from 'path';
56
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
67
import { createDeferred, Deferred } from '../../../common/utils/async';
78
import { traceVerbose } from '../../../logging';
89
import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types';
10+
import {
11+
ExecutionFactoryCreateWithEnvironmentOptions,
12+
IPythonExecutionFactory,
13+
SpawnOptions,
14+
} from '../../../common/process/types';
15+
import { EXTENSION_ROOT_DIR } from '../../../constants';
916

1017
/**
1118
* Wrapper Class for pytest test execution. This is where we call `runTestCommand`?
@@ -33,69 +40,68 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
3340
}
3441

3542
// ** Old version of discover tests.
36-
async runTests(uri: Uri, testIds: string[], debugBool?: boolean): Promise<ExecutionTestPayload> {
43+
async runTests(
44+
uri: Uri,
45+
testIds: string[],
46+
debugBool?: boolean,
47+
executionFactory?: IPythonExecutionFactory,
48+
): Promise<ExecutionTestPayload> {
3749
traceVerbose(uri, testIds, debugBool);
50+
if (executionFactory !== undefined) {
51+
return this.runTestsNew(uri, testIds, debugBool, executionFactory);
52+
}
3853
// TODO:Remove this line after enabling runs
54+
// if executionFactory is undefined, we are using the old version of discover tests.
3955
this.outputChannel.appendLine('Running tests.');
4056
this.deferred = createDeferred<ExecutionTestPayload>();
4157
return this.deferred.promise;
4258
}
43-
}
44-
45-
// public async runTests(
46-
// uri: Uri,
47-
// testIds: string[],
48-
// debugBool?: boolean,
49-
// executionFactory?: IPythonExecutionFactory,
50-
// ): Promise<ExecutionTestPayload> {
51-
// const deferred = createDeferred<ExecutionTestPayload>();
52-
// const relativePathToPytest = 'pythonFiles';
53-
// const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
54-
// this.configSettings.isTestExecution();
55-
// const uuid = this.testServer.createUUID(uri.fsPath);
56-
// this.promiseMap.set(uuid, deferred);
57-
// const settings = this.configSettings.getSettings(uri);
58-
// const { pytestArgs } = settings.testing;
59-
60-
// const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? [];
61-
// const pythonPathCommand = [fullPluginPath, ...pythonPathParts].join(path.delimiter);
6259

63-
// const spawnOptions: SpawnOptions = {
64-
// cwd: uri.fsPath,
65-
// throwOnStdErr: true,
66-
// extraVariables: {
67-
// PYTHONPATH: pythonPathCommand,
68-
// TEST_UUID: uuid.toString(),
69-
// TEST_PORT: this.testServer.getPort().toString(),
70-
// },
71-
// outputChannel: this.outputChannel,
72-
// };
60+
private async runTestsNew(
61+
uri: Uri,
62+
testIds: string[],
63+
debugBool?: boolean,
64+
executionFactory?: IPythonExecutionFactory,
65+
): Promise<ExecutionTestPayload> {
66+
const deferred = createDeferred<ExecutionTestPayload>();
67+
const relativePathToPytest = 'pythonFiles';
68+
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
69+
this.configSettings.isTestExecution();
70+
const uuid = this.testServer.createUUID(uri.fsPath);
71+
this.promiseMap.set(uuid, deferred);
72+
const settings = this.configSettings.getSettings(uri);
73+
const { pytestArgs } = settings.testing;
7374

74-
// // Create the Python environment in which to execute the command.
75-
// const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
76-
// allowEnvironmentFetchExceptions: false,
77-
// resource: uri,
78-
// };
79-
// // need to check what will happen in the exec service is NOT defined and is null
80-
// const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
75+
const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? [];
76+
const pythonPathCommand = [fullPluginPath, ...pythonPathParts].join(path.delimiter);
8177

82-
// const testIdsString = testIds.join(' ');
83-
// console.debug('what to do with debug bool?', debugBool);
84-
// try {
85-
// execService?.exec(['-m', 'pytest', '-p', 'vscode_pytest', testIdsString].concat(pytestArgs), spawnOptions);
86-
// } catch (ex) {
87-
// console.error(ex);
88-
// }
78+
const spawnOptions: SpawnOptions = {
79+
cwd: uri.fsPath,
80+
throwOnStdErr: true,
81+
extraVariables: {
82+
PYTHONPATH: pythonPathCommand,
83+
TEST_UUID: uuid.toString(),
84+
TEST_PORT: this.testServer.getPort().toString(),
85+
},
86+
outputChannel: this.outputChannel,
87+
};
8988

90-
// return deferred.promise;
91-
// }
92-
// }
89+
// Create the Python environment in which to execute the command.
90+
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
91+
allowEnvironmentFetchExceptions: false,
92+
resource: uri,
93+
};
94+
// need to check what will happen in the exec service is NOT defined and is null
95+
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
9396

94-
// function buildExecutionCommand(args: string[]): TestExecutionCommand {
95-
// const executionScript = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py');
97+
const testIdsString = testIds.join(' ');
98+
console.debug('what to do with debug bool?', debugBool);
99+
try {
100+
execService?.exec(['-m', 'pytest', '-p', 'vscode_pytest', testIdsString].concat(pytestArgs), spawnOptions);
101+
} catch (ex) {
102+
console.error(ex);
103+
}
96104

97-
// return {
98-
// script: executionScript,
99-
// args: ['--udiscovery', ...args],
100-
// };
101-
// }
105+
return deferred.promise;
106+
}
107+
}

src/client/testing/testController/workspaceTestAdapter.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { splitLines } from '../../common/stringUtils';
1818
import { createDeferred, Deferred } from '../../common/utils/async';
1919
import { Testing } from '../../common/utils/localize';
20-
import { traceError } from '../../logging';
20+
import { traceError, traceVerbose } from '../../logging';
2121
import { sendTelemetryEvent } from '../../telemetry';
2222
import { EventName } from '../../telemetry/constants';
2323
import { TestProvider } from '../types';
@@ -36,6 +36,7 @@ import {
3636
ITestExecutionAdapter,
3737
} from './common/types';
3838
import { fixLogLines } from './common/utils';
39+
import { IPythonExecutionFactory } from '../../common/process/types';
3940

4041
/**
4142
* This class exposes a test-provider-agnostic way of discovering tests.
@@ -68,13 +69,13 @@ export class WorkspaceTestAdapter {
6869
this.vsIdToRunId = new Map<string, string>();
6970
}
7071

71-
// ** add executionFactory?: IPythonExecutionFactory, to the parameters
7272
public async executeTests(
7373
testController: TestController,
7474
runInstance: TestRun,
7575
includes: TestItem[],
7676
token?: CancellationToken,
7777
debugBool?: boolean,
78+
executionFactory?: IPythonExecutionFactory,
7879
): Promise<void> {
7980
if (this.executing) {
8081
return this.executing.promise;
@@ -101,18 +102,18 @@ export class WorkspaceTestAdapter {
101102
}
102103
});
103104

104-
// ** First line is old way, section with if statement below is new way.
105-
rawTestExecData = await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool);
106-
// if (executionFactory !== undefined) {
107-
// rawTestExecData = await this.executionAdapter.runTests(
108-
// this.workspaceUri,
109-
// testCaseIds,
110-
// debugBool,
111-
// executionFactory,
112-
// );
113-
// } else {
114-
// traceVerbose('executionFactory is undefined');
115-
// }
105+
// ** execution factory only defined for new rewrite way
106+
if (executionFactory !== undefined) {
107+
rawTestExecData = await this.executionAdapter.runTests(
108+
this.workspaceUri,
109+
testCaseIds,
110+
debugBool,
111+
executionFactory,
112+
);
113+
traceVerbose('executionFactory defined');
114+
} else {
115+
rawTestExecData = await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool);
116+
}
116117
deferred.resolve();
117118
} catch (ex) {
118119
// handle token and telemetry here
@@ -209,12 +210,12 @@ export class WorkspaceTestAdapter {
209210
return Promise.resolve();
210211
}
211212

212-
// add `executionFactory?: IPythonExecutionFactory,` to the function for new pytest method
213213
public async discoverTests(
214214
testController: TestController,
215215
token?: CancellationToken,
216216
isMultiroot?: boolean,
217217
workspaceFilePath?: string,
218+
executionFactory?: IPythonExecutionFactory,
218219
): Promise<void> {
219220
sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider });
220221

@@ -230,13 +231,12 @@ export class WorkspaceTestAdapter {
230231

231232
let rawTestData;
232233
try {
233-
// ** First line is old way, section with if statement below is new way.
234-
rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri);
235-
// if (executionFactory !== undefined) {
236-
// rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory);
237-
// } else {
238-
// traceVerbose('executionFactory is undefined');
239-
// }
234+
// ** execution factory only defined for new rewrite way
235+
if (executionFactory !== undefined) {
236+
rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory);
237+
} else {
238+
rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri);
239+
}
240240
deferred.resolve();
241241
} catch (ex) {
242242
sendTelemetryEvent(EventName.UNITTEST_DISCOVERY_DONE, undefined, { tool: this.testProvider, failed: true });

0 commit comments

Comments
 (0)