@@ -148,3 +148,99 @@ def _set_perms(path: Path, perms: int):
148148
149149
150150# ---> КОНЕЦ: Перенесенная фикстура set_perms <---
151+
152+
153+ # --- New fixtures for test modernization ---
154+
155+
156+ @pytest .fixture
157+ def project_builder (tmp_path ):
158+ """Builder pattern for creating test project structures."""
159+ from typing import List
160+
161+ class ProjectBuilder :
162+ def __init__ (self , base_path : Path ):
163+ self .root = base_path / "treemapper_test_project"
164+ self .root .mkdir ()
165+
166+ def add_file (self , path : str , content : str = "" ) -> Path :
167+ file_path = self .root / path
168+ file_path .parent .mkdir (parents = True , exist_ok = True )
169+ file_path .write_text (content , encoding = "utf-8" )
170+ return file_path
171+
172+ def add_binary (self , path : str , content : bytes = b"\x00 \x01 \x02 " ) -> Path :
173+ file_path = self .root / path
174+ file_path .parent .mkdir (parents = True , exist_ok = True )
175+ file_path .write_bytes (content )
176+ return file_path
177+
178+ def add_dir (self , path : str ) -> Path :
179+ dir_path = self .root / path
180+ dir_path .mkdir (parents = True , exist_ok = True )
181+ return dir_path
182+
183+ def add_gitignore (self , patterns : List [str ], subdir : str = "" ) -> Path :
184+ path = self .root / subdir / ".gitignore" if subdir else self .root / ".gitignore"
185+ path .parent .mkdir (parents = True , exist_ok = True )
186+ path .write_text ("\n " .join (patterns ) + "\n " , encoding = "utf-8" )
187+ return path
188+
189+ def add_treemapperignore (self , patterns : List [str ]) -> Path :
190+ path = self .root / ".treemapperignore"
191+ path .write_text ("\n " .join (patterns ) + "\n " , encoding = "utf-8" )
192+ return path
193+
194+ def create_nested (self , depth : int , files_per_level : int = 1 ) -> None :
195+ current = self .root
196+ for i in range (depth ):
197+ current = current / f"level{ i } "
198+ current .mkdir (exist_ok = True )
199+ for j in range (files_per_level ):
200+ (current / f"file{ j } .txt" ).write_text (f"Content { i } -{ j } " )
201+
202+ return ProjectBuilder (tmp_path )
203+
204+
205+ @pytest .fixture
206+ def cli_runner (temp_project ):
207+ """Simplified CLI runner with automatic success assertion."""
208+
209+ def _run (args , cwd = None , expect_success = True ):
210+ result = run_treemapper_subprocess (args , cwd = cwd or temp_project )
211+ if expect_success :
212+ assert result .returncode == 0 , f"CLI failed with stderr: { result .stderr } "
213+ return result
214+
215+ return _run
216+
217+
218+ @pytest .fixture
219+ def run_and_verify (run_mapper , temp_project ):
220+ """Run mapper and verify tree structure."""
221+ from tests .utils import get_all_files_in_tree , load_yaml
222+
223+ def _run (
224+ args = None ,
225+ expected_files = None ,
226+ excluded_files = None ,
227+ output_name = "output.yaml" ,
228+ ):
229+ output_path = temp_project / output_name
230+ full_args = ["." ] + (args or []) + ["-o" , str (output_path )]
231+ success = run_mapper (full_args )
232+ assert success , f"Mapper failed with args: { full_args } "
233+
234+ result = load_yaml (output_path )
235+ all_files = get_all_files_in_tree (result )
236+
237+ if expected_files :
238+ for f in expected_files :
239+ assert f in all_files , f"Expected file '{ f } ' not found in tree"
240+ if excluded_files :
241+ for f in excluded_files :
242+ assert f not in all_files , f"File '{ f } ' should be excluded from tree"
243+
244+ return result
245+
246+ return _run
0 commit comments