14
14
# limitations under the License.
15
15
from __future__ import annotations
16
16
17
- import os
18
- import tempfile
19
17
from pathlib import Path
18
+ import tempfile
20
19
from unittest .mock import MagicMock , patch
21
20
22
21
import pytest
@@ -34,7 +33,7 @@ class TestDefaultFindFilePath:
34
33
def test_init_with_defaults (self ):
35
34
"""Test initialization with default values."""
36
35
finder = DefaultFindFilePath ()
37
-
36
+
38
37
assert finder .code_package == "payload"
39
38
assert finder .file_folder == "files"
40
39
assert finder .config_file == "config.json"
@@ -44,254 +43,264 @@ def test_init_with_custom_values(self):
44
43
finder = DefaultFindFilePath (
45
44
code_package = "custom_package" ,
46
45
file_folder = "custom_files" ,
47
- config_file = "custom_config.json"
46
+ config_file = "custom_config.json" ,
48
47
)
49
-
48
+
50
49
assert finder .code_package == "custom_package"
51
50
assert finder .file_folder == "custom_files"
52
51
assert finder .config_file == "custom_config.json"
53
52
54
53
def test_find_file_path_empty_filename (self ):
55
54
"""Test find_file_path with empty filename raises ValueError."""
56
55
finder = DefaultFindFilePath ()
57
-
56
+
58
57
with pytest .raises (ValueError , match = "file_name cannot be empty" ):
59
58
finder .find_file_path ("" )
60
-
59
+
61
60
with pytest .raises (ValueError , match = "file_name cannot be empty" ):
62
61
finder .find_file_path (None )
63
62
64
63
def test_find_file_path_file_not_found (self ):
65
64
"""Test find_file_path when file doesn't exist raises FileNotFoundError."""
66
65
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 :
69
68
mock_path = MagicMock ()
70
69
mock_path .exists .return_value = False
71
70
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
+ ):
74
76
finder .find_file_path ("test.txt" )
75
77
76
78
def test_find_file_path_success (self ):
77
79
"""Test find_file_path when file exists returns Path."""
78
80
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 :
81
83
mock_path = MagicMock ()
82
84
mock_path .exists .return_value = True
83
85
mock_resolve .return_value = mock_path
84
-
86
+
85
87
result = finder .find_file_path ("test.txt" )
86
-
88
+
87
89
assert result == mock_path
88
90
mock_resolve .assert_called_once_with ("test.txt" )
89
91
90
92
def test_resolve_file_path_code_package_exists (self ):
91
93
"""Test _resolve_file_path when code package exists and file is found."""
92
94
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 :
96
100
mock_path = MagicMock ()
97
101
mock_path .exists .return_value = True
98
102
mock_get_path .return_value = mock_path
99
-
103
+
100
104
result = finder ._resolve_file_path ("test.txt" )
101
-
105
+
102
106
assert result == mock_path
103
107
mock_exists .assert_called_once ()
104
108
mock_get_path .assert_called_once_with ("test.txt" )
105
109
106
110
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."""
108
113
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 :
114
121
# Code package file doesn't exist
115
122
mock_code_path = MagicMock ()
116
123
mock_code_path .exists .return_value = False
117
124
mock_get_path .return_value = mock_code_path
118
-
125
+
119
126
# Config file exists and config-based file exists
120
127
mock_config_path = MagicMock ()
121
128
mock_find_config .return_value = mock_config_path
122
-
129
+
123
130
mock_config_file_path = MagicMock ()
124
131
mock_config_file_path .exists .return_value = True
125
132
mock_get_config_path .return_value = mock_config_file_path
126
-
133
+
127
134
result = finder ._resolve_file_path ("test.txt" )
128
-
135
+
129
136
assert result == mock_config_file_path
130
137
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
+ )
132
141
133
142
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."""
135
145
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 ):
139
149
result = finder ._resolve_file_path ("test.txt" )
140
-
150
+
141
151
assert result == Path ("test.txt" )
142
152
143
153
def test_code_package_exists_true (self ):
144
154
"""Test _code_package_exists returns True when directory exists."""
145
155
finder = DefaultFindFilePath ()
146
-
147
- with patch (' os.path.exists' , return_value = True ):
156
+
157
+ with patch (" os.path.exists" , return_value = True ):
148
158
assert finder ._code_package_exists () is True
149
159
150
160
def test_code_package_exists_false (self ):
151
161
"""Test _code_package_exists returns False when directory doesn't exist."""
152
162
finder = DefaultFindFilePath ()
153
-
154
- with patch (' os.path.exists' , return_value = False ):
163
+
164
+ with patch (" os.path.exists" , return_value = False ):
155
165
assert finder ._code_package_exists () is False
156
166
157
167
def test_get_code_package_file_path (self ):
158
168
"""Test _get_code_package_file_path constructs correct path."""
159
169
finder = DefaultFindFilePath ()
160
-
170
+
161
171
result = finder ._get_code_package_file_path ("test.txt" )
162
-
172
+
163
173
expected = Path ("payload/files/test.txt" )
164
174
assert result == expected
165
175
166
176
def test_get_code_package_file_path_custom_values (self ):
167
177
"""Test _get_code_package_file_path with custom values."""
168
178
finder = DefaultFindFilePath (
169
- code_package = "custom_package" ,
170
- file_folder = "custom_files"
179
+ code_package = "custom_package" , file_folder = "custom_files"
171
180
)
172
-
181
+
173
182
result = finder ._get_code_package_file_path ("test.txt" )
174
-
183
+
175
184
expected = Path ("custom_package/custom_files/test.txt" )
176
185
assert result == expected
177
186
178
187
def test_find_config_file_found (self ):
179
188
"""Test _find_config_file when config file is found."""
180
189
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 :
183
192
mock_path = MagicMock ()
184
193
mock_find .return_value = mock_path
185
-
194
+
186
195
result = finder ._find_config_file ()
187
-
196
+
188
197
assert result == mock_path
189
198
mock_find .assert_called_once_with ("config.json" , Path .cwd ())
190
199
191
200
def test_find_config_file_not_found (self ):
192
201
"""Test _find_config_file when config file is not found."""
193
202
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 ):
196
205
result = finder ._find_config_file ()
197
-
206
+
198
207
assert result is None
199
208
200
209
def test_get_config_based_file_path (self ):
201
210
"""Test _get_config_based_file_path constructs correct path."""
202
211
finder = DefaultFindFilePath ()
203
212
config_path = Path ("/some/path/config.json" )
204
-
213
+
205
214
result = finder ._get_config_based_file_path ("test.txt" , config_path )
206
-
215
+
207
216
expected = Path ("files/test.txt" )
208
217
assert result == expected
209
218
210
219
def test_get_config_based_file_path_custom_folder (self ):
211
220
"""Test _get_config_based_file_path with custom file folder."""
212
221
finder = DefaultFindFilePath (file_folder = "custom_files" )
213
222
config_path = Path ("/some/path/config.json" )
214
-
223
+
215
224
result = finder ._get_config_based_file_path ("test.txt" , config_path )
216
-
225
+
217
226
expected = Path ("custom_files/test.txt" )
218
227
assert result == expected
219
228
220
229
def test_find_file_in_tree_found (self ):
221
230
"""Test _find_file_in_tree when file is found."""
222
231
finder = DefaultFindFilePath ()
223
-
232
+
224
233
with tempfile .TemporaryDirectory () as temp_dir :
225
234
temp_path = Path (temp_dir )
226
235
test_file = temp_path / "test.txt"
227
236
test_file .write_text ("test content" )
228
-
237
+
229
238
result = finder ._find_file_in_tree ("test.txt" , temp_path )
230
-
239
+
231
240
assert result is not None
232
241
assert result .name == "test.txt"
233
242
234
243
def test_find_file_in_tree_not_found (self ):
235
244
"""Test _find_file_in_tree when file is not found."""
236
245
finder = DefaultFindFilePath ()
237
-
246
+
238
247
with tempfile .TemporaryDirectory () as temp_dir :
239
248
temp_path = Path (temp_dir )
240
-
249
+
241
250
result = finder ._find_file_in_tree ("nonexistent.txt" , temp_path )
242
-
251
+
243
252
assert result is None
244
253
245
254
def test_find_file_in_tree_multiple_matches (self ):
246
255
"""Test _find_file_in_tree when multiple files match, returns first one."""
247
256
finder = DefaultFindFilePath ()
248
-
257
+
249
258
with tempfile .TemporaryDirectory () as temp_dir :
250
259
temp_path = Path (temp_dir )
251
-
260
+
252
261
# Create multiple files with same name in different subdirectories
253
262
(temp_path / "subdir1" ).mkdir ()
254
263
(temp_path / "subdir2" ).mkdir ()
255
-
264
+
256
265
file1 = temp_path / "subdir1" / "test.txt"
257
266
file2 = temp_path / "subdir2" / "test.txt"
258
-
267
+
259
268
file1 .write_text ("content1" )
260
269
file2 .write_text ("content2" )
261
-
270
+
262
271
result = finder ._find_file_in_tree ("test.txt" , temp_path )
263
-
272
+
264
273
assert result is not None
265
274
assert result .name == "test.txt"
266
275
# Should return one of the files (implementation returns first found)
267
276
268
277
def test_integration_find_file_path_success (self ):
269
278
"""Test integration: find_file_path with real file system."""
270
279
finder = DefaultFindFilePath ()
271
-
280
+
272
281
with tempfile .TemporaryDirectory () as temp_dir :
273
282
# Create a test file
274
283
test_file = Path (temp_dir ) / "test.txt"
275
284
test_file .write_text ("test content" )
276
-
285
+
277
286
# Mock the code package to point to our temp directory
278
287
finder .code_package = temp_dir
279
288
finder .file_folder = ""
280
-
289
+
281
290
result = finder .find_file_path ("test.txt" )
282
-
291
+
283
292
assert result == test_file
284
293
assert result .exists ()
285
294
286
295
def test_integration_find_file_path_not_found (self ):
287
296
"""Test integration: find_file_path when file doesn't exist."""
288
297
finder = DefaultFindFilePath ()
289
-
298
+
290
299
with tempfile .TemporaryDirectory () as temp_dir :
291
300
# Don't create any files
292
301
finder .code_package = temp_dir
293
302
finder .file_folder = ""
294
-
303
+
295
304
with pytest .raises (FileNotFoundError ):
296
305
finder .find_file_path ("nonexistent.txt" )
297
306
0 commit comments