Skip to content

Commit

Permalink
Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Oct 8, 2024
1 parent 2de7950 commit f44ff1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
49 changes: 16 additions & 33 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@

import pytest


# sys.path.append("/Users/eleanorboyd/vscode-python/.nox/install_python_libs/lib/python3.10")
# sys.path.append("/Users/eleanorboyd/vscode-python-debugger")
# sys.path.append("/Users/eleanorboyd/vscode-python-debugger/bundled")
# sys.path.append("/Users/eleanorboyd/vscode-python-debugger/bundled/libs")

# import debugpy # noqa: E402

# debugpy.connect(5678)
# debugpy.breakpoint() # noqa: E702

if TYPE_CHECKING:
from pluggy import Result

Expand Down Expand Up @@ -161,7 +150,7 @@ def pytest_exception_interact(node, call, report):
collected_test = TestRunResultDict()
collected_test[node_id] = item_result
cwd = pathlib.Path.cwd()
execution_post(
send_execution_message(
os.fsdecode(cwd),
"success",
collected_test if collected_test else None,
Expand Down Expand Up @@ -285,7 +274,7 @@ def pytest_report_teststatus(report, config): # noqa: ARG001
)
collected_test = TestRunResultDict()
collected_test[absolute_node_id] = item_result
execution_post(
send_execution_message(
os.fsdecode(cwd),
"success",
collected_test if collected_test else None,
Expand Down Expand Up @@ -319,7 +308,7 @@ def pytest_runtest_protocol(item, nextitem): # noqa: ARG001
)
collected_test = TestRunResultDict()
collected_test[absolute_node_id] = item_result
execution_post(
send_execution_message(
os.fsdecode(cwd),
"success",
collected_test if collected_test else None,
Expand Down Expand Up @@ -390,15 +379,15 @@ def pytest_sessionfinish(session, exitstatus):
"children": [],
"id_": "",
}
post_response(os.fsdecode(cwd), error_node)
send_discovery_message(os.fsdecode(cwd), error_node)
try:
session_node: TestNode | None = build_test_tree(session)
if not session_node:
raise VSCodePytestError(
"Something went wrong following pytest finish, \
no session node was created"
)
post_response(os.fsdecode(cwd), session_node)
send_discovery_message(os.fsdecode(cwd), session_node)
except Exception as e:
ERRORS.append(
f"Error Occurred, traceback: {(traceback.format_exc() if e.__traceback__ else '')}"
Expand All @@ -410,7 +399,7 @@ def pytest_sessionfinish(session, exitstatus):
"children": [],
"id_": "",
}
post_response(os.fsdecode(cwd), error_node)
send_discovery_message(os.fsdecode(cwd), error_node)
else:
if exitstatus == 0 or exitstatus == 1:
exitstatus_bool = "success"
Expand All @@ -420,15 +409,15 @@ def pytest_sessionfinish(session, exitstatus):
)
exitstatus_bool = "error"

execution_post(
send_execution_message(
os.fsdecode(cwd),
exitstatus_bool,
None,
)
# send end of transmission token
command_type = "discovery" if IS_DISCOVERY else "execution"
payload: EOTPayloadDict = {"command_type": command_type, "eot": True}
send_post_request(payload)
send_message(payload)


def build_test_tree(session: pytest.Session) -> TestNode:
Expand Down Expand Up @@ -790,8 +779,10 @@ def get_node_path(node: Any) -> pathlib.Path:
atexit.register(lambda: __writer.close() if __writer else None)


def execution_post(cwd: str, status: Literal["success", "error"], tests: TestRunResultDict | None):
"""Sends a POST request with execution payload details.
def send_execution_message(
cwd: str, status: Literal["success", "error"], tests: TestRunResultDict | None
):
"""Sends message execution payload details.
Args:
cwd (str): Current working directory.
Expand All @@ -803,10 +794,10 @@ def execution_post(cwd: str, status: Literal["success", "error"], tests: TestRun
)
if ERRORS:
payload["error"] = ERRORS
send_post_request(payload)
send_message(payload)


def post_response(cwd: str, session_node: TestNode) -> None:
def send_discovery_message(cwd: str, session_node: TestNode) -> None:
"""
Sends a POST request with test session details in payload.
Expand All @@ -822,7 +813,7 @@ def post_response(cwd: str, session_node: TestNode) -> None:
}
if ERRORS is not None:
payload["error"] = ERRORS
send_post_request(payload, cls_encoder=PathEncoder)
send_message(payload, cls_encoder=PathEncoder)


class PathEncoder(json.JSONEncoder):
Expand All @@ -834,7 +825,7 @@ def default(self, obj):
return super().default(obj)


def send_post_request(
def send_message(
payload: ExecutionPayloadDict | DiscoveryPayloadDict | EOTPayloadDict,
cls_encoder=None,
):
Expand All @@ -845,7 +836,6 @@ def send_post_request(
payload -- the payload data to be sent.
cls_encoder -- a custom encoder if needed.
"""
print("EJFB into send post request!")
if not TEST_RUN_PIPE:
error_msg = (
"PYTEST ERROR: TEST_RUN_PIPE is not set at the time of pytest starting. "
Expand All @@ -860,10 +850,8 @@ def send_post_request(

if __writer is None:
try:
print("EJFB attemping writer open")
__writer = open(TEST_RUN_PIPE, "w", encoding="utf-8", newline="\r\n") # noqa: SIM115, PTH123
except Exception as error:
print("EJFB error in writer open")
error_msg = f"Error attempting to connect to extension named pipe {TEST_RUN_PIPE}[vscode-pytest]: {error}"
print(error_msg, file=sys.stderr)
print(
Expand All @@ -880,16 +868,11 @@ def send_post_request(
"params": payload,
}
data = json.dumps(rpc, cls=cls_encoder)
print(f"EJFB Plugin info[vscode-pytest]: sending data: \n{data}\n")
try:
if __writer:
request = f"""content-length: {len(data)}\ncontent-type: application/json\n\n{data}"""
__writer.write(request)
__writer.flush()
print(
f"EJFB Plugin info[vscode-pytest]: data sent successfully[vscode-pytest]: \n{data}\n"
)
# __writer.close()
else:
print(
f"Plugin error connection error[vscode-pytest], writer is None \n[vscode-pytest] data: \n{data} \n",
Expand Down
2 changes: 1 addition & 1 deletion src/client/common/pipes/namedPipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export async function createReaderPipe(pipeName: string, token?: CancellationTok
deferred.resolve(combined);
return deferred.promise;
}
// linux implementation of FIFO
// mac/linux implementation of FIFO
await mkfifo(pipeName);
const reader = fs.createReadStream(pipeName, {
encoding: 'utf-8',
Expand Down

0 comments on commit f44ff1f

Please sign in to comment.