5
5
import enum
6
6
import json
7
7
import os
8
+ import pathlib
9
+ import socket
8
10
import sys
9
11
import traceback
10
12
import unittest
11
13
from types import TracebackType
12
14
from typing import Dict , List , Optional , Tuple , Type , Union
13
15
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
+
14
21
# Add the path to pythonFiles to sys.path to find testing_tools.socket_manager.
15
22
PYTHON_FILES = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
16
23
sys .path .insert (0 , PYTHON_FILES )
25
32
26
33
def parse_execution_cli_args (
27
34
args : List [str ],
28
- ) -> Tuple [int , Union [str , None ], List [ str ] ]:
35
+ ) -> Tuple [int , Union [str , None ]]:
29
36
"""Parse command-line arguments that should be processed by the script.
30
37
31
38
So far this includes the port number that it needs to connect to, the uuid passed by the TS side,
@@ -39,10 +46,9 @@ def parse_execution_cli_args(
39
46
arg_parser = argparse .ArgumentParser ()
40
47
arg_parser .add_argument ("--port" , default = DEFAULT_PORT )
41
48
arg_parser .add_argument ("--uuid" )
42
- arg_parser .add_argument ("--testids" , nargs = "+" )
43
49
parsed_args , _ = arg_parser .parse_known_args (args )
44
50
45
- return (int (parsed_args .port ), parsed_args .uuid , parsed_args . testids )
51
+ return (int (parsed_args .port ), parsed_args .uuid )
46
52
47
53
48
54
ErrorType = Union [
@@ -226,11 +232,62 @@ def run_tests(
226
232
227
233
start_dir , pattern , top_level_dir = parse_unittest_args (argv [index + 1 :])
228
234
229
- # Perform test execution.
230
- port , uuid , testids = parse_execution_cli_args (argv [:index ])
231
- payload = run_tests (start_dir , testids , pattern , top_level_dir , uuid )
235
+ run_test_ids_port = os .environ .get ("RUN_TEST_IDS_PORT" )
236
+ run_test_ids_port_int = (
237
+ int (run_test_ids_port ) if run_test_ids_port is not None else 0
238
+ )
239
+
240
+ # get data from socket
241
+ test_ids_from_buffer = []
242
+ try :
243
+ client_socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
244
+ client_socket .connect (("localhost" , run_test_ids_port_int ))
245
+ print (f"CLIENT: Server listening on port { run_test_ids_port_int } ..." )
246
+ buffer = b""
247
+
248
+ while True :
249
+ # Receive the data from the client
250
+ data = client_socket .recv (1024 * 1024 )
251
+ if not data :
252
+ break
253
+
254
+ # Append the received data to the buffer
255
+ buffer += data
256
+
257
+ try :
258
+ # Try to parse the buffer as JSON
259
+ test_ids_from_buffer = process_json_util .process_rpc_json (
260
+ buffer .decode ("utf-8" )
261
+ )
262
+ # Clear the buffer as complete JSON object is received
263
+ buffer = b""
264
+
265
+ # Process the JSON data
266
+ print (f"Received JSON data: { test_ids_from_buffer } " )
267
+ break
268
+ except json .JSONDecodeError :
269
+ # JSON decoding error, the complete JSON object is not yet received
270
+ continue
271
+ except socket .error as e :
272
+ print (f"Error: Could not connect to runTestIdsPort: { e } " )
273
+ print ("Error: Could not connect to runTestIdsPort" )
274
+
275
+ port , uuid = parse_execution_cli_args (argv [:index ])
276
+ if test_ids_from_buffer :
277
+ # Perform test execution.
278
+ payload = run_tests (
279
+ start_dir , test_ids_from_buffer , pattern , top_level_dir , uuid
280
+ )
281
+ else :
282
+ cwd = os .path .abspath (start_dir )
283
+ status = TestExecutionStatus .error
284
+ payload : PayloadDict = {
285
+ "cwd" : cwd ,
286
+ "status" : status ,
287
+ "error" : "No test ids received from buffer" ,
288
+ }
232
289
233
- # Build the request data (it has to be a POST request or the Node side will not process it), and send it.
290
+ # Build the request data and send it.
234
291
addr = ("localhost" , port )
235
292
data = json .dumps (payload )
236
293
request = f"""Content-Length: { len (data )}
0 commit comments