1414
1515import pytest
1616
17- script_dir = pathlib .Path (__file__ ).parent .parent
18- sys .path .append (os .fspath (script_dir ))
19- sys .path .append (os .fspath (script_dir / "lib" / "python" ))
20- from testing_tools import socket_manager # noqa: E402
21-
2217if TYPE_CHECKING :
2318 from pluggy import Result
2419
@@ -164,7 +159,7 @@ def pytest_exception_interact(node, call, report):
164159 collected_test = TestRunResultDict ()
165160 collected_test [node_id ] = item_result
166161 cwd = pathlib .Path .cwd ()
167- execution_post (
162+ send_execution_message (
168163 os .fsdecode (cwd ),
169164 "success" ,
170165 collected_test if collected_test else None ,
@@ -288,7 +283,7 @@ def pytest_report_teststatus(report, config): # noqa: ARG001
288283 )
289284 collected_test = TestRunResultDict ()
290285 collected_test [absolute_node_id ] = item_result
291- execution_post (
286+ send_execution_message (
292287 os .fsdecode (cwd ),
293288 "success" ,
294289 collected_test if collected_test else None ,
@@ -322,7 +317,7 @@ def pytest_runtest_protocol(item, nextitem): # noqa: ARG001
322317 )
323318 collected_test = TestRunResultDict ()
324319 collected_test [absolute_node_id ] = item_result
325- execution_post (
320+ send_execution_message (
326321 os .fsdecode (cwd ),
327322 "success" ,
328323 collected_test if collected_test else None ,
@@ -398,15 +393,15 @@ def pytest_sessionfinish(session, exitstatus):
398393 "children" : [],
399394 "id_" : "" ,
400395 }
401- post_response (os .fsdecode (cwd ), error_node )
396+ send_discovery_message (os .fsdecode (cwd ), error_node )
402397 try :
403398 session_node : TestNode | None = build_test_tree (session )
404399 if not session_node :
405400 raise VSCodePytestError (
406401 "Something went wrong following pytest finish, \
407402 no session node was created"
408403 )
409- post_response (os .fsdecode (cwd ), session_node )
404+ send_discovery_message (os .fsdecode (cwd ), session_node )
410405 except Exception as e :
411406 ERRORS .append (
412407 f"Error Occurred, traceback: { (traceback .format_exc () if e .__traceback__ else '' )} "
@@ -418,7 +413,7 @@ def pytest_sessionfinish(session, exitstatus):
418413 "children" : [],
419414 "id_" : "" ,
420415 }
421- post_response (os .fsdecode (cwd ), error_node )
416+ send_discovery_message (os .fsdecode (cwd ), error_node )
422417 else :
423418 if exitstatus == 0 or exitstatus == 1 :
424419 exitstatus_bool = "success"
@@ -428,7 +423,7 @@ def pytest_sessionfinish(session, exitstatus):
428423 )
429424 exitstatus_bool = "error"
430425
431- execution_post (
426+ send_execution_message (
432427 os .fsdecode (cwd ),
433428 exitstatus_bool ,
434429 None ,
@@ -482,7 +477,7 @@ def pytest_sessionfinish(session, exitstatus):
482477 result = file_coverage_map ,
483478 error = None ,
484479 )
485- send_post_request (payload )
480+ send_message (payload )
486481
487482
488483def build_test_tree (session : pytest .Session ) -> TestNode :
@@ -591,19 +586,8 @@ def build_test_tree(session: pytest.Session) -> TestNode:
591586 if test_class_node is not None and test_class_node not in test_file_node ["children" ]:
592587 test_file_node ["children" ].append (test_class_node )
593588 elif not hasattr (test_case , "callspec" ):
589+ # This includes test cases that are pytest functions or a doctests.
594590 parent_path = get_node_path (test_case .parent )
595- # # This includes test cases that are pytest functions or a doctests. got here with ruff test
596- # is_ruff = False
597- # # 'script_a.py::ruff'
598- # for mark in test_case.own_markers:
599- # if mark.name == "ruff":
600- # # This is a ruff test, we do not want to include this in the tree.
601- # print("[vscode-pytest]: Skipping ruff test: ", test_case.nodeid)
602- # is_ruff = True
603- # if is_ruff:
604- # # cast RuffFile type to pytest.File
605- # print("is_ruff true")
606- # # parent_case = pytest.Module.from_parent(test_case.parent)
607591 try :
608592 parent_test_case = file_nodes_dict [os .fspath (parent_path )]
609593 except KeyError :
@@ -879,8 +863,10 @@ def get_node_path(node: Any) -> pathlib.Path:
879863atexit .register (lambda : __writer .close () if __writer else None )
880864
881865
882- def execution_post (cwd : str , status : Literal ["success" , "error" ], tests : TestRunResultDict | None ):
883- """Sends a POST request with execution payload details.
866+ def send_execution_message (
867+ cwd : str , status : Literal ["success" , "error" ], tests : TestRunResultDict | None
868+ ):
869+ """Sends message execution payload details.
884870
885871 Args:
886872 cwd (str): Current working directory.
@@ -892,10 +878,10 @@ def execution_post(cwd: str, status: Literal["success", "error"], tests: TestRun
892878 )
893879 if ERRORS :
894880 payload ["error" ] = ERRORS
895- send_post_request (payload )
881+ send_message (payload )
896882
897883
898- def post_response (cwd : str , session_node : TestNode ) -> None :
884+ def send_discovery_message (cwd : str , session_node : TestNode ) -> None :
899885 """
900886 Sends a POST request with test session details in payload.
901887
@@ -911,7 +897,7 @@ def post_response(cwd: str, session_node: TestNode) -> None:
911897 }
912898 if ERRORS is not None :
913899 payload ["error" ] = ERRORS
914- send_post_request (payload , cls_encoder = PathEncoder )
900+ send_message (payload , cls_encoder = PathEncoder )
915901
916902
917903class PathEncoder (json .JSONEncoder ):
@@ -923,7 +909,7 @@ def default(self, o):
923909 return super ().default (o )
924910
925911
926- def send_post_request (
912+ def send_message (
927913 payload : ExecutionPayloadDict | DiscoveryPayloadDict | CoveragePayloadDict ,
928914 cls_encoder = None ,
929915):
@@ -948,8 +934,7 @@ def send_post_request(
948934
949935 if __writer is None :
950936 try :
951- __writer = socket_manager .PipeManager (TEST_RUN_PIPE )
952- __writer .connect ()
937+ __writer = open (TEST_RUN_PIPE , "wb" ) # noqa: SIM115, PTH123
953938 except Exception as error :
954939 error_msg = f"Error attempting to connect to extension named pipe { TEST_RUN_PIPE } [vscode-pytest]: { error } "
955940 print (error_msg , file = sys .stderr )
@@ -967,10 +952,18 @@ def send_post_request(
967952 "params" : payload ,
968953 }
969954 data = json .dumps (rpc , cls = cls_encoder )
970-
971955 try :
972956 if __writer :
973- __writer .write (data )
957+ request = (
958+ f"""content-length: { len (data )} \r \n content-type: application/json\r \n \r \n { data } """
959+ )
960+ size = 4096
961+ encoded = request .encode ("utf-8" )
962+ bytes_written = 0
963+ while bytes_written < len (encoded ):
964+ segment = encoded [bytes_written : bytes_written + size ]
965+ bytes_written += __writer .write (segment )
966+ __writer .flush ()
974967 else :
975968 print (
976969 f"Plugin error connection error[vscode-pytest], writer is None \n [vscode-pytest] data: \n { data } \n " ,
0 commit comments