diff --git a/pythonFiles/vscode_pytest/run_pytest_script.py b/pythonFiles/vscode_pytest/run_pytest_script.py index 57bb41a3a7dd..ffb4d0c55b16 100644 --- a/pythonFiles/vscode_pytest/run_pytest_script.py +++ b/pythonFiles/vscode_pytest/run_pytest_script.py @@ -21,6 +21,7 @@ # Add the root directory to the path so that we can import the plugin. directory_path = pathlib.Path(__file__).parent.parent sys.path.append(os.fspath(directory_path)) + sys.path.insert(0, os.getcwd()) # Get the rest of the args to run with pytest. args = sys.argv[1:] run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT") diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index 1bf31e80e11a..9d426dc60ffa 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -62,7 +62,7 @@ export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContai return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); } -export const startServer = (testIds: string): Promise => +export const startTestIdServer = (testIds: string[]): Promise => new Promise((resolve, reject) => { const server = net.createServer((socket: net.Socket) => { // Convert the test_ids array to JSON diff --git a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index 6d63b49d007f..9c8a42ae097f 100644 --- a/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -3,7 +3,6 @@ import { TestRun, Uri } from 'vscode'; import * as path from 'path'; -import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; @@ -23,6 +22,7 @@ import { removePositionalFoldersAndFiles } from './arguments'; import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; import { PYTEST_PROVIDER } from '../../common/constants'; import { EXTENSION_ROOT_DIR } from '../../../common/constants'; +import { startTestIdServer } from '../common/utils'; // eslint-disable-next-line @typescript-eslint/no-explicit-any // (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; @@ -116,40 +116,10 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { if (debugBool && !testArgs.some((a) => a.startsWith('--capture') || a === '-s')) { testArgs.push('--capture', 'no'); } - - // create payload with testIds to send to run pytest script - const testData = JSON.stringify(testIds); - const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; - const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; - traceLog(`Running pytest execution for the following test ids: ${testIds}`); + traceLog(`Running PYTEST execution for the following test ids: ${testIds}`); let pytestRunTestIdsPort: string | undefined; - const startServer = (): Promise => - new Promise((resolve, reject) => { - const server = net.createServer((socket: net.Socket) => { - socket.on('end', () => { - traceVerbose('Client disconnected for pytest test ids server'); - }); - }); - - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceVerbose(`Server listening on port ${port} for pytest test ids server`); - resolve(port); - }); - - server.on('error', (error: Error) => { - traceError('Error starting server for pytest test ids server:', error); - reject(error); - }); - server.on('connection', (socket: net.Socket) => { - socket.write(payload); - traceVerbose('payload sent for pytest execution', payload); - }); - }); - - // Start the server and wait until it is listening - await startServer() + await startTestIdServer(testIds) .then((assignedPort) => { traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); pytestRunTestIdsPort = assignedPort.toString(); diff --git a/src/client/testing/testController/unittest/testExecutionAdapter.ts b/src/client/testing/testController/unittest/testExecutionAdapter.ts index 16e7d03804a1..a85490c30bc8 100644 --- a/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -3,9 +3,8 @@ import * as path from 'path'; import { TestRun, Uri } from 'vscode'; -import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; -import { createDeferred, Deferred } from '../../../common/utils/async'; +import { Deferred, createDeferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; import { DataReceivedEvent, @@ -17,6 +16,7 @@ import { TestExecutionCommand, } from '../common/types'; import { traceLog, traceError } from '../../../logging'; +import { startTestIdServer } from '../common/utils'; /** * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? @@ -75,37 +75,10 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { const deferred = createDeferred(); this.promiseMap.set(uuid, deferred); - // create payload with testIds to send to run pytest script - const testData = JSON.stringify(testIds); - const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; - const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; + traceLog(`Running UNITTEST execution for the following test ids: ${testIds}`); let runTestIdsPort: string | undefined; - const startServer = (): Promise => - new Promise((resolve, reject) => { - const server = net.createServer((socket: net.Socket) => { - socket.on('end', () => { - traceLog('Client disconnected'); - }); - }); - - server.listen(0, () => { - const { port } = server.address() as net.AddressInfo; - traceLog(`Server listening on port ${port}`); - resolve(port); - }); - - server.on('error', (error: Error) => { - reject(error); - }); - server.on('connection', (socket: net.Socket) => { - socket.write(payload); - traceLog('payload sent', payload); - }); - }); - - // Start the server and wait until it is listening - await startServer() + await startTestIdServer(testIds) .then((assignedPort) => { traceLog(`Server started and listening on port ${assignedPort}`); runTestIdsPort = assignedPort.toString();