Skip to content

Commit c6c47f7

Browse files
committed
minor updates to fix EOT removal and unittest working
1 parent 5451fe6 commit c6c47f7

File tree

8 files changed

+36
-36
lines changed

8 files changed

+36
-36
lines changed

python_files/testing_tools/socket_manager.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ def connect(self):
2626

2727
def close(self):
2828
self._writer.close()
29-
self._reader.close()
29+
if hasattr(self, "_reader"):
30+
self._reader.close()
3031

3132
def write(self, data: str):
32-
3333
try:
3434
# for windows, is should only use \n\n
35-
request = (
36-
f"""content-length: {len(data)}\ncontent-type: application/json\n\n{data}"""
37-
)
35+
request = f"""content-length: {len(data)}\ncontent-type: application/json\n\n{data}"""
3836
self._writer.write(request)
3937
self._writer.flush()
4038
except Exception as e:

python_files/unittestadapter/pvsc_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def parse_unittest_args(
307307

308308

309309
def send_post_request(
310-
payload: Union[ExecutionPayloadDict, DiscoveryPayloadDict, CoveragePayloadDict],
310+
payload: ExecutionPayloadDict | DiscoveryPayloadDict | CoveragePayloadDict,
311311
test_run_pipe: Optional[str],
312312
):
313313
"""
@@ -331,10 +331,10 @@ def send_post_request(
331331

332332
if __writer is None:
333333
try:
334-
__writer = socket_manager.PipeManager(test_run_pipe)
335-
__writer.connect()
334+
__writer = open(test_run_pipe, "w", encoding="utf-8", newline="\r\n") # noqa: SIM115, PTH123
336335
except Exception as error:
337336
error_msg = f"Error attempting to connect to extension named pipe {test_run_pipe}[vscode-unittest]: {error}"
337+
print(error_msg, file=sys.stderr)
338338
__writer = None
339339
raise VSCodeUnittestError(error_msg) from error
340340

@@ -343,10 +343,11 @@ def send_post_request(
343343
"params": payload,
344344
}
345345
data = json.dumps(rpc)
346-
347346
try:
348347
if __writer:
349-
__writer.write(data)
348+
request = f"""content-length: {len(data)}\ncontent-type: application/json\n\n{data}"""
349+
__writer.write(request)
350+
__writer.flush()
350351
else:
351352
print(
352353
f"Connection error[vscode-unittest], writer is None \n[vscode-unittest] data: \n{data} \n",

python_files/vscode_pytest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def pytest_sessionfinish(session, exitstatus):
448448
result=file_coverage_map,
449449
error=None,
450450
)
451-
send_post_request(payload)
451+
send_message(payload)
452452

453453

454454
def build_test_tree(session: pytest.Session) -> TestNode:
@@ -858,7 +858,7 @@ def default(self, o):
858858
return super().default(o)
859859

860860

861-
def send_post_request(
861+
def send_message(
862862
payload: ExecutionPayloadDict | DiscoveryPayloadDict | CoveragePayloadDict,
863863
cls_encoder=None,
864864
):

src/client/common/pipes/namedPipes.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export async function createWriterPipe(pipeName: string, token?: CancellationTok
7575
await mkfifo(pipeName);
7676
try {
7777
await fs.chmod(pipeName, 0o666);
78-
} catch {}
78+
} catch {
79+
// Intentionally ignored
80+
}
7981
const writer = fs.createWriteStream(pipeName, {
8082
encoding: 'utf-8',
8183
});
@@ -89,6 +91,7 @@ class CombinedReader implements rpc.MessageReader {
8991

9092
private _onPartialMessage = new rpc.Emitter<rpc.PartialMessageInfo>();
9193

94+
// eslint-disable-next-line @typescript-eslint/no-empty-function
9295
private _callback: rpc.DataCallback = () => {};
9396

9497
private _disposables: rpc.Disposable[] = [];
@@ -107,6 +110,7 @@ class CombinedReader implements rpc.MessageReader {
107110

108111
listen(callback: rpc.DataCallback): rpc.Disposable {
109112
this._callback = callback;
113+
// eslint-disable-next-line no-return-assign, @typescript-eslint/no-empty-function
110114
return new Disposable(() => (this._callback = () => {}));
111115
}
112116

@@ -167,7 +171,7 @@ export async function createReaderPipe(pipeName: string, token?: CancellationTok
167171
server.listen(pipeName);
168172
if (token) {
169173
token.onCancellationRequested(() => {
170-
if (server.listening) {
174+
if (server.listening) {
171175
server.close();
172176
}
173177
deferred.reject(new CancellationError());
@@ -180,7 +184,9 @@ export async function createReaderPipe(pipeName: string, token?: CancellationTok
180184
await mkfifo(pipeName);
181185
try {
182186
await fs.chmod(pipeName, 0o666);
183-
} catch {}
187+
} catch {
188+
// Intentionally ignored
189+
}
184190
const reader = fs.createReadStream(pipeName, { encoding: 'utf-8' });
185191
return new rpc.StreamMessageReader(reader, 'utf-8');
186192
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export async function startRunResultNamedPipe(
256256
reader.listen((data: Message) => {
257257
traceVerbose(`Test Result named pipe ${pipeName} received data`);
258258
// if EOT, call decrement connection count (callback)
259-
dataReceivedCallback((data as ExecutionResultMessage).params as ExecutionTestPayload | EOTTestPayload);
259+
dataReceivedCallback((data as ExecutionResultMessage).params as ExecutionTestPayload);
260260
}),
261261
reader.onClose(() => {
262262
// this is called once the server close, once per run instance
@@ -306,10 +306,9 @@ export async function startDiscoveryNamedPipe(
306306
reader,
307307
reader.listen((data: Message) => {
308308
traceVerbose(`Test Discovery named pipe ${pipeName} received data`);
309-
callback((data as DiscoveryResultMessage).params as DiscoveredTestPayload | EOTTestPayload);
309+
callback((data as DiscoveryResultMessage).params as DiscoveredTestPayload);
310310
}),
311311
reader.onClose(() => {
312-
callback(createEOTPayload(false));
313312
traceVerbose(`Test Discovery named pipe ${pipeName} closed`);
314313
disposable.dispose();
315314
}),

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { TestRun, TestRunProfileKind, Uri } from 'vscode';
4+
import { CancellationTokenSource, TestRun, TestRunProfileKind, Uri } from 'vscode';
55
import * as path from 'path';
66
import { ChildProcess } from 'child_process';
77
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
@@ -58,9 +58,6 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
5858
);
5959
runInstance?.token.onCancellationRequested(() => {
6060
traceInfo(`Test run cancelled, resolving 'TillServerClose' deferred for ${uri.fsPath}.`);
61-
// if canceled, stop listening for results
62-
serverDispose(); // this will resolve deferredTillServerClose
63-
6461
const executionPayload: ExecutionTestPayload = {
6562
cwd: uri.fsPath,
6663
status: 'success',
@@ -74,7 +71,6 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
7471
uri,
7572
testIds,
7673
name,
77-
deferredTillEOT,
7874
cSource,
7975
runInstance,
8076
profileKind,
@@ -100,7 +96,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
10096
uri: Uri,
10197
testIds: string[],
10298
resultNamedPipeName: string,
103-
serverDispose: () => void,
99+
serverCancel: CancellationTokenSource,
104100
runInstance?: TestRun,
105101
profileKind?: TestRunProfileKind,
106102
executionFactory?: IPythonExecutionFactory,
@@ -174,7 +170,6 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
174170
traceInfo(`Running DEBUG pytest with arguments: ${testArgs} for workspace ${uri.fsPath} \r\n`);
175171
await debugLauncher!.launchDebugger(launchOptions, () => {
176172
serverCancel.cancel();
177-
deferredTillEOT?.resolve();
178173
});
179174
} else {
180175
// deferredTillExecClose is resolved when all stdout and stderr is read

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
4444
const { unittestArgs } = settings.testing;
4545
const cwd = settings.testing.cwd && settings.testing.cwd.length > 0 ? settings.testing.cwd : uri.fsPath;
4646

47-
const { name, dispose } = await startDiscoveryNamedPipe((data: DiscoveredTestPayload) => {
47+
const name = await startDiscoveryNamedPipe((data: DiscoveredTestPayload) => {
4848
this.resultResolver?.resolveDiscovery(data);
4949
});
5050

@@ -66,7 +66,7 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter {
6666
try {
6767
await this.runDiscovery(uri, options, name, cwd, executionFactory);
6868
} finally {
69-
dispose();
69+
// none
7070
}
7171
// placeholder until after the rewrite is adopted
7272
// TODO: remove after adoption.

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

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

44
import * as path from 'path';
5-
import { TestRun, TestRunProfileKind, Uri } from 'vscode';
5+
import { CancellationTokenSource, TestRun, TestRunProfileKind, Uri } from 'vscode';
66
import { ChildProcess } from 'child_process';
77
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
88
import { Deferred, createDeferred } from '../../../common/utils/async';
@@ -58,23 +58,24 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
5858
traceError(`No run instance found, cannot resolve execution, for workspace ${uri.fsPath}.`);
5959
}
6060
};
61-
const { name: resultNamedPipeName, dispose: serverDispose } = await utils.startRunResultNamedPipe(
61+
const cSource = new CancellationTokenSource();
62+
runInstance?.token.onCancellationRequested(() => cSource.cancel());
63+
const name = await utils.startRunResultNamedPipe(
6264
dataReceivedCallback, // callback to handle data received
6365
deferredTillServerClose, // deferred to resolve when server closes
64-
runInstance?.token, // token to cancel
66+
cSource.token, // token to cancel
6567
);
6668
runInstance?.token.onCancellationRequested(() => {
6769
console.log(`Test run cancelled, resolving 'till TillAllServerClose' deferred for ${uri.fsPath}.`);
6870
// if canceled, stop listening for results
6971
deferredTillServerClose.resolve();
70-
serverDispose();
7172
});
7273
try {
7374
await this.runTestsNew(
7475
uri,
7576
testIds,
76-
resultNamedPipeName,
77-
serverDispose,
77+
name,
78+
cSource,
7879
runInstance,
7980
profileKind,
8081
executionFactory,
@@ -97,7 +98,7 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
9798
uri: Uri,
9899
testIds: string[],
99100
resultNamedPipeName: string,
100-
serverDispose: () => void,
101+
serverCancel: CancellationTokenSource,
101102
runInstance?: TestRun,
102103
profileKind?: TestRunProfileKind,
103104
executionFactory?: IPythonExecutionFactory,
@@ -172,7 +173,7 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
172173
throw new Error('Debug launcher is not defined');
173174
}
174175
await debugLauncher.launchDebugger(launchOptions, () => {
175-
serverDispose(); // this will resolve the deferredTillAllServerClose
176+
serverCancel.cancel();
176177
});
177178
} else {
178179
// This means it is running the test
@@ -225,9 +226,9 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
225226
runInstance,
226227
);
227228
}
228-
serverDispose();
229229
}
230230
deferredTillExecClose.resolve();
231+
serverCancel.cancel();
231232
});
232233
await deferredTillExecClose.promise;
233234
}

0 commit comments

Comments
 (0)