Skip to content

Commit f37f883

Browse files
authored
Fix path for pytest Nodes (#21548)
Fixes #21540
1 parent af7eb56 commit f37f883

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

pythonFiles/vscode_pytest/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
290290
# parameterized test cases cut the repetitive part of the name off.
291291
name_split = test_node["name"].split("[")
292292
test_node["name"] = "[" + name_split[1]
293-
parent_path = os.fspath(test_case.path) + "::" + name_split[0]
293+
parent_path = os.fspath(get_node_path(test_case)) + "::" + name_split[0]
294294
try:
295295
function_name = test_case.originalname # type: ignore
296296
function_test_case = function_nodes_dict[parent_path]
@@ -303,7 +303,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
303303
)
304304
except KeyError:
305305
function_test_case: TestNode = create_parameterized_function_node(
306-
function_name, test_case.path, test_case.nodeid
306+
function_name, get_node_path(test_case), test_case.nodeid
307307
)
308308
function_nodes_dict[parent_path] = function_test_case
309309
function_test_case["children"].append(test_node)
@@ -354,7 +354,7 @@ def build_nested_folders(
354354

355355
# Begin the iterator_path one level above the current file.
356356
iterator_path = file_node["path"].parent
357-
while iterator_path != session.path:
357+
while iterator_path != get_node_path(session):
358358
curr_folder_name = iterator_path.name
359359
try:
360360
curr_folder_node: TestNode = created_files_folders_dict[
@@ -385,7 +385,7 @@ def create_test_node(
385385
)
386386
return {
387387
"name": test_case.name,
388-
"path": test_case.path,
388+
"path": get_node_path(test_case),
389389
"lineno": test_case_loc,
390390
"type_": "test",
391391
"id_": test_case.nodeid,
@@ -399,13 +399,13 @@ def create_session_node(session: pytest.Session) -> TestNode:
399399
Keyword arguments:
400400
session -- the pytest session.
401401
"""
402-
session_path = session.path if session.path else pathlib.Path.cwd()
402+
node_path = get_node_path(session)
403403
return {
404404
"name": session.name,
405-
"path": session_path,
405+
"path": node_path,
406406
"type_": "folder",
407407
"children": [],
408-
"id_": os.fspath(session.path),
408+
"id_": os.fspath(node_path),
409409
}
410410

411411

@@ -417,7 +417,7 @@ def create_class_node(class_module: pytest.Class) -> TestNode:
417417
"""
418418
return {
419419
"name": class_module.name,
420-
"path": class_module.path,
420+
"path": get_node_path(class_module),
421421
"type_": "class",
422422
"children": [],
423423
"id_": class_module.nodeid,
@@ -451,11 +451,12 @@ def create_file_node(file_module: Any) -> TestNode:
451451
Keyword arguments:
452452
file_module -- the pytest file module.
453453
"""
454+
node_path = get_node_path(file_module)
454455
return {
455-
"name": file_module.path.name,
456-
"path": file_module.path,
456+
"name": node_path.name,
457+
"path": node_path,
457458
"type_": "file",
458-
"id_": os.fspath(file_module.path),
459+
"id_": os.fspath(node_path),
459460
"children": [],
460461
}
461462

@@ -497,6 +498,10 @@ class ExecutionPayloadDict(Dict):
497498
error: Union[str, None] # Currently unused need to check
498499

499500

501+
def get_node_path(node: Any) -> pathlib.Path:
502+
return getattr(node, "path", pathlib.Path(node.fspath))
503+
504+
500505
def execution_post(
501506
cwd: str,
502507
status: Literal["success", "error"],

0 commit comments

Comments
 (0)