@@ -34,113 +34,6 @@ export function createTestingDeferred(): Deferred<void> {
3434 return createDeferred < void > ( ) ;
3535}
3636
37- export function extractJsonPayload ( rawData : string , uuids : Array < string > ) : ExtractOutput {
38- /**
39- * Extracts JSON-RPC payload from the provided raw data.
40- * @param {string } rawData - The raw string data from which the JSON payload will be extracted.
41- * @param {Array<string> } uuids - The list of UUIDs that are active.
42- * @returns {string } The remaining raw data after the JSON payload is extracted.
43- */
44-
45- const rpcHeaders : ParsedRPCHeadersAndData = parseJsonRPCHeadersAndData ( rawData ) ;
46-
47- // verify the RPC has a UUID and that it is recognized
48- let uuid = rpcHeaders . headers . get ( JSONRPC_UUID_HEADER ) ;
49- uuid = checkUuid ( uuid , uuids ) ;
50-
51- const payloadLength = rpcHeaders . headers . get ( 'Content-Length' ) ;
52-
53- // separate out the data within context length of the given payload from the remaining data in the buffer
54- const rpcContent : IJSONRPCData = ExtractJsonRPCData ( payloadLength , rpcHeaders . remainingRawData ) ;
55- const cleanedJsonData = rpcContent . extractedJSON ;
56- const { remainingRawData } = rpcContent ;
57-
58- // if the given payload has the complete json, process it otherwise wait for the rest in the buffer
59- if ( cleanedJsonData . length === Number ( payloadLength ) ) {
60- // call to process this data
61- // remove this data from the buffer
62- return { uuid, cleanedJsonData, remainingRawData } ;
63- }
64- // wait for the remaining
65- return { uuid : undefined , cleanedJsonData : undefined , remainingRawData : rawData } ;
66- }
67-
68- export function checkUuid ( uuid : string | undefined , uuids : Array < string > ) : string | undefined {
69- if ( ! uuid ) {
70- // no UUID found, this could occurred if the payload is full yet so send back without erroring
71- return undefined ;
72- }
73- if ( ! uuids . includes ( uuid ) ) {
74- // no UUID found, this could occurred if the payload is full yet so send back without erroring
75- throw new Error ( 'On data received: Error occurred because the payload UUID is not recognized' ) ;
76- }
77- return uuid ;
78- }
79-
80- export function parseJsonRPCHeadersAndData ( rawData : string ) : ParsedRPCHeadersAndData {
81- /**
82- * Parses the provided raw data to extract JSON-RPC specific headers and remaining data.
83- *
84- * This function aims to extract specific JSON-RPC headers (like UUID, content length,
85- * and content type) from the provided raw string data. Headers are expected to be
86- * delimited by newlines and the format should be "key:value". The function stops parsing
87- * once it encounters an empty line, and the rest of the data after this line is treated
88- * as the remaining raw data.
89- *
90- * @param {string } rawData - The raw string containing headers and possibly other data.
91- * @returns {ParsedRPCHeadersAndData } An object containing the parsed headers as a map and the
92- * remaining raw data after the headers.
93- */
94- const lines = rawData . split ( '\n' ) ;
95- let remainingRawData = '' ;
96- const headerMap = new Map < string , string > ( ) ;
97- for ( let i = 0 ; i < lines . length ; i += 1 ) {
98- const line = lines [ i ] ;
99- if ( line === '' ) {
100- remainingRawData = lines . slice ( i + 1 ) . join ( '\n' ) ;
101- break ;
102- }
103- const [ key , value ] = line . split ( ':' ) ;
104- if ( value && value . trim ( ) ) {
105- if ( [ JSONRPC_UUID_HEADER , JSONRPC_CONTENT_LENGTH_HEADER , JSONRPC_CONTENT_TYPE_HEADER ] . includes ( key ) ) {
106- headerMap . set ( key . trim ( ) , value . trim ( ) ) ;
107- }
108- }
109- }
110-
111- return {
112- headers : headerMap ,
113- remainingRawData,
114- } ;
115- }
116-
117- export function ExtractJsonRPCData ( payloadLength : string | undefined , rawData : string ) : IJSONRPCData {
118- /**
119- * Extracts JSON-RPC content based on provided headers and raw data.
120- *
121- * This function uses the `Content-Length` header from the provided headers map
122- * to determine how much of the rawData string represents the actual JSON content.
123- * After extracting the expected content, it also returns any remaining data
124- * that comes after the extracted content as remaining raw data.
125- *
126- * @param {string | undefined } payloadLength - The value of the `Content-Length` header.
127- * @param {string } rawData - The raw string data from which the JSON content will be extracted.
128- *
129- * @returns {IJSONRPCContent } An object containing the extracted JSON content and any remaining raw data.
130- */
131- const length = parseInt ( payloadLength ?? '0' , 10 ) ;
132- const data = rawData . slice ( 0 , length ) ;
133- const remainingRawData = rawData . slice ( length ) ;
134- return {
135- extractedJSON : data ,
136- remainingRawData,
137- } ;
138- }
139-
140- export function pythonTestAdapterRewriteEnabled ( serviceContainer : IServiceContainer ) : boolean {
141- const experiment = serviceContainer . get < IExperimentService > ( IExperimentService ) ;
142- return experiment . inExperimentSync ( EnableTestAdapterRewrite . experiment ) ;
143- }
14437
14538export async function startTestIdsNamedPipe ( testIds : string [ ] ) : Promise < string > {
14639 const pipeName : string = generateRandomPipeName ( 'python-test-ids' ) ;
@@ -284,63 +177,6 @@ export async function startDiscoveryNamedPipe(
284177 return { name : pipeName , dispose } ;
285178}
286179
287- export async function startTestIdServer ( testIds : string [ ] ) : Promise < number > {
288- const startServer = ( ) : Promise < number > =>
289- new Promise ( ( resolve , reject ) => {
290- const server = net . createServer ( ( socket : net . Socket ) => {
291- // Convert the test_ids array to JSON
292- const testData = JSON . stringify ( testIds ) ;
293-
294- // Create the headers
295- const headers = [ `Content-Length: ${ Buffer . byteLength ( testData ) } ` , 'Content-Type: application/json' ] ;
296-
297- // Create the payload by concatenating the headers and the test data
298- const payload = `${ headers . join ( '\r\n' ) } \r\n\r\n${ testData } ` ;
299-
300- // Send the payload to the socket
301- socket . write ( payload ) ;
302-
303- // Handle socket events
304- socket . on ( 'data' , ( data ) => {
305- traceLog ( 'Received data:' , data . toString ( ) ) ;
306- } ) ;
307-
308- socket . on ( 'end' , ( ) => {
309- traceLog ( 'Client disconnected' ) ;
310- } ) ;
311- } ) ;
312-
313- server . listen ( 0 , ( ) => {
314- const { port } = server . address ( ) as net . AddressInfo ;
315- traceLog ( `Server listening on port ${ port } ` ) ;
316- resolve ( port ) ;
317- } ) ;
318-
319- server . on ( 'error' , ( error : Error ) => {
320- reject ( error ) ;
321- } ) ;
322- } ) ;
323-
324- // Start the server and wait until it is listening
325- let returnPort = 0 ;
326- try {
327- await startServer ( )
328- . then ( ( assignedPort ) => {
329- traceVerbose ( `Server started for pytest test ids server and listening on port ${ assignedPort } ` ) ;
330- returnPort = assignedPort ;
331- } )
332- . catch ( ( error ) => {
333- traceError ( 'Error starting server for pytest test ids server:' , error ) ;
334- return 0 ;
335- } )
336- . finally ( ( ) => returnPort ) ;
337- return returnPort ;
338- } catch {
339- traceError ( 'Error starting server for pytest test ids server, cannot get port.' ) ;
340- return returnPort ;
341- }
342- }
343-
344180export function buildErrorNodeOptions ( uri : Uri , message : string , testType : string ) : ErrorTestItemOptions {
345181 const labelText = testType === 'pytest' ? 'pytest Discovery Error' : 'Unittest Discovery Error' ;
346182 return {
0 commit comments