Skip to content

Commit 1c41760

Browse files
committed
support pytest-forked plugin
1 parent 2fed954 commit 1c41760

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
import pytest
5+
import os
6+
7+
8+
@pytest.mark.forked
9+
def test_forked_process_p():
10+
pid = os.getpid()
11+
print(f"Running in process with PID: {pid}")
12+
assert True
13+
@pytest.mark.forked
14+
def test_forked_process_f():
15+
pid = os.getpid()
16+
print(f"Running in process with PID: {pid}")
17+
assert False

python_files/vscode_pytest/__init__.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def __init__(self, message):
6868
collected_tests_so_far = []
6969
TEST_RUN_PIPE = os.getenv("TEST_RUN_PIPE")
7070
SYMLINK_PATH = None
71+
# Get variable set in the `run_pytest_script.py` for the parent process to check if its forked
72+
ROOT_PROCESS_PID: int = os.getenv("PROCESS_ID")
7173

7274

7375
def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
@@ -92,6 +94,24 @@ def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001
9294
global IS_DISCOVERY
9395
IS_DISCOVERY = True
9496

97+
global ROOT_PROCESS_PID
98+
print(early_config.pluginmanager.list_name_plugin())
99+
if early_config.pluginmanager.has_plugin("pytest_forked"):
100+
if ROOT_PROCESS_PID and int(os.getpid()) == int(ROOT_PROCESS_PID):
101+
print(
102+
"Plugin info[vscode-pytest]: Forked plugin detected but NOT running in forked process."
103+
)
104+
elif ROOT_PROCESS_PID:
105+
print(
106+
f"Plugin info[vscode-pytest]: Forked plugin detected and running in forked process. Root PID: {ROOT_PROCESS_PID}, Current PID: {os.getpid()}"
107+
)
108+
else:
109+
print("Plugin info[vscode-pytest]: No root PID defined, setting to 0.")
110+
ROOT_PROCESS_PID = 0
111+
else:
112+
# If the plugin is not detected, then the root process is the current process.
113+
ROOT_PROCESS_PID = 0
114+
95115
# check if --rootdir is in the args
96116
for arg in args:
97117
if "--rootdir=" in arg:
@@ -858,12 +878,19 @@ def send_execution_message(
858878
status (Literal["success", "error"]): Execution status indicating success or error.
859879
tests (Union[testRunResultDict, None]): Test run results, if available.
860880
"""
861-
payload: ExecutionPayloadDict = ExecutionPayloadDict(
862-
cwd=cwd, status=status, result=tests, not_found=None, error=None
863-
)
864-
if ERRORS:
865-
payload["error"] = ERRORS
866-
send_message(payload)
881+
current_pid = os.getpid()
882+
# if ROOT_PROCESS_PID is 0 then forked plugin is not enabled
883+
if ROOT_PROCESS_PID != 0 and int(current_pid) != int(ROOT_PROCESS_PID):
884+
print("PID NOT equal to the root proccess, no return", current_pid, ROOT_PROCESS_PID)
885+
return
886+
else:
887+
print("PID equal to the root of forked proccess", current_pid, ROOT_PROCESS_PID)
888+
payload: ExecutionPayloadDict = ExecutionPayloadDict(
889+
cwd=cwd, status=status, result=tests, not_found=None, error=None
890+
)
891+
if ERRORS:
892+
payload["error"] = ERRORS
893+
send_message(payload)
867894

868895

869896
def send_discovery_message(cwd: str, session_node: TestNode) -> None:

python_files/vscode_pytest/run_pytest_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def run_pytest(args):
2828
# args through sys.argv. It then runs pytest.main() with the args and test_ids.
2929

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

0 commit comments

Comments
 (0)