Skip to content

Commit bfdb6ac

Browse files
committed
Result resolver feature branch (microsoft#21457)
fixes microsoft#21394
1 parent e257501 commit bfdb6ac

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

pythonFiles/unittestadapter/execution.py

+58-7
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import json
77
import os
88
import pathlib
9+
import socket
910
import sys
1011
import traceback
1112
import unittest
1213
from types import TracebackType
1314
from typing import Dict, List, Optional, Tuple, Type, Union
1415

16+
from pythonFiles.testing_tools import process_json_util
17+
1518
directory_path = pathlib.Path(__file__).parent.parent / "lib" / "python"
1619
# Add the path to pythonFiles to sys.path to find testing_tools.socket_manager.
1720
PYTHON_FILES = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -28,7 +31,7 @@
2831

2932
def parse_execution_cli_args(
3033
args: List[str],
31-
) -> Tuple[int, Union[str, None], List[str]]:
34+
) -> Tuple[int, Union[str, None]]:
3235
"""Parse command-line arguments that should be processed by the script.
3336
3437
So far this includes the port number that it needs to connect to, the uuid passed by the TS side,
@@ -42,10 +45,9 @@ def parse_execution_cli_args(
4245
arg_parser = argparse.ArgumentParser()
4346
arg_parser.add_argument("--port", default=DEFAULT_PORT)
4447
arg_parser.add_argument("--uuid")
45-
arg_parser.add_argument("--testids", nargs="+")
4648
parsed_args, _ = arg_parser.parse_known_args(args)
4749

48-
return (int(parsed_args.port), parsed_args.uuid, parsed_args.testids)
50+
return (int(parsed_args.port), parsed_args.uuid)
4951

5052

5153
ErrorType = Union[
@@ -258,8 +260,57 @@ def send_run_data(raw_data, port, uuid):
258260
argv = sys.argv[1:]
259261
index = argv.index("--udiscovery")
260262

261-
START_DIR, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
263+
start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :])
264+
265+
run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT")
266+
run_test_ids_port_int = (
267+
int(run_test_ids_port) if run_test_ids_port is not None else 0
268+
)
262269

263-
# Perform test execution.
264-
PORT, UUID, testids = parse_execution_cli_args(argv[:index])
265-
payload = run_tests(START_DIR, testids, pattern, top_level_dir, UUID)
270+
# get data from socket
271+
test_ids_from_buffer = []
272+
try:
273+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
274+
client_socket.connect(("localhost", run_test_ids_port_int))
275+
buffer = b""
276+
277+
while True:
278+
# Receive the data from the client
279+
data = client_socket.recv(1024 * 1024)
280+
if not data:
281+
break
282+
283+
# Append the received data to the buffer
284+
buffer += data
285+
286+
try:
287+
# Try to parse the buffer as JSON
288+
test_ids_from_buffer = process_json_util.process_rpc_json(
289+
buffer.decode("utf-8")
290+
)
291+
# Clear the buffer as complete JSON object is received
292+
buffer = b""
293+
294+
# Process the JSON data
295+
break
296+
except json.JSONDecodeError:
297+
# JSON decoding error, the complete JSON object is not yet received
298+
continue
299+
except socket.error as e:
300+
print(f"Error: Could not connect to runTestIdsPort: {e}")
301+
print("Error: Could not connect to runTestIdsPort")
302+
303+
port, uuid = parse_execution_cli_args(argv[:index])
304+
if test_ids_from_buffer:
305+
# Perform test execution.
306+
payload = run_tests(
307+
start_dir, test_ids_from_buffer, pattern, top_level_dir, uuid
308+
)
309+
else:
310+
cwd = os.path.abspath(start_dir)
311+
status = TestExecutionStatus.error
312+
payload: PayloadDict = {
313+
"cwd": cwd,
314+
"status": status,
315+
"error": "No test ids received from buffer",
316+
}

0 commit comments

Comments
 (0)