Skip to content

Commit

Permalink
support pytest-forked plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Nov 20, 2024
1 parent 2fed954 commit 1c41760
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
17 changes: 17 additions & 0 deletions python_files/tests/pytestadapter/.data/test_forked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

import pytest
import os


@pytest.mark.forked
def test_forked_process_p():
pid = os.getpid()
print(f"Running in process with PID: {pid}")
assert True
@pytest.mark.forked
def test_forked_process_f():
pid = os.getpid()
print(f"Running in process with PID: {pid}")
assert False
39 changes: 33 additions & 6 deletions python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def __init__(self, message):
collected_tests_so_far = []
TEST_RUN_PIPE = os.getenv("TEST_RUN_PIPE")
SYMLINK_PATH = None
# Get variable set in the `run_pytest_script.py` for the parent process to check if its forked
ROOT_PROCESS_PID: int = os.getenv("PROCESS_ID")

Check failure on line 72 in python_files/vscode_pytest/__init__.py

View workflow job for this annotation

GitHub Actions / Check Python types

Expression of type "str | None" cannot be assigned to declared type "int"   Type "str | None" cannot be assigned to type "int"     "str" is incompatible with "int" (reportGeneralTypeIssues)


def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
Expand All @@ -92,6 +94,24 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
global IS_DISCOVERY
IS_DISCOVERY = True

global ROOT_PROCESS_PID
print(early_config.pluginmanager.list_name_plugin())
if early_config.pluginmanager.has_plugin("pytest_forked"):
if ROOT_PROCESS_PID and int(os.getpid()) == int(ROOT_PROCESS_PID):
print(
"Plugin info[vscode-pytest]: Forked plugin detected but NOT running in forked process."
)
elif ROOT_PROCESS_PID:
print(
f"Plugin info[vscode-pytest]: Forked plugin detected and running in forked process. Root PID: {ROOT_PROCESS_PID}, Current PID: {os.getpid()}"
)
else:
print("Plugin info[vscode-pytest]: No root PID defined, setting to 0.")
ROOT_PROCESS_PID = 0
else:
# If the plugin is not detected, then the root process is the current process.
ROOT_PROCESS_PID = 0

# check if --rootdir is in the args
for arg in args:
if "--rootdir=" in arg:
Expand Down Expand Up @@ -858,12 +878,19 @@ def send_execution_message(
status (Literal["success", "error"]): Execution status indicating success or error.
tests (Union[testRunResultDict, None]): Test run results, if available.
"""
payload: ExecutionPayloadDict = ExecutionPayloadDict(
cwd=cwd, status=status, result=tests, not_found=None, error=None
)
if ERRORS:
payload["error"] = ERRORS
send_message(payload)
current_pid = os.getpid()
# if ROOT_PROCESS_PID is 0 then forked plugin is not enabled
if ROOT_PROCESS_PID != 0 and int(current_pid) != int(ROOT_PROCESS_PID):
print("PID NOT equal to the root proccess, no return", current_pid, ROOT_PROCESS_PID)
return
else:
print("PID equal to the root of forked proccess", current_pid, ROOT_PROCESS_PID)
payload: ExecutionPayloadDict = ExecutionPayloadDict(
cwd=cwd, status=status, result=tests, not_found=None, error=None
)
if ERRORS:
payload["error"] = ERRORS
send_message(payload)


def send_discovery_message(cwd: str, session_node: TestNode) -> None:
Expand Down
1 change: 1 addition & 0 deletions python_files/vscode_pytest/run_pytest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def run_pytest(args):
# args through sys.argv. It then runs pytest.main() with the args and test_ids.

if __name__ == "__main__":
os.environ["PROCESS_ID"] = str(os.getpid())
# Add the root directory to the path so that we can import the plugin.
directory_path = pathlib.Path(__file__).parent.parent
sys.path.append(os.fspath(directory_path))
Expand Down

0 comments on commit 1c41760

Please sign in to comment.