1414# limitations under the License.
1515from __future__ import annotations
1616
17- import os
18- import tempfile
1917from pathlib import Path
18+ import tempfile
2019from unittest .mock import MagicMock , patch
2120
2221import pytest
@@ -34,7 +33,7 @@ class TestDefaultFindFilePath:
3433 def test_init_with_defaults (self ):
3534 """Test initialization with default values."""
3635 finder = DefaultFindFilePath ()
37-
36+
3837 assert finder .code_package == "payload"
3938 assert finder .file_folder == "files"
4039 assert finder .config_file == "config.json"
@@ -44,254 +43,264 @@ def test_init_with_custom_values(self):
4443 finder = DefaultFindFilePath (
4544 code_package = "custom_package" ,
4645 file_folder = "custom_files" ,
47- config_file = "custom_config.json"
46+ config_file = "custom_config.json" ,
4847 )
49-
48+
5049 assert finder .code_package == "custom_package"
5150 assert finder .file_folder == "custom_files"
5251 assert finder .config_file == "custom_config.json"
5352
5453 def test_find_file_path_empty_filename (self ):
5554 """Test find_file_path with empty filename raises ValueError."""
5655 finder = DefaultFindFilePath ()
57-
56+
5857 with pytest .raises (ValueError , match = "file_name cannot be empty" ):
5958 finder .find_file_path ("" )
60-
59+
6160 with pytest .raises (ValueError , match = "file_name cannot be empty" ):
6261 finder .find_file_path (None )
6362
6463 def test_find_file_path_file_not_found (self ):
6564 """Test find_file_path when file doesn't exist raises FileNotFoundError."""
6665 finder = DefaultFindFilePath ()
67-
68- with patch .object (finder , ' _resolve_file_path' ) as mock_resolve :
66+
67+ with patch .object (finder , " _resolve_file_path" ) as mock_resolve :
6968 mock_path = MagicMock ()
7069 mock_path .exists .return_value = False
7170 mock_resolve .return_value = mock_path
72-
73- with pytest .raises (FileNotFoundError , match = "File 'test.txt' not found in any search location" ):
71+
72+ with pytest .raises (
73+ FileNotFoundError ,
74+ match = "File 'test.txt' not found in any search location" ,
75+ ):
7476 finder .find_file_path ("test.txt" )
7577
7678 def test_find_file_path_success (self ):
7779 """Test find_file_path when file exists returns Path."""
7880 finder = DefaultFindFilePath ()
79-
80- with patch .object (finder , ' _resolve_file_path' ) as mock_resolve :
81+
82+ with patch .object (finder , " _resolve_file_path" ) as mock_resolve :
8183 mock_path = MagicMock ()
8284 mock_path .exists .return_value = True
8385 mock_resolve .return_value = mock_path
84-
86+
8587 result = finder .find_file_path ("test.txt" )
86-
88+
8789 assert result == mock_path
8890 mock_resolve .assert_called_once_with ("test.txt" )
8991
9092 def test_resolve_file_path_code_package_exists (self ):
9193 """Test _resolve_file_path when code package exists and file is found."""
9294 finder = DefaultFindFilePath ()
93-
94- with patch .object (finder , '_code_package_exists' , return_value = True ) as mock_exists :
95- with patch .object (finder , '_get_code_package_file_path' ) as mock_get_path :
95+
96+ with patch .object (
97+ finder , "_code_package_exists" , return_value = True
98+ ) as mock_exists :
99+ with patch .object (finder , "_get_code_package_file_path" ) as mock_get_path :
96100 mock_path = MagicMock ()
97101 mock_path .exists .return_value = True
98102 mock_get_path .return_value = mock_path
99-
103+
100104 result = finder ._resolve_file_path ("test.txt" )
101-
105+
102106 assert result == mock_path
103107 mock_exists .assert_called_once ()
104108 mock_get_path .assert_called_once_with ("test.txt" )
105109
106110 def test_resolve_file_path_code_package_exists_file_not_found (self ):
107- """Test _resolve_file_path when code package exists but file not found, falls back to config."""
111+ """Test _resolve_file_path when code package exists but file not found,
112+ falls back to config."""
108113 finder = DefaultFindFilePath ()
109-
110- with patch .object (finder , '_code_package_exists' , return_value = True ) as mock_exists :
111- with patch .object (finder , '_get_code_package_file_path' ) as mock_get_path :
112- with patch .object (finder , '_find_config_file' ) as mock_find_config :
113- with patch .object (finder , '_get_config_based_file_path' ) as mock_get_config_path :
114+
115+ with patch .object (finder , "_code_package_exists" , return_value = True ):
116+ with patch .object (finder , "_get_code_package_file_path" ) as mock_get_path :
117+ with patch .object (finder , "_find_config_file" ) as mock_find_config :
118+ with patch .object (
119+ finder , "_get_config_based_file_path"
120+ ) as mock_get_config_path :
114121 # Code package file doesn't exist
115122 mock_code_path = MagicMock ()
116123 mock_code_path .exists .return_value = False
117124 mock_get_path .return_value = mock_code_path
118-
125+
119126 # Config file exists and config-based file exists
120127 mock_config_path = MagicMock ()
121128 mock_find_config .return_value = mock_config_path
122-
129+
123130 mock_config_file_path = MagicMock ()
124131 mock_config_file_path .exists .return_value = True
125132 mock_get_config_path .return_value = mock_config_file_path
126-
133+
127134 result = finder ._resolve_file_path ("test.txt" )
128-
135+
129136 assert result == mock_config_file_path
130137 mock_find_config .assert_called_once ()
131- mock_get_config_path .assert_called_once_with ("test.txt" , mock_config_path )
138+ mock_get_config_path .assert_called_once_with (
139+ "test.txt" , mock_config_path
140+ )
132141
133142 def test_resolve_file_path_fallback_to_filename (self ):
134- """Test _resolve_file_path falls back to Path(filename) when no other location works."""
143+ """Test _resolve_file_path falls back to Path(filename)
144+ when no other location works."""
135145 finder = DefaultFindFilePath ()
136-
137- with patch .object (finder , ' _code_package_exists' , return_value = False ):
138- with patch .object (finder , ' _find_config_file' , return_value = None ):
146+
147+ with patch .object (finder , " _code_package_exists" , return_value = False ):
148+ with patch .object (finder , " _find_config_file" , return_value = None ):
139149 result = finder ._resolve_file_path ("test.txt" )
140-
150+
141151 assert result == Path ("test.txt" )
142152
143153 def test_code_package_exists_true (self ):
144154 """Test _code_package_exists returns True when directory exists."""
145155 finder = DefaultFindFilePath ()
146-
147- with patch (' os.path.exists' , return_value = True ):
156+
157+ with patch (" os.path.exists" , return_value = True ):
148158 assert finder ._code_package_exists () is True
149159
150160 def test_code_package_exists_false (self ):
151161 """Test _code_package_exists returns False when directory doesn't exist."""
152162 finder = DefaultFindFilePath ()
153-
154- with patch (' os.path.exists' , return_value = False ):
163+
164+ with patch (" os.path.exists" , return_value = False ):
155165 assert finder ._code_package_exists () is False
156166
157167 def test_get_code_package_file_path (self ):
158168 """Test _get_code_package_file_path constructs correct path."""
159169 finder = DefaultFindFilePath ()
160-
170+
161171 result = finder ._get_code_package_file_path ("test.txt" )
162-
172+
163173 expected = Path ("payload/files/test.txt" )
164174 assert result == expected
165175
166176 def test_get_code_package_file_path_custom_values (self ):
167177 """Test _get_code_package_file_path with custom values."""
168178 finder = DefaultFindFilePath (
169- code_package = "custom_package" ,
170- file_folder = "custom_files"
179+ code_package = "custom_package" , file_folder = "custom_files"
171180 )
172-
181+
173182 result = finder ._get_code_package_file_path ("test.txt" )
174-
183+
175184 expected = Path ("custom_package/custom_files/test.txt" )
176185 assert result == expected
177186
178187 def test_find_config_file_found (self ):
179188 """Test _find_config_file when config file is found."""
180189 finder = DefaultFindFilePath ()
181-
182- with patch .object (finder , ' _find_file_in_tree' ) as mock_find :
190+
191+ with patch .object (finder , " _find_file_in_tree" ) as mock_find :
183192 mock_path = MagicMock ()
184193 mock_find .return_value = mock_path
185-
194+
186195 result = finder ._find_config_file ()
187-
196+
188197 assert result == mock_path
189198 mock_find .assert_called_once_with ("config.json" , Path .cwd ())
190199
191200 def test_find_config_file_not_found (self ):
192201 """Test _find_config_file when config file is not found."""
193202 finder = DefaultFindFilePath ()
194-
195- with patch .object (finder , ' _find_file_in_tree' , return_value = None ):
203+
204+ with patch .object (finder , " _find_file_in_tree" , return_value = None ):
196205 result = finder ._find_config_file ()
197-
206+
198207 assert result is None
199208
200209 def test_get_config_based_file_path (self ):
201210 """Test _get_config_based_file_path constructs correct path."""
202211 finder = DefaultFindFilePath ()
203212 config_path = Path ("/some/path/config.json" )
204-
213+
205214 result = finder ._get_config_based_file_path ("test.txt" , config_path )
206-
215+
207216 expected = Path ("files/test.txt" )
208217 assert result == expected
209218
210219 def test_get_config_based_file_path_custom_folder (self ):
211220 """Test _get_config_based_file_path with custom file folder."""
212221 finder = DefaultFindFilePath (file_folder = "custom_files" )
213222 config_path = Path ("/some/path/config.json" )
214-
223+
215224 result = finder ._get_config_based_file_path ("test.txt" , config_path )
216-
225+
217226 expected = Path ("custom_files/test.txt" )
218227 assert result == expected
219228
220229 def test_find_file_in_tree_found (self ):
221230 """Test _find_file_in_tree when file is found."""
222231 finder = DefaultFindFilePath ()
223-
232+
224233 with tempfile .TemporaryDirectory () as temp_dir :
225234 temp_path = Path (temp_dir )
226235 test_file = temp_path / "test.txt"
227236 test_file .write_text ("test content" )
228-
237+
229238 result = finder ._find_file_in_tree ("test.txt" , temp_path )
230-
239+
231240 assert result is not None
232241 assert result .name == "test.txt"
233242
234243 def test_find_file_in_tree_not_found (self ):
235244 """Test _find_file_in_tree when file is not found."""
236245 finder = DefaultFindFilePath ()
237-
246+
238247 with tempfile .TemporaryDirectory () as temp_dir :
239248 temp_path = Path (temp_dir )
240-
249+
241250 result = finder ._find_file_in_tree ("nonexistent.txt" , temp_path )
242-
251+
243252 assert result is None
244253
245254 def test_find_file_in_tree_multiple_matches (self ):
246255 """Test _find_file_in_tree when multiple files match, returns first one."""
247256 finder = DefaultFindFilePath ()
248-
257+
249258 with tempfile .TemporaryDirectory () as temp_dir :
250259 temp_path = Path (temp_dir )
251-
260+
252261 # Create multiple files with same name in different subdirectories
253262 (temp_path / "subdir1" ).mkdir ()
254263 (temp_path / "subdir2" ).mkdir ()
255-
264+
256265 file1 = temp_path / "subdir1" / "test.txt"
257266 file2 = temp_path / "subdir2" / "test.txt"
258-
267+
259268 file1 .write_text ("content1" )
260269 file2 .write_text ("content2" )
261-
270+
262271 result = finder ._find_file_in_tree ("test.txt" , temp_path )
263-
272+
264273 assert result is not None
265274 assert result .name == "test.txt"
266275 # Should return one of the files (implementation returns first found)
267276
268277 def test_integration_find_file_path_success (self ):
269278 """Test integration: find_file_path with real file system."""
270279 finder = DefaultFindFilePath ()
271-
280+
272281 with tempfile .TemporaryDirectory () as temp_dir :
273282 # Create a test file
274283 test_file = Path (temp_dir ) / "test.txt"
275284 test_file .write_text ("test content" )
276-
285+
277286 # Mock the code package to point to our temp directory
278287 finder .code_package = temp_dir
279288 finder .file_folder = ""
280-
289+
281290 result = finder .find_file_path ("test.txt" )
282-
291+
283292 assert result == test_file
284293 assert result .exists ()
285294
286295 def test_integration_find_file_path_not_found (self ):
287296 """Test integration: find_file_path when file doesn't exist."""
288297 finder = DefaultFindFilePath ()
289-
298+
290299 with tempfile .TemporaryDirectory () as temp_dir :
291300 # Don't create any files
292301 finder .code_package = temp_dir
293302 finder .file_folder = ""
294-
303+
295304 with pytest .raises (FileNotFoundError ):
296305 finder .find_file_path ("nonexistent.txt" )
297306
0 commit comments