Skip to content

Commit 6661ed1

Browse files
committed
fix crash when xdist plugin is not enabled
1 parent 890b6e6 commit 6661ed1

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

python_files/vscode_pytest/__init__.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616
sys.path.append(os.fspath(script_dir / "lib" / "python"))
1717

1818
from testing_tools import socket_manager # noqa: E402
19-
from typing import Any, Dict, Generator, List, Optional, Union, TypedDict, Literal # noqa: E402
19+
from typing import ( # noqa: E402
20+
Any,
21+
Dict,
22+
Generator,
23+
List,
24+
Optional,
25+
Union,
26+
TypedDict,
27+
Literal,
28+
)
2029

2130

2231
class TestData(TypedDict):
@@ -89,7 +98,9 @@ def pytest_load_initial_conftests(early_config, parser, args):
8998
f"Plugin info[vscode-pytest]: rootdir argument, {rootdir}, is identified as a symlink."
9099
)
91100
elif pathlib.Path(os.path.realpath(rootdir)) != rootdir:
92-
print("Plugin info[vscode-pytest]: Checking if rootdir is a child of a symlink.")
101+
print(
102+
"Plugin info[vscode-pytest]: Checking if rootdir is a child of a symlink."
103+
)
93104
isSymlink = has_symlink_parent(rootdir)
94105
if isSymlink:
95106
print(
@@ -125,9 +136,13 @@ def pytest_exception_interact(node, call, report):
125136
if call.excinfo and call.excinfo.typename != "AssertionError":
126137
if report.outcome == "skipped" and "SkipTest" in str(call):
127138
return
128-
ERRORS.append(call.excinfo.exconly() + "\n Check Python Test Logs for more details.")
139+
ERRORS.append(
140+
call.excinfo.exconly() + "\n Check Python Test Logs for more details."
141+
)
129142
else:
130-
ERRORS.append(report.longreprtext + "\n Check Python Test Logs for more details.")
143+
ERRORS.append(
144+
report.longreprtext + "\n Check Python Test Logs for more details."
145+
)
131146
else:
132147
# If during execution, send this data that the given node failed.
133148
report_value = "error"
@@ -448,9 +463,13 @@ def build_test_tree(session: pytest.Session) -> TestNode:
448463
".py", 1
449464
) # splits the file path from the rest of the nodeid
450465

451-
class_and_method = second_split[1] + "::" # This has "::" separator at both ends
466+
class_and_method = (
467+
second_split[1] + "::"
468+
) # This has "::" separator at both ends
452469
# construct the parent id, so it is absolute path :: any class and method :: parent_part
453-
parent_id = os.fspath(get_node_path(test_case)) + class_and_method + parent_part
470+
parent_id = (
471+
os.fspath(get_node_path(test_case)) + class_and_method + parent_part
472+
)
454473
# file, middle, param = test_case.nodeid.rsplit("::", 2)
455474
# parent_id = test_case.nodeid.rsplit("::", 1)[0] + "::" + parent_part
456475
# parent_path = os.fspath(get_node_path(test_case)) + "::" + parent_part
@@ -461,7 +480,9 @@ def build_test_tree(session: pytest.Session) -> TestNode:
461480
ERRORS.append(
462481
f"unable to find original name for {test_case.name} with parameterization detected."
463482
)
464-
raise VSCodePytestError("Unable to find original name for parameterized test case")
483+
raise VSCodePytestError(
484+
"Unable to find original name for parameterized test case"
485+
)
465486
except KeyError:
466487
function_test_node: TestNode = create_parameterized_function_node(
467488
function_name, get_node_path(test_case), parent_id
@@ -510,7 +531,10 @@ def build_test_tree(session: pytest.Session) -> TestNode:
510531
test_file_node = create_file_node(parent_module)
511532
file_nodes_dict[parent_module] = test_file_node
512533
# Check if the class is already a child of the file node.
513-
if test_class_node is not None and test_class_node not in test_file_node["children"]:
534+
if (
535+
test_class_node is not None
536+
and test_class_node not in test_file_node["children"]
537+
):
514538
test_file_node["children"].append(test_class_node)
515539
elif not hasattr(test_case, "callspec"):
516540
# This includes test cases that are pytest functions or a doctests.
@@ -533,7 +557,9 @@ def build_test_tree(session: pytest.Session) -> TestNode:
533557
print(
534558
"[vscode-pytest]: Session path not a parent of test paths, adjusting session node to common parent."
535559
)
536-
common_parent = os.path.commonpath([file_node["path"], get_node_path(session)])
560+
common_parent = os.path.commonpath(
561+
[file_node["path"], get_node_path(session)]
562+
)
537563
common_parent_path = pathlib.Path(common_parent)
538564
print("[vscode-pytest]: Session node now set to: ", common_parent)
539565
session_node["path"] = common_parent_path # pathlib.Path
@@ -583,9 +609,13 @@ def build_nested_folders(
583609
while iterator_path != session_node_path:
584610
curr_folder_name = iterator_path.name
585611
try:
586-
curr_folder_node: TestNode = created_files_folders_dict[os.fspath(iterator_path)]
612+
curr_folder_node: TestNode = created_files_folders_dict[
613+
os.fspath(iterator_path)
614+
]
587615
except KeyError:
588-
curr_folder_node: TestNode = create_folder_node(curr_folder_name, iterator_path)
616+
curr_folder_node: TestNode = create_folder_node(
617+
curr_folder_name, iterator_path
618+
)
589619
created_files_folders_dict[os.fspath(iterator_path)] = curr_folder_node
590620
if prev_folder_node not in curr_folder_node["children"]:
591621
curr_folder_node["children"].append(prev_folder_node)
@@ -884,7 +914,15 @@ def send_post_request(
884914
)
885915

886916

887-
@pytest.hookimpl(hookwrapper=True)
888-
def pytest_xdist_auto_num_workers(config: pytest.Config) -> Generator[None, Result[int], int]:
889-
"""determine how many workers to use based on how many tests were selected in the test explorer"""
890-
return min((yield).get_result(), len(config.option.file_or_dir))
917+
def pytest_configure(config: pytest.Config):
918+
if config.pluginmanager.hasplugin("xdist"):
919+
920+
class XdistHook:
921+
@pytest.hookimpl(hookwrapper=True)
922+
def pytest_xdist_auto_num_workers(
923+
self, config: pytest.Config
924+
) -> Generator[None, Result[int], int]:
925+
"""determine how many workers to use based on how many tests were selected in the test explorer"""
926+
return min((yield).get_result(), len(config.option.file_or_dir))
927+
928+
config.pluginmanager.register(XdistHook())

0 commit comments

Comments
 (0)