@@ -509,9 +509,25 @@ def build_test_tree(session: pytest.Session) -> TestNode:
509509 created_files_folders_dict : Dict [str , TestNode ] = {}
510510 for _ , file_node in file_nodes_dict .items ():
511511 # Iterate through all the files that exist and construct them into nested folders.
512- root_folder_node : TestNode = build_nested_folders (
513- file_node , created_files_folders_dict , session
514- )
512+ root_folder_node : TestNode
513+ try :
514+ root_folder_node : TestNode = build_nested_folders (
515+ file_node , created_files_folders_dict , session_node
516+ )
517+ except ValueError :
518+ # This exception is raised when the session node is not a parent of the file node.
519+ print (
520+ "[vscode-pytest]: Session path not a parent of test paths, adjusting session node to common parent."
521+ )
522+ common_parent = os .path .commonpath ([file_node ["path" ], get_node_path (session )])
523+ common_parent_path = pathlib .Path (common_parent )
524+ print ("[vscode-pytest]: Session node now set to: " , common_parent )
525+ session_node ["path" ] = common_parent_path # pathlib.Path
526+ session_node ["id_" ] = common_parent # str
527+ session_node ["name" ] = common_parent_path .name # str
528+ root_folder_node = build_nested_folders (
529+ file_node , created_files_folders_dict , session_node
530+ )
515531 # The final folder we get to is the highest folder in the path
516532 # and therefore we add this as a child to the session.
517533 root_id = root_folder_node .get ("id_" )
@@ -524,7 +540,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
524540def build_nested_folders (
525541 file_node : TestNode ,
526542 created_files_folders_dict : Dict [str , TestNode ],
527- session : pytest . Session ,
543+ session_node : TestNode ,
528544) -> TestNode :
529545 """Takes a file or folder and builds the nested folder structure for it.
530546
@@ -534,11 +550,23 @@ def build_nested_folders(
534550 created_files_folders_dict -- Dictionary of all the folders and files that have been created where the key is the path.
535551 session -- the pytest session object.
536552 """
537- prev_folder_node = file_node
553+ # check if session node is a parent of the file node, throw error if not.
554+ session_node_path = session_node ["path" ]
555+ is_relative = False
556+ try :
557+ is_relative = file_node ["path" ].is_relative_to (session_node_path )
558+ except AttributeError :
559+ is_relative = file_node ["path" ].relative_to (session_node_path )
560+ if not is_relative :
561+ # If the session node is not a parent of the file node, we need to find their common parent.
562+ raise ValueError ("session and file not relative to each other, fixing now...." )
538563
539564 # Begin the iterator_path one level above the current file.
565+ prev_folder_node = file_node
540566 iterator_path = file_node ["path" ].parent
541- while iterator_path != get_node_path (session ):
567+ counter = 0
568+ max_iter = 100
569+ while iterator_path != session_node_path :
542570 curr_folder_name = iterator_path .name
543571 try :
544572 curr_folder_node : TestNode = created_files_folders_dict [os .fspath (iterator_path )]
@@ -549,6 +577,15 @@ def build_nested_folders(
549577 curr_folder_node ["children" ].append (prev_folder_node )
550578 iterator_path = iterator_path .parent
551579 prev_folder_node = curr_folder_node
580+ # Handles error where infinite loop occurs.
581+ counter += 1
582+ if counter > max_iter :
583+ raise ValueError (
584+ "[vscode-pytest]: Infinite loop occurred in build_nested_folders. iterator_path: " ,
585+ iterator_path ,
586+ "session_node_path: " ,
587+ session_node_path ,
588+ )
552589 return prev_folder_node
553590
554591
0 commit comments