Skip to content

Commit 85f2f8e

Browse files
committed
remove uuid stuff from other PR
1 parent 9d063d0 commit 85f2f8e

File tree

6 files changed

+48
-86
lines changed

6 files changed

+48
-86
lines changed

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

+4-23
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
import * as net from 'net';
55
import * as crypto from 'crypto';
6-
import { Disposable, Event, EventEmitter, TestRun } from 'vscode';
6+
import { Disposable, Event, EventEmitter } from 'vscode';
77
import * as path from 'path';
88
import {
99
ExecutionFactoryCreateWithEnvironmentOptions,
10-
ExecutionResult,
1110
IPythonExecutionFactory,
1211
SpawnOptions,
1312
} from '../../../common/process/types';
@@ -16,7 +15,6 @@ import { DataReceivedEvent, ITestServer, TestCommandOptions } from './types';
1615
import { ITestDebugLauncher, LaunchOptions } from '../../common/types';
1716
import { UNITTEST_PROVIDER } from '../../common/constants';
1817
import { jsonRPCHeaders, jsonRPCContent, JSONRPC_UUID_HEADER } from './utils';
19-
import { createDeferred } from '../../../common/utils/async';
2018

2119
export class PythonTestServer implements ITestServer, Disposable {
2220
private _onDataReceived: EventEmitter<DataReceivedEvent> = new EventEmitter<DataReceivedEvent>();
@@ -142,12 +140,7 @@ export class PythonTestServer implements ITestServer, Disposable {
142140
return this._onDataReceived.event;
143141
}
144142

145-
async sendCommand(
146-
options: TestCommandOptions,
147-
runTestIdPort?: string,
148-
runInstance?: TestRun,
149-
callback?: () => void,
150-
): Promise<void> {
143+
async sendCommand(options: TestCommandOptions, runTestIdPort?: string, callback?: () => void): Promise<void> {
151144
const { uuid } = options;
152145

153146
const pythonPathParts: string[] = process.env.PYTHONPATH?.split(path.delimiter) ?? [];
@@ -161,7 +154,7 @@ export class PythonTestServer implements ITestServer, Disposable {
161154
};
162155

163156
if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = runTestIdPort;
164-
const isRun = runTestIdPort !== undefined;
157+
const isRun = !options.testIds;
165158
// Create the Python environment in which to execute the command.
166159
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
167160
allowEnvironmentFetchExceptions: false,
@@ -202,19 +195,7 @@ export class PythonTestServer implements ITestServer, Disposable {
202195
// This means it is running discovery
203196
traceLog(`Discovering unittest tests with arguments: ${args}\r\n`);
204197
}
205-
const deferred = createDeferred<ExecutionResult<string>>();
206-
207-
const result = execService.execObservable(args, spawnOptions);
208-
209-
runInstance?.token.onCancellationRequested(() => {
210-
result?.proc?.kill();
211-
});
212-
result?.proc?.on('close', () => {
213-
traceLog('Exec server closed.', uuid);
214-
deferred.resolve({ stdout: '', stderr: '' });
215-
callback?.();
216-
});
217-
await deferred.promise;
198+
await execService.exec(args, spawnOptions);
218199
}
219200
} catch (ex) {
220201
this.uuids = this.uuids.filter((u) => u !== uuid);

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,7 @@ export interface ITestServer {
174174
readonly onDataReceived: Event<DataReceivedEvent>;
175175
readonly onRunDataReceived: Event<DataReceivedEvent>;
176176
readonly onDiscoveryDataReceived: Event<DataReceivedEvent>;
177-
sendCommand(
178-
options: TestCommandOptions,
179-
runTestIdsPort?: string,
180-
runInstance?: TestRun,
181-
callback?: () => void,
182-
): Promise<void>;
177+
sendCommand(options: TestCommandOptions, runTestIdsPort?: string, callback?: () => void): Promise<void>;
183178
serverReady(): Promise<void>;
184179
getPort(): number;
185180
createUUID(cwd: string): string;

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import * as path from 'path';
44
import { Uri } from 'vscode';
55
import {
66
ExecutionFactoryCreateWithEnvironmentOptions,
7-
ExecutionResult,
87
IPythonExecutionFactory,
98
SpawnOptions,
109
} from '../../../common/process/types';
1110
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
1211
import { createDeferred } from '../../../common/utils/async';
1312
import { EXTENSION_ROOT_DIR } from '../../../constants';
14-
import { traceVerbose } from '../../../logging';
13+
import { traceError, traceVerbose } from '../../../logging';
1514
import {
1615
DataReceivedEvent,
1716
DiscoveredTestPayload,
@@ -49,7 +48,7 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
4948
return discoveryPayload;
5049
}
5150

52-
async runPytestDiscovery(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise<void> {
51+
async runPytestDiscovery(uri: Uri, executionFactory?: IPythonExecutionFactory): Promise<DiscoveredTestPayload> {
5352
const deferred = createDeferred<DiscoveredTestPayload>();
5453
const relativePathToPytest = 'pythonFiles';
5554
const fullPluginPath = path.join(EXTENSION_ROOT_DIR, relativePathToPytest);
@@ -79,15 +78,17 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
7978
};
8079
const execService = await executionFactory?.createActivatedEnvironment(creationOptions);
8180
// delete UUID following entire discovery finishing.
82-
const deferredExec = createDeferred<ExecutionResult<string>>();
83-
const execArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs);
84-
const result = execService?.execObservable(execArgs, spawnOptions);
85-
86-
result?.proc?.on('close', () => {
87-
deferredExec.resolve({ stdout: '', stderr: '' });
88-
this.testServer.deleteUUID(uuid);
89-
deferred.resolve();
90-
});
91-
await deferredExec.promise;
81+
execService
82+
?.exec(['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs), spawnOptions)
83+
.then(() => {
84+
this.testServer.deleteUUID(uuid);
85+
return deferred.resolve();
86+
})
87+
.catch((err) => {
88+
traceError(`Error while trying to run pytest discovery, \n${err}\r\n\r\n`);
89+
this.testServer.deleteUUID(uuid);
90+
return deferred.reject(err);
91+
});
92+
return deferred.promise;
9293
}
9394
}

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

+15-26
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from '../common/types';
1616
import {
1717
ExecutionFactoryCreateWithEnvironmentOptions,
18-
ExecutionResult,
1918
IPythonExecutionFactory,
2019
SpawnOptions,
2120
} from '../../../common/process/types';
@@ -25,6 +24,12 @@ import { PYTEST_PROVIDER } from '../../common/constants';
2524
import { EXTENSION_ROOT_DIR } from '../../../common/constants';
2625
import { startTestIdServer } from '../common/utils';
2726

27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
// (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR;
29+
/**
30+
* Wrapper Class for pytest test execution..
31+
*/
32+
2833
export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
2934
constructor(
3035
public testServer: ITestServer,
@@ -43,20 +48,18 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
4348
): Promise<ExecutionTestPayload> {
4449
const uuid = this.testServer.createUUID(uri.fsPath);
4550
traceVerbose(uri, testIds, debugBool);
46-
const disposedDataReceived = this.testServer.onRunDataReceived((e: DataReceivedEvent) => {
51+
const disposable = this.testServer.onRunDataReceived((e: DataReceivedEvent) => {
4752
if (runInstance) {
4853
this.resultResolver?.resolveExecution(JSON.parse(e.data), runInstance);
4954
}
5055
});
51-
const dispose = function (testServer: ITestServer) {
52-
testServer.deleteUUID(uuid);
53-
disposedDataReceived.dispose();
54-
};
55-
runInstance?.token.onCancellationRequested(() => {
56-
dispose(this.testServer);
57-
});
58-
await this.runTestsNew(uri, testIds, uuid, runInstance, debugBool, executionFactory, debugLauncher);
59-
56+
try {
57+
await this.runTestsNew(uri, testIds, uuid, debugBool, executionFactory, debugLauncher);
58+
} finally {
59+
this.testServer.deleteUUID(uuid);
60+
disposable.dispose();
61+
// confirm with testing that this gets called (it must clean this up)
62+
}
6063
// placeholder until after the rewrite is adopted
6164
// TODO: remove after adoption.
6265
const executionPayload: ExecutionTestPayload = {
@@ -71,7 +74,6 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
7174
uri: Uri,
7275
testIds: string[],
7376
uuid: string,
74-
runInstance?: TestRun,
7577
debugBool?: boolean,
7678
executionFactory?: IPythonExecutionFactory,
7779
debugLauncher?: ITestDebugLauncher,
@@ -141,27 +143,14 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
141143
traceInfo(`Running DEBUG pytest with arguments: ${testArgs.join(' ')}\r\n`);
142144
await debugLauncher!.launchDebugger(launchOptions, () => {
143145
deferred.resolve();
144-
this.testServer.deleteUUID(uuid);
145146
});
146147
} else {
147148
// combine path to run script with run args
148149
const scriptPath = path.join(fullPluginPath, 'vscode_pytest', 'run_pytest_script.py');
149150
const runArgs = [scriptPath, ...testArgs];
150151
traceInfo(`Running pytests with arguments: ${runArgs.join(' ')}\r\n`);
151152

152-
const deferredExec = createDeferred<ExecutionResult<string>>();
153-
const result = execService?.execObservable(runArgs, spawnOptions);
154-
155-
runInstance?.token.onCancellationRequested(() => {
156-
result?.proc?.kill();
157-
});
158-
159-
result?.proc?.on('close', () => {
160-
deferredExec.resolve({ stdout: '', stderr: '' });
161-
this.testServer.deleteUUID(uuid);
162-
deferred.resolve();
163-
});
164-
await deferredExec.promise;
153+
await execService?.exec(runArgs, spawnOptions);
165154
}
166155
} catch (ex) {
167156
traceError(`Error while running tests: ${testIds}\r\n${ex}\r\n\r\n`);

src/client/testing/testController/unittest/testDiscoveryAdapter.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
4646
const disposable = this.testServer.onDiscoveryDataReceived((e: DataReceivedEvent) => {
4747
this.resultResolver?.resolveDiscovery(JSON.parse(e.data));
4848
});
49-
50-
await this.callSendCommand(options, () => {
49+
try {
50+
await this.callSendCommand(options);
51+
} finally {
5152
this.testServer.deleteUUID(uuid);
5253
disposable.dispose();
53-
});
54+
}
5455
// placeholder until after the rewrite is adopted
5556
// TODO: remove after adoption.
5657
const discoveryPayload: DiscoveredTestPayload = {
@@ -60,8 +61,8 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
6061
return discoveryPayload;
6162
}
6263

63-
private async callSendCommand(options: TestCommandOptions, callback: () => void): Promise<DiscoveredTestPayload> {
64-
await this.testServer.sendCommand(options, undefined, undefined, callback);
64+
private async callSendCommand(options: TestCommandOptions): Promise<DiscoveredTestPayload> {
65+
await this.testServer.sendCommand(options);
6566
const discoveryPayload: DiscoveredTestPayload = { cwd: '', status: 'success' };
6667
return discoveryPayload;
6768
}

src/client/testing/testController/unittest/testExecutionAdapter.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
3737
runInstance?: TestRun,
3838
): Promise<ExecutionTestPayload> {
3939
const uuid = this.testServer.createUUID(uri.fsPath);
40-
const disposedDataReceived = this.testServer.onRunDataReceived((e: DataReceivedEvent) => {
40+
const disposable = this.testServer.onRunDataReceived((e: DataReceivedEvent) => {
4141
if (runInstance) {
4242
this.resultResolver?.resolveExecution(JSON.parse(e.data), runInstance);
4343
}
4444
});
45-
const dispose = function () {
46-
disposedDataReceived.dispose();
47-
};
48-
runInstance?.token.onCancellationRequested(() => {
45+
try {
46+
await this.runTestsNew(uri, testIds, uuid, debugBool);
47+
} finally {
4948
this.testServer.deleteUUID(uuid);
50-
dispose();
51-
});
52-
await this.runTestsNew(uri, testIds, uuid, runInstance, debugBool, dispose);
49+
disposable.dispose();
50+
// confirm with testing that this gets called (it must clean this up)
51+
}
5352
const executionPayload: ExecutionTestPayload = { cwd: uri.fsPath, status: 'success', error: '' };
5453
return executionPayload;
5554
}
@@ -58,9 +57,7 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
5857
uri: Uri,
5958
testIds: string[],
6059
uuid: string,
61-
runInstance?: TestRun,
6260
debugBool?: boolean,
63-
dispose?: () => void,
6461
): Promise<ExecutionTestPayload> {
6562
const settings = this.configSettings.getSettings(uri);
6663
const { unittestArgs } = settings.testing;
@@ -83,10 +80,8 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
8380

8481
const runTestIdsPort = await startTestIdServer(testIds);
8582

86-
await this.testServer.sendCommand(options, runTestIdsPort.toString(), runInstance, () => {
87-
this.testServer.deleteUUID(uuid);
83+
await this.testServer.sendCommand(options, runTestIdsPort.toString(), () => {
8884
deferred.resolve();
89-
dispose?.();
9085
});
9186
// placeholder until after the rewrite is adopted
9287
// TODO: remove after adoption.

0 commit comments

Comments
 (0)