1313from types import TracebackType
1414from typing import Dict , List , Optional , Tuple , Type , Union
1515
16- script_dir = pathlib .Path (__file__ ).parent .parent
17- sys .path .append (os .fspath (script_dir ))
18- sys .path .append (os .fspath (script_dir / "lib" / "python" ))
19- from testing_tools import process_json_util
20-
16+ directory_path = pathlib .Path (__file__ ).parent .parent / "lib" / "python"
2117# Add the path to pythonFiles to sys.path to find testing_tools.socket_manager.
22- PYTHON_FILES = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
23- sys .path .insert (0 , PYTHON_FILES )
18+ PYTHON_FILES = pathlib .Path (__file__ ).parent .parent
19+
20+ sys .path .insert (0 , os .fspath (PYTHON_FILES ))
2421# Add the lib path to sys.path to find the typing_extensions module.
2522sys .path .insert (0 , os .path .join (PYTHON_FILES , "lib" , "python" ))
26- from testing_tools import socket_manager
23+ from testing_tools import process_json_util , socket_manager
2724from typing_extensions import NotRequired , TypeAlias , TypedDict
2825from unittestadapter .utils import parse_unittest_args
2926
@@ -54,6 +51,9 @@ def parse_execution_cli_args(
5451ErrorType = Union [
5552 Tuple [Type [BaseException ], BaseException , TracebackType ], Tuple [None , None , None ]
5653]
54+ PORT = 0
55+ UUID = 0
56+ START_DIR = ""
5757
5858
5959class TestOutcomeEnum (str , enum .Enum ):
@@ -148,8 +148,10 @@ def formatResult(
148148 "traceback" : tb ,
149149 "subtest" : subtest .id () if subtest else None ,
150150 }
151-
152151 self .formatted [test_id ] = result
152+ if PORT == 0 or UUID == 0 :
153+ print ("Error sending response, port or uuid unknown to python server." )
154+ send_run_data (result , PORT , UUID )
153155
154156
155157class TestExecutionStatus (str , enum .Enum ):
@@ -225,6 +227,33 @@ def run_tests(
225227 return payload
226228
227229
230+ def send_run_data (raw_data , port , uuid ):
231+ # Build the request data (it has to be a POST request or the Node side will not process it), and send it.
232+ status = raw_data ["outcome" ]
233+ cwd = os .path .abspath (START_DIR )
234+ if raw_data ["subtest" ]:
235+ test_id = raw_data ["subtest" ]
236+ else :
237+ test_id = raw_data ["test" ]
238+ test_dict = {}
239+ test_dict [test_id ] = raw_data
240+ payload : PayloadDict = {"cwd" : cwd , "status" : status , "result" : test_dict }
241+ addr = ("localhost" , port )
242+ data = json .dumps (payload )
243+ request = f"""Content-Length: { len (data )}
244+ Content-Type: application/json
245+ Request-uuid: { uuid }
246+
247+ { data } """
248+ try :
249+ with socket_manager .SocketManager (addr ) as s :
250+ if s .socket is not None :
251+ s .socket .sendall (request .encode ("utf-8" ))
252+ except Exception as e :
253+ print (f"Error sending response: { e } " )
254+ print (f"Request data: { request } " )
255+
256+
228257if __name__ == "__main__" :
229258 # Get unittest test execution arguments.
230259 argv = sys .argv [1 :]
@@ -270,11 +299,11 @@ def run_tests(
270299 print (f"Error: Could not connect to runTestIdsPort: { e } " )
271300 print ("Error: Could not connect to runTestIdsPort" )
272301
273- port , uuid = parse_execution_cli_args (argv [:index ])
302+ PORT , UUID = parse_execution_cli_args (argv [:index ])
274303 if test_ids_from_buffer :
275304 # Perform test execution.
276305 payload = run_tests (
277- start_dir , test_ids_from_buffer , pattern , top_level_dir , uuid
306+ start_dir , test_ids_from_buffer , pattern , top_level_dir , UUID
278307 )
279308 else :
280309 cwd = os .path .abspath (start_dir )
@@ -284,19 +313,3 @@ def run_tests(
284313 "status" : status ,
285314 "error" : "No test ids received from buffer" ,
286315 }
287-
288- # Build the request data and send it.
289- addr = ("localhost" , port )
290- data = json .dumps (payload )
291- request = f"""Content-Length: { len (data )}
292- Content-Type: application/json
293- Request-uuid: { uuid }
294-
295- { data } """
296- try :
297- with socket_manager .SocketManager (addr ) as s :
298- if s .socket is not None :
299- s .socket .sendall (request .encode ("utf-8" ))
300- except Exception as e :
301- print (f"Error sending response: { e } " )
302- print (f"Request data: { request } " )
0 commit comments