@@ -23,12 +23,16 @@ class RobotVisitorFileImports(ModelVisitor):
2323 Gather file imports
2424 """
2525
26+ root_directory : Path
2627 files : dict [str , FileUseData ]
28+ init_files : dict [Path , FileUseData ]
2729 current_working_file : FileUseData | None = None
2830 current_working_directory : Path | None = None
2931
30- def __init__ (self ) -> None :
32+ def __init__ (self , root_directory : Path ) -> None :
33+ self .root_directory = root_directory .absolute ()
3134 self .files = {}
35+ self .init_files = {}
3236 super ().__init__ ()
3337
3438 def visit_File (self , node : File ): # noqa: N802
@@ -48,21 +52,63 @@ def visit_File(self, node: File): # noqa: N802
4852 # Already found as import
4953 self .current_working_file = self .files [current_file_path_normalized ]
5054 else :
51- self .current_working_file = FileUseData (
52- normalize_file_path (current_working_file ),
53- path_absolute = current_working_file ,
54- type = {file_type },
55- used_by = [],
55+ self .current_working_file = self ._register_file (
56+ current_file_path_normalized ,
57+ file_type ,
58+ current_working_file ,
5659 )
57- self .files [current_file_path_normalized ] = self .current_working_file
5860
5961 return self .generic_visit (node )
6062
63+ def _register_file (
64+ self ,
65+ current_file_path_normalized : str ,
66+ file_type : Literal ["SUITE" , "SUITE_INIT" , "RESOURCE" ],
67+ current_working_file : Path ,
68+ ) -> FileUseData :
69+ """Register a file"""
70+ file = FileUseData (
71+ id = current_file_path_normalized ,
72+ path_absolute = current_working_file ,
73+ type = {file_type },
74+ used_by = [],
75+ )
76+
77+ if file_type == "SUITE_INIT" :
78+ self .init_files [current_working_file .parent ] = file
79+
80+ # Assumption: Due to file path sorting, `__init__` is always processed before any suite
81+ # file it applies to.
82+
83+ if file_type == "SUITE" :
84+ self ._register_use_of_suite_init (file )
85+
86+ self .files [current_file_path_normalized ] = file
87+ return file
88+
89+ def _register_use_of_suite_init (self , file : FileUseData ) -> None :
90+ """Register use of suite __init__ files"""
91+ if len (self .init_files ) == 0 :
92+ return
93+
94+ root_dir_parts_len = len (self .root_directory .parts )
95+
96+ path = file .path_absolute
97+ while len (path .parts ) > root_dir_parts_len :
98+ path = path .parent
99+ init_file = self .init_files .get (path , None )
100+
101+ if init_file :
102+ init_file .used_by .append (file )
103+
61104 def _get_file_type (
62105 self ,
63106 file_node : File ,
64107 file_path : Path ,
65- ) -> Literal ["SUITE" , "RESOURCE" ] | None :
108+ ) -> Literal ["SUITE" , "SUITE_INIT" , "RESOURCE" ] | None :
109+ if file_path .stem == "__init__" :
110+ return "SUITE_INIT"
111+
66112 file_extension = file_path .suffix .lstrip ("." ).lower ()
67113
68114 if file_extension == "robot" :
0 commit comments