Skip to content

Commit dc5fa4e

Browse files
authored
Move testid server (#21400)
Created a testIdServer to handle the starting up of the testIdServer for both unittest and pytest. This extracts the method to be in another file and easy to test.
1 parent 8cec0fc commit dc5fa4e

File tree

4 files changed

+9
-65
lines changed

4 files changed

+9
-65
lines changed

pythonFiles/vscode_pytest/run_pytest_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Add the root directory to the path so that we can import the plugin.
2222
directory_path = pathlib.Path(__file__).parent.parent
2323
sys.path.append(os.fspath(directory_path))
24+
sys.path.insert(0, os.getcwd())
2425
# Get the rest of the args to run with pytest.
2526
args = sys.argv[1:]
2627
run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai
6262
return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment);
6363
}
6464

65-
export const startServer = (testIds: string): Promise<number> =>
65+
export const startTestIdServer = (testIds: string[]): Promise<number> =>
6666
new Promise((resolve, reject) => {
6767
const server = net.createServer((socket: net.Socket) => {
6868
// Convert the test_ids array to JSON

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { TestRun, Uri } from 'vscode';
55
import * as path from 'path';
6-
import * as net from 'net';
76
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
87
import { createDeferred, Deferred } from '../../../common/utils/async';
98
import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging';
@@ -23,6 +22,7 @@ import { removePositionalFoldersAndFiles } from './arguments';
2322
import { ITestDebugLauncher, LaunchOptions } from '../../common/types';
2423
import { PYTEST_PROVIDER } from '../../common/constants';
2524
import { EXTENSION_ROOT_DIR } from '../../../common/constants';
25+
import { startTestIdServer } from '../common/utils';
2626

2727
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2828
// (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR;
@@ -116,40 +116,10 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter {
116116
if (debugBool && !testArgs.some((a) => a.startsWith('--capture') || a === '-s')) {
117117
testArgs.push('--capture', 'no');
118118
}
119-
120-
// create payload with testIds to send to run pytest script
121-
const testData = JSON.stringify(testIds);
122-
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
123-
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
124-
traceLog(`Running pytest execution for the following test ids: ${testIds}`);
119+
traceLog(`Running PYTEST execution for the following test ids: ${testIds}`);
125120

126121
let pytestRunTestIdsPort: string | undefined;
127-
const startServer = (): Promise<number> =>
128-
new Promise((resolve, reject) => {
129-
const server = net.createServer((socket: net.Socket) => {
130-
socket.on('end', () => {
131-
traceVerbose('Client disconnected for pytest test ids server');
132-
});
133-
});
134-
135-
server.listen(0, () => {
136-
const { port } = server.address() as net.AddressInfo;
137-
traceVerbose(`Server listening on port ${port} for pytest test ids server`);
138-
resolve(port);
139-
});
140-
141-
server.on('error', (error: Error) => {
142-
traceError('Error starting server for pytest test ids server:', error);
143-
reject(error);
144-
});
145-
server.on('connection', (socket: net.Socket) => {
146-
socket.write(payload);
147-
traceVerbose('payload sent for pytest execution', payload);
148-
});
149-
});
150-
151-
// Start the server and wait until it is listening
152-
await startServer()
122+
await startTestIdServer(testIds)
153123
.then((assignedPort) => {
154124
traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`);
155125
pytestRunTestIdsPort = assignedPort.toString();

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

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
import * as path from 'path';
55
import { TestRun, Uri } from 'vscode';
6-
import * as net from 'net';
76
import { IConfigurationService, ITestOutputChannel } from '../../../common/types';
8-
import { createDeferred, Deferred } from '../../../common/utils/async';
7+
import { Deferred, createDeferred } from '../../../common/utils/async';
98
import { EXTENSION_ROOT_DIR } from '../../../constants';
109
import {
1110
DataReceivedEvent,
@@ -17,6 +16,7 @@ import {
1716
TestExecutionCommand,
1817
} from '../common/types';
1918
import { traceLog, traceError } from '../../../logging';
19+
import { startTestIdServer } from '../common/utils';
2020

2121
/**
2222
* Wrapper Class for unittest test execution. This is where we call `runTestCommand`?
@@ -75,37 +75,10 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter {
7575

7676
const deferred = createDeferred<ExecutionTestPayload>();
7777
this.promiseMap.set(uuid, deferred);
78-
// create payload with testIds to send to run pytest script
79-
const testData = JSON.stringify(testIds);
80-
const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json'];
81-
const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`;
78+
traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`);
8279

8380
let runTestIdsPort: string | undefined;
84-
const startServer = (): Promise<number> =>
85-
new Promise((resolve, reject) => {
86-
const server = net.createServer((socket: net.Socket) => {
87-
socket.on('end', () => {
88-
traceLog('Client disconnected');
89-
});
90-
});
91-
92-
server.listen(0, () => {
93-
const { port } = server.address() as net.AddressInfo;
94-
traceLog(`Server listening on port ${port}`);
95-
resolve(port);
96-
});
97-
98-
server.on('error', (error: Error) => {
99-
reject(error);
100-
});
101-
server.on('connection', (socket: net.Socket) => {
102-
socket.write(payload);
103-
traceLog('payload sent', payload);
104-
});
105-
});
106-
107-
// Start the server and wait until it is listening
108-
await startServer()
81+
await startTestIdServer(testIds)
10982
.then((assignedPort) => {
11083
traceLog(`Server started and listening on port ${assignedPort}`);
11184
runTestIdsPort = assignedPort.toString();

0 commit comments

Comments
 (0)