|
3 | 3 |
|
4 | 4 | import atexit |
5 | 5 | import enum |
6 | | -import json |
7 | 6 | import os |
8 | 7 | import pathlib |
9 | 8 | import sys |
|
24 | 23 |
|
25 | 24 | from django_handler import django_execution_runner # noqa: E402 |
26 | 25 |
|
27 | | -from testing_tools import process_json_util, socket_manager # noqa: E402 |
28 | 26 | from unittestadapter.pvsc_utils import ( # noqa: E402 |
29 | 27 | EOTPayloadDict, |
30 | 28 | ExecutionPayloadDict, |
@@ -269,8 +267,15 @@ def run_tests( |
269 | 267 | return payload |
270 | 268 |
|
271 | 269 |
|
| 270 | +def execute_eot_and_cleanup(): |
| 271 | + eot_payload: EOTPayloadDict = {"command_type": "execution", "eot": True} |
| 272 | + send_post_request(eot_payload, test_run_pipe) |
| 273 | + if __socket: |
| 274 | + __socket.close() |
| 275 | + |
| 276 | + |
272 | 277 | __socket = None |
273 | | -atexit.register(lambda: __socket.close() if __socket else None) |
| 278 | +atexit.register(execute_eot_and_cleanup) |
274 | 279 |
|
275 | 280 |
|
276 | 281 | def send_run_data(raw_data, test_run_pipe): |
@@ -306,70 +311,43 @@ def send_run_data(raw_data, test_run_pipe): |
306 | 311 | if not test_run_pipe: |
307 | 312 | print("Error[vscode-unittest]: TEST_RUN_PIPE env var is not set.") |
308 | 313 | raise VSCodeUnittestError("Error[vscode-unittest]: TEST_RUN_PIPE env var is not set.") |
309 | | - test_ids_from_buffer = [] |
310 | | - raw_json = None |
311 | | - try: |
312 | | - with socket_manager.PipeManager(run_test_ids_pipe) as sock: |
313 | | - buffer: str = "" |
314 | | - while True: |
315 | | - # Receive the data from the client |
316 | | - data: str = sock.read() |
317 | | - if not data: |
318 | | - break |
319 | | - |
320 | | - # Append the received data to the buffer |
321 | | - buffer += data |
322 | | - |
323 | | - try: |
324 | | - # Try to parse the buffer as JSON |
325 | | - raw_json = process_json_util.process_rpc_json(buffer) |
326 | | - # Clear the buffer as complete JSON object is received |
327 | | - buffer = "" |
328 | | - print("Received JSON data in run") |
329 | | - break |
330 | | - except json.JSONDecodeError: |
331 | | - # JSON decoding error, the complete JSON object is not yet received |
332 | | - continue |
333 | | - except OSError as e: |
334 | | - msg = f"Error: Could not connect to RUN_TEST_IDS_PIPE: {e}" |
335 | | - print(msg) |
336 | | - raise VSCodeUnittestError(msg) from e |
337 | | - |
| 314 | + test_ids = [] |
338 | 315 | try: |
339 | | - if raw_json and "params" in raw_json and raw_json["params"]: |
340 | | - test_ids_from_buffer = raw_json["params"] |
341 | | - # Check to see if we are running django tests. |
342 | | - if manage_py_path := os.environ.get("MANAGE_PY_PATH"): |
343 | | - args = argv[index + 1 :] or [] |
344 | | - django_execution_runner(manage_py_path, test_ids_from_buffer, args) |
345 | | - # the django run subprocesses sends the eot payload. |
346 | | - else: |
347 | | - # Perform test execution. |
348 | | - payload = run_tests( |
349 | | - start_dir, |
350 | | - test_ids_from_buffer, |
351 | | - pattern, |
352 | | - top_level_dir, |
353 | | - verbosity, |
354 | | - failfast, |
355 | | - locals_, |
356 | | - ) |
357 | | - eot_payload: EOTPayloadDict = {"command_type": "execution", "eot": True} |
358 | | - send_post_request(eot_payload, test_run_pipe) |
359 | | - else: |
360 | | - # No test ids received from buffer |
361 | | - cwd = os.path.abspath(start_dir) # noqa: PTH100 |
362 | | - status = TestExecutionStatus.error |
363 | | - payload: ExecutionPayloadDict = { |
364 | | - "cwd": cwd, |
365 | | - "status": status, |
366 | | - "error": "No test ids received from buffer", |
367 | | - "result": None, |
368 | | - } |
369 | | - send_post_request(payload, test_run_pipe) |
370 | | - eot_payload: EOTPayloadDict = {"command_type": "execution", "eot": True} |
371 | | - send_post_request(eot_payload, test_run_pipe) |
372 | | - except json.JSONDecodeError as exc: |
373 | | - msg = "Error: Could not parse test ids from stdin" |
374 | | - print(msg) |
375 | | - raise VSCodeUnittestError(msg) from exc |
| 316 | + # Read the test ids from the file, attempt to delete file afterwords. |
| 317 | + ids_path = pathlib.Path(run_test_ids_pipe) |
| 318 | + test_ids = ids_path.read_text(encoding="utf-8").splitlines() |
| 319 | + print("Received test ids from temp file.") |
| 320 | + try: |
| 321 | + ids_path.unlink() |
| 322 | + except Exception as e: |
| 323 | + print("Error[vscode-pytest]: unable to delete temp file" + str(e)) |
| 324 | + |
| 325 | + except Exception as e: |
| 326 | + # No test ids received from buffer, return error payload |
| 327 | + cwd = pathlib.Path(start_dir).absolute() |
| 328 | + status: TestExecutionStatus = TestExecutionStatus.error |
| 329 | + payload: ExecutionPayloadDict = { |
| 330 | + "cwd": str(cwd), |
| 331 | + "status": status, |
| 332 | + "result": None, |
| 333 | + "error": "No test ids read from temp file," + str(e), |
| 334 | + } |
| 335 | + send_post_request(payload, test_run_pipe) |
| 336 | + |
| 337 | + # If no error occurred, we will have test ids to run. |
| 338 | + if manage_py_path := os.environ.get("MANAGE_PY_PATH"): |
| 339 | + print("MANAGE_PY_PATH env var set, running Django test suite.") |
| 340 | + args = argv[index + 1 :] or [] |
| 341 | + django_execution_runner(manage_py_path, test_ids, args) |
| 342 | + # the django run subprocesses sends the eot payload. |
| 343 | + else: |
| 344 | + # Perform regular unittest execution. |
| 345 | + payload = run_tests( |
| 346 | + start_dir, |
| 347 | + test_ids, |
| 348 | + pattern, |
| 349 | + top_level_dir, |
| 350 | + verbosity, |
| 351 | + failfast, |
| 352 | + locals_, |
| 353 | + ) |
0 commit comments