|
6 | 6 | import os |
7 | 7 | import pathlib |
8 | 8 | import sys |
9 | | -import time |
10 | 9 | import traceback |
11 | 10 |
|
12 | 11 | import pytest |
@@ -56,9 +55,21 @@ def __init__(self, message): |
56 | 55 | IS_DISCOVERY = False |
57 | 56 | map_id_to_path = dict() |
58 | 57 | collected_tests_so_far = list() |
| 58 | +TEST_PORT = os.getenv("TEST_PORT") |
| 59 | +TEST_UUID = os.getenv("TEST_UUID") |
59 | 60 |
|
60 | 61 |
|
61 | 62 | def pytest_load_initial_conftests(early_config, parser, args): |
| 63 | + global TEST_PORT |
| 64 | + global TEST_UUID |
| 65 | + TEST_PORT = os.getenv("TEST_PORT") |
| 66 | + TEST_UUID = os.getenv("TEST_UUID") |
| 67 | + error_string = ( |
| 68 | + "PYTEST ERROR: TEST_UUID and/or TEST_PORT are not set at the time of pytest starting. Please confirm these environment variables are not being" |
| 69 | + " changed or removed as they are required for successful test discovery and execution." |
| 70 | + f" \nTEST_UUID = {TEST_UUID}\nTEST_PORT = {TEST_PORT}\n" |
| 71 | + ) |
| 72 | + print(error_string, file=sys.stderr) |
62 | 73 | if "--collect-only" in args: |
63 | 74 | global IS_DISCOVERY |
64 | 75 | IS_DISCOVERY = True |
@@ -689,58 +700,54 @@ def send_post_request( |
689 | 700 | payload -- the payload data to be sent. |
690 | 701 | cls_encoder -- a custom encoder if needed. |
691 | 702 | """ |
692 | | - testPort = os.getenv("TEST_PORT") |
693 | | - testUuid = os.getenv("TEST_UUID") |
694 | | - if testPort is None: |
695 | | - print( |
696 | | - "Error[vscode-pytest]: TEST_PORT is not set.", |
697 | | - " TEST_UUID = ", |
698 | | - testUuid, |
699 | | - ) |
700 | | - testPort = DEFAULT_PORT |
701 | | - if testUuid is None: |
702 | | - print( |
703 | | - "Error[vscode-pytest]: TEST_UUID is not set.", |
704 | | - " TEST_PORT = ", |
705 | | - testPort, |
| 703 | + global TEST_PORT |
| 704 | + global TEST_UUID |
| 705 | + if TEST_UUID is None or TEST_PORT is None: |
| 706 | + # if TEST_UUID or TEST_PORT is None, print an error and fail as these are both critical errors |
| 707 | + error_msg = ( |
| 708 | + "PYTEST ERROR: TEST_UUID and/or TEST_PORT are not set at the time of pytest starting. Please confirm these environment variables are not being" |
| 709 | + " changed or removed as they are required for successful pytest discovery and execution." |
| 710 | + f" \nTEST_UUID = {TEST_UUID}\nTEST_PORT = {TEST_PORT}\n" |
706 | 711 | ) |
707 | | - testUuid = "unknown" |
708 | | - addr = ("localhost", int(testPort)) |
| 712 | + print(error_msg, file=sys.stderr) |
| 713 | + raise VSCodePytestError(error_msg) |
| 714 | + |
| 715 | + addr = ("localhost", int(TEST_PORT)) |
709 | 716 | global __socket |
710 | 717 |
|
711 | 718 | if __socket is None: |
712 | 719 | try: |
713 | 720 | __socket = socket_manager.SocketManager(addr) |
714 | 721 | __socket.connect() |
715 | 722 | except Exception as error: |
716 | | - print(f"Plugin error connection error[vscode-pytest]: {error}") |
| 723 | + error_msg = f"Error attempting to connect to extension communication socket[vscode-pytest]: {error}" |
| 724 | + print(error_msg, file=sys.stderr) |
| 725 | + print( |
| 726 | + "If you are on a Windows machine, this error may be occurring if any of your tests clear environment variables" |
| 727 | + " as they are required to communicate with the extension. Please reference https://docs.pytest.org/en/stable/how-to/monkeypatch.html#monkeypatching-environment-variables" |
| 728 | + "for the correct way to clear environment variables during testing.\n", |
| 729 | + file=sys.stderr, |
| 730 | + ) |
717 | 731 | __socket = None |
| 732 | + raise VSCodePytestError(error_msg) |
718 | 733 |
|
719 | 734 | data = json.dumps(payload, cls=cls_encoder) |
720 | 735 | request = f"""Content-Length: {len(data)} |
721 | 736 | Content-Type: application/json |
722 | | -Request-uuid: {testUuid} |
| 737 | +Request-uuid: {TEST_UUID} |
723 | 738 |
|
724 | 739 | {data}""" |
725 | 740 |
|
726 | | - max_retries = 3 |
727 | | - retries = 0 |
728 | | - while retries < max_retries: |
729 | | - try: |
730 | | - if __socket is not None and __socket.socket is not None: |
731 | | - __socket.socket.sendall(request.encode("utf-8")) |
732 | | - # print("Post request sent successfully!") |
733 | | - # print("data sent", payload, "end of data") |
734 | | - break # Exit the loop if the send was successful |
735 | | - else: |
736 | | - print("Plugin error connection error[vscode-pytest]") |
737 | | - print(f"[vscode-pytest] data: {request}") |
738 | | - except Exception as error: |
739 | | - print(f"Plugin error connection error[vscode-pytest]: {error}") |
740 | | - print(f"[vscode-pytest] data: {request}") |
741 | | - retries += 1 # Increment retry counter |
742 | | - if retries < max_retries: |
743 | | - print(f"Retrying ({retries}/{max_retries}) in 2 seconds...") |
744 | | - time.sleep(2) # Wait for a short duration before retrying |
745 | | - else: |
746 | | - print("Maximum retry attempts reached. Cannot send post request.") |
| 741 | + try: |
| 742 | + if __socket is not None and __socket.socket is not None: |
| 743 | + __socket.socket.sendall(request.encode("utf-8")) |
| 744 | + else: |
| 745 | + print( |
| 746 | + f"Plugin error connection error[vscode-pytest], socket is None \n[vscode-pytest] data: \n{request} \n", |
| 747 | + file=sys.stderr, |
| 748 | + ) |
| 749 | + except Exception as error: |
| 750 | + print( |
| 751 | + f"Plugin error, exception thrown while attempting to send data[vscode-pytest]: {error} \n[vscode-pytest] data: \n{request}\n", |
| 752 | + file=sys.stderr, |
| 753 | + ) |
0 commit comments