@@ -57,6 +57,7 @@ def __init__(self, message):
57
57
collected_tests_so_far = list ()
58
58
TEST_PORT = os .getenv ("TEST_PORT" )
59
59
TEST_UUID = os .getenv ("TEST_UUID" )
60
+ SYMLINK_PATH = None
60
61
61
62
62
63
def pytest_load_initial_conftests (early_config , parser , args ):
@@ -75,6 +76,25 @@ def pytest_load_initial_conftests(early_config, parser, args):
75
76
global IS_DISCOVERY
76
77
IS_DISCOVERY = True
77
78
79
+ # check if --rootdir is in the args
80
+ for arg in args :
81
+ if "--rootdir=" in arg :
82
+ rootdir = arg .split ("--rootdir=" )[1 ]
83
+ if not os .path .exists (rootdir ):
84
+ raise VSCodePytestError (
85
+ f"The path set in the argument --rootdir={ rootdir } does not exist."
86
+ )
87
+ if (
88
+ os .path .islink (rootdir )
89
+ and pathlib .Path (os .path .realpath (rootdir )) == pathlib .Path .cwd ()
90
+ ):
91
+ print (
92
+ f"Plugin info[vscode-pytest]: rootdir argument, { rootdir } , is identified as a symlink to the cwd, { pathlib .Path .cwd ()} ." ,
93
+ "Therefore setting symlink path to rootdir argument." ,
94
+ )
95
+ global SYMLINK_PATH
96
+ SYMLINK_PATH = pathlib .Path (rootdir )
97
+
78
98
79
99
def pytest_internalerror (excrepr , excinfo ):
80
100
"""A pytest hook that is called when an internal error occurs.
@@ -326,6 +346,13 @@ def pytest_sessionfinish(session, exitstatus):
326
346
Exit code 5: No tests were collected
327
347
"""
328
348
cwd = pathlib .Path .cwd ()
349
+ if SYMLINK_PATH :
350
+ print ("Plugin warning[vscode-pytest]: SYMLINK set, adjusting cwd." )
351
+ # Get relative between the cwd (resolved path) and the node path.
352
+ rel_path = os .path .relpath (cwd , pathlib .Path .cwd ())
353
+ # Calculate the new node path by making it relative to the symlink path.
354
+ cwd = pathlib .Path (os .path .join (SYMLINK_PATH , rel_path ))
355
+
329
356
if IS_DISCOVERY :
330
357
if not (exitstatus == 0 or exitstatus == 1 or exitstatus == 5 ):
331
358
errorNode : TestNode = {
@@ -388,6 +415,11 @@ def build_test_tree(session: pytest.Session) -> TestNode:
388
415
class_nodes_dict : Dict [str , TestNode ] = {}
389
416
function_nodes_dict : Dict [str , TestNode ] = {}
390
417
418
+ # Check to see if the global variable for symlink path is set
419
+ if SYMLINK_PATH :
420
+ session_node ["path" ] = SYMLINK_PATH
421
+ session_node ["id_" ] = os .fspath (SYMLINK_PATH )
422
+
391
423
for test_case in session .items :
392
424
test_node = create_test_node (test_case )
393
425
if isinstance (test_case .parent , pytest .Class ):
@@ -645,13 +677,31 @@ class EOTPayloadDict(TypedDict):
645
677
646
678
647
679
def get_node_path (node : Any ) -> pathlib .Path :
648
- """A function that returns the path of a node given the switch to pathlib.Path."""
680
+ """
681
+ A function that returns the path of a node given the switch to pathlib.Path.
682
+ It also evaluates if the node is a symlink and returns the equivalent path.
683
+ """
649
684
path = getattr (node , "path" , None ) or pathlib .Path (node .fspath )
650
685
651
686
if not path :
652
687
raise VSCodePytestError (
653
688
f"Unable to find path for node: { node } , node.path: { node .path } , node.fspath: { node .fspath } "
654
689
)
690
+
691
+ # Check for the session node since it has the symlink already.
692
+ if SYMLINK_PATH and not isinstance (node , pytest .Session ):
693
+ # Get relative between the cwd (resolved path) and the node path.
694
+ try :
695
+ rel_path = path .relative_to (pathlib .Path .cwd ())
696
+
697
+ # Calculate the new node path by making it relative to the symlink path.
698
+ sym_path = pathlib .Path (os .path .join (SYMLINK_PATH , rel_path ))
699
+ return sym_path
700
+ except Exception as e :
701
+ raise VSCodePytestError (
702
+ f"Error occurred while calculating symlink equivalent from node path: { e } "
703
+ "\n SYMLINK_PATH: {SYMLINK_PATH}, \n node path: {path}, \n cwd: {{pathlib.Path.cwd()}}"
704
+ )
655
705
return path
656
706
657
707
@@ -687,7 +737,6 @@ def post_response(cwd: str, session_node: TestNode) -> None:
687
737
cwd (str): Current working directory.
688
738
session_node (TestNode): Node information of the test session.
689
739
"""
690
-
691
740
payload : DiscoveryPayloadDict = {
692
741
"cwd" : cwd ,
693
742
"status" : "success" if not ERRORS else "error" ,
0 commit comments