|
1 | 1 | import tempfile |
2 | | -import unittest |
3 | 2 | from pathlib import Path |
4 | 3 |
|
| 4 | +import pytest |
| 5 | + |
5 | 6 | from codecov_cli.services.upload.file_finder import FileFinder |
6 | 7 | from codecov_cli.types import UploadCollectionResultFile |
7 | 8 |
|
@@ -153,179 +154,266 @@ def test_find_coverage_files_test_results(self, tmp_path): |
153 | 154 | assert actual - expected == {UploadCollectionResultFile(extra)} |
154 | 155 |
|
155 | 156 |
|
156 | | -class TestCoverageFileFinderUserInput(unittest.TestCase): |
157 | | - def setUp(self): |
158 | | - self.temp_dir = tempfile.TemporaryDirectory() # Create a temporary directory |
159 | | - self.project_root = Path(self.temp_dir.name) |
160 | | - self.folders_to_ignore = [] |
161 | | - self.explicitly_listed_files = [ |
162 | | - self.project_root / "test_file.abc", |
163 | | - self.project_root / "subdirectory" / "another_file.abc", |
164 | | - ] |
165 | | - self.disable_search = False |
166 | | - self.coverage_file_finder = FileFinder( |
167 | | - self.project_root, |
168 | | - self.folders_to_ignore, |
169 | | - self.explicitly_listed_files, |
170 | | - self.disable_search, |
171 | | - ) |
172 | | - |
173 | | - def tearDown(self): |
174 | | - self.temp_dir.cleanup() # Clean up the temporary directory |
175 | | - |
176 | | - def test_find_coverage_files_with_existing_files(self): |
177 | | - # Create some sample coverage files |
| 157 | +@pytest.fixture() |
| 158 | +def coverage_file_finder_fixture(): |
| 159 | + temp_dir = tempfile.TemporaryDirectory() # Create a temporary directory |
| 160 | + project_root = Path(temp_dir.name) |
| 161 | + folders_to_ignore = [] |
| 162 | + explicitly_listed_files = [ |
| 163 | + project_root / "test_file.abc", |
| 164 | + project_root / "subdirectory" / "another_file.abc", |
| 165 | + ] |
| 166 | + disable_search = False |
| 167 | + coverage_file_finder = FileFinder( |
| 168 | + project_root, |
| 169 | + folders_to_ignore, |
| 170 | + explicitly_listed_files, |
| 171 | + disable_search, |
| 172 | + ) |
| 173 | + yield project_root, coverage_file_finder |
| 174 | + temp_dir.cleanup() |
| 175 | + |
| 176 | + |
| 177 | +class TestCoverageFileFinderUserInput: |
| 178 | + def test_find_coverage_files_with_existing_files( |
| 179 | + self, coverage_file_finder_fixture |
| 180 | + ): |
| 181 | + # Create some sample coverage coverage_file_finder_fixture |
| 182 | + ( |
| 183 | + project_root, |
| 184 | + coverage_file_finder, |
| 185 | + ) = coverage_file_finder_fixture |
178 | 186 | coverage_files = [ |
179 | | - self.project_root / "coverage.xml", |
180 | | - self.project_root / "subdirectory" / "test_coverage.xml", |
181 | | - self.project_root / "other_file.txt", |
182 | | - self.project_root / ".tox" / "another_file.abc", |
| 187 | + project_root / "coverage.xml", |
| 188 | + project_root / "subdirectory" / "test_coverage.xml", |
| 189 | + project_root / "other_file.txt", |
| 190 | + project_root / ".tox" / "another_file.abc", |
183 | 191 | ] |
184 | | - (self.project_root / "subdirectory").mkdir() |
185 | | - (self.project_root / ".tox").mkdir() |
| 192 | + (project_root / "subdirectory").mkdir() |
| 193 | + (project_root / ".tox").mkdir() |
186 | 194 | for file in coverage_files: |
187 | 195 | file.touch() |
188 | 196 |
|
189 | 197 | result = sorted( |
190 | | - [file.get_filename() for file in self.coverage_file_finder.find_files()] |
| 198 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
191 | 199 | ) |
192 | 200 | expected = [ |
193 | | - UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")), |
| 201 | + UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")), |
194 | 202 | UploadCollectionResultFile( |
195 | | - Path(f"{self.project_root}/subdirectory/test_coverage.xml") |
| 203 | + Path(f"{project_root}/subdirectory/test_coverage.xml") |
196 | 204 | ), |
197 | 205 | ] |
198 | 206 | expected_paths = sorted([file.get_filename() for file in expected]) |
199 | | - self.assertEqual(result, expected_paths) |
200 | | - |
201 | | - def test_find_coverage_files_with_no_files(self): |
202 | | - result = self.coverage_file_finder.find_files() |
203 | | - self.assertEqual(result, []) |
204 | | - |
205 | | - def test_find_coverage_files_with_disabled_search(self): |
206 | | - # Create some sample coverage files |
207 | | - print("project root", self.project_root) |
| 207 | + assert result == expected_paths |
| 208 | + |
| 209 | + def test_find_coverage_files_with_no_files(self, coverage_file_finder_fixture): |
| 210 | + ( |
| 211 | + _, |
| 212 | + coverage_file_finder, |
| 213 | + ) = coverage_file_finder_fixture |
| 214 | + result = coverage_file_finder.find_files() |
| 215 | + assert result == [] |
| 216 | + |
| 217 | + def test_find_coverage_files_with_disabled_search( |
| 218 | + self, coverage_file_finder_fixture |
| 219 | + ): |
| 220 | + ( |
| 221 | + project_root, |
| 222 | + coverage_file_finder, |
| 223 | + ) = coverage_file_finder_fixture |
| 224 | + # Create some sample coverage coverage_file_finder_fixture |
| 225 | + print("project root", project_root) |
208 | 226 | coverage_files = [ |
209 | | - self.project_root / "test_file.abc", |
210 | | - self.project_root / "subdirectory" / "another_file.abc", |
211 | | - self.project_root / "subdirectory" / "test_coverage.xml", |
212 | | - self.project_root / "other_file.txt", |
213 | | - self.project_root / ".tox" / "another_file.abc", |
| 227 | + project_root / "test_file.abc", |
| 228 | + project_root / "subdirectory" / "another_file.abc", |
| 229 | + project_root / "subdirectory" / "test_coverage.xml", |
| 230 | + project_root / "other_file.txt", |
| 231 | + project_root / ".tox" / "another_file.abc", |
214 | 232 | ] |
215 | | - (self.project_root / "subdirectory").mkdir() |
216 | | - (self.project_root / ".tox").mkdir() |
| 233 | + (project_root / "subdirectory").mkdir() |
| 234 | + (project_root / ".tox").mkdir() |
217 | 235 | for file in coverage_files: |
218 | 236 | file.touch() |
219 | 237 |
|
220 | 238 | # Disable search |
221 | | - self.coverage_file_finder.disable_search = True |
| 239 | + coverage_file_finder.disable_search = True |
222 | 240 |
|
223 | 241 | result = sorted( |
224 | | - [file.get_filename() for file in self.coverage_file_finder.find_files()] |
| 242 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
225 | 243 | ) |
226 | 244 |
|
227 | 245 | expected = [ |
228 | | - UploadCollectionResultFile(Path(f"{self.project_root}/test_file.abc")), |
| 246 | + UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")), |
229 | 247 | UploadCollectionResultFile( |
230 | | - Path(f"{self.project_root}/subdirectory/another_file.abc") |
| 248 | + Path(f"{project_root}/subdirectory/another_file.abc") |
231 | 249 | ), |
232 | 250 | ] |
233 | 251 | expected_paths = sorted([file.get_filename() for file in expected]) |
234 | 252 |
|
235 | | - self.assertEqual(result, expected_paths) |
| 253 | + assert result == expected_paths |
236 | 254 |
|
237 | | - def test_find_coverage_files_with_user_specified_files(self): |
238 | | - # Create some sample coverage files |
| 255 | + def test_find_coverage_files_with_user_specified_files( |
| 256 | + self, coverage_file_finder_fixture |
| 257 | + ): |
| 258 | + ( |
| 259 | + project_root, |
| 260 | + coverage_file_finder, |
| 261 | + ) = coverage_file_finder_fixture |
| 262 | + |
| 263 | + # Create some sample coverage coverage_file_finder_fixture |
239 | 264 | coverage_files = [ |
240 | | - self.project_root / "coverage.xml", |
241 | | - self.project_root / "subdirectory" / "test_coverage.xml", |
242 | | - self.project_root / "test_file.abc", |
243 | | - self.project_root / "subdirectory" / "another_file.abc", |
244 | | - self.project_root / ".tox" / "another_file.abc", |
| 265 | + project_root / "coverage.xml", |
| 266 | + project_root / "subdirectory" / "test_coverage.xml", |
| 267 | + project_root / "test_file.abc", |
| 268 | + project_root / "subdirectory" / "another_file.abc", |
| 269 | + project_root / ".tox" / "another_file.abc", |
245 | 270 | ] |
246 | | - (self.project_root / "subdirectory").mkdir() |
247 | | - (self.project_root / ".tox").mkdir() |
| 271 | + (project_root / "subdirectory").mkdir() |
| 272 | + (project_root / ".tox").mkdir() |
248 | 273 | for file in coverage_files: |
249 | 274 | file.touch() |
250 | 275 |
|
251 | 276 | result = sorted( |
252 | | - [file.get_filename() for file in self.coverage_file_finder.find_files()] |
| 277 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
253 | 278 | ) |
254 | 279 |
|
255 | 280 | expected = [ |
256 | | - UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")), |
| 281 | + UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")), |
257 | 282 | UploadCollectionResultFile( |
258 | | - Path(f"{self.project_root}/subdirectory/test_coverage.xml") |
| 283 | + Path(f"{project_root}/subdirectory/test_coverage.xml") |
259 | 284 | ), |
260 | | - UploadCollectionResultFile(Path(f"{self.project_root}/test_file.abc")), |
| 285 | + UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")), |
261 | 286 | UploadCollectionResultFile( |
262 | | - Path(f"{self.project_root}/subdirectory/another_file.abc") |
| 287 | + Path(f"{project_root}/subdirectory/another_file.abc") |
263 | 288 | ), |
264 | 289 | ] |
265 | 290 | expected_paths = sorted([file.get_filename() for file in expected]) |
266 | | - self.assertEqual(result, expected_paths) |
| 291 | + assert result == expected_paths |
267 | 292 |
|
268 | | - def test_find_coverage_files_with_user_specified_files_not_found(self): |
269 | | - # Create some sample coverage files |
| 293 | + def test_find_coverage_files_with_user_specified_files_not_found( |
| 294 | + self, coverage_file_finder_fixture |
| 295 | + ): |
| 296 | + ( |
| 297 | + project_root, |
| 298 | + coverage_file_finder, |
| 299 | + ) = coverage_file_finder_fixture |
| 300 | + |
| 301 | + # Create some sample coverage coverage_file_finder_fixture |
270 | 302 | coverage_files = [ |
271 | | - self.project_root / "coverage.xml", |
272 | | - self.project_root / "subdirectory" / "test_coverage.xml", |
273 | | - self.project_root / ".tox" / "another_file.abc", |
| 303 | + project_root / "coverage.xml", |
| 304 | + project_root / "subdirectory" / "test_coverage.xml", |
| 305 | + project_root / ".tox" / "another_file.abc", |
274 | 306 | ] |
275 | | - (self.project_root / "subdirectory").mkdir() |
276 | | - (self.project_root / ".tox").mkdir() |
| 307 | + (project_root / "subdirectory").mkdir() |
| 308 | + (project_root / ".tox").mkdir() |
277 | 309 | for file in coverage_files: |
278 | 310 | file.touch() |
279 | 311 |
|
280 | 312 | # Add a non-existent file to explicitly_listed_files |
281 | | - self.coverage_file_finder.explicitly_listed_files.append( |
282 | | - self.project_root / "non_existent.xml" |
| 313 | + coverage_file_finder.explicitly_listed_files.append( |
| 314 | + project_root / "non_existent.xml" |
283 | 315 | ) |
284 | 316 |
|
285 | 317 | result = sorted( |
286 | | - [file.get_filename() for file in self.coverage_file_finder.find_files()] |
| 318 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
287 | 319 | ) |
288 | 320 |
|
289 | 321 | expected = [ |
290 | | - UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")), |
| 322 | + UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")), |
291 | 323 | UploadCollectionResultFile( |
292 | | - Path(f"{self.project_root}/subdirectory/test_coverage.xml") |
| 324 | + Path(f"{project_root}/subdirectory/test_coverage.xml") |
293 | 325 | ), |
294 | 326 | ] |
295 | 327 | expected_paths = sorted([file.get_filename() for file in expected]) |
296 | | - self.assertEqual(result, expected_paths) |
| 328 | + assert result == expected_paths |
297 | 329 |
|
298 | 330 | def test_find_coverage_files_with_user_specified_files_in_default_ignored_folder( |
299 | | - self, |
| 331 | + self, coverage_file_finder_fixture |
300 | 332 | ): |
| 333 | + |
| 334 | + ( |
| 335 | + project_root, |
| 336 | + coverage_file_finder, |
| 337 | + ) = coverage_file_finder_fixture |
| 338 | + |
301 | 339 | # Create some sample coverage files |
302 | 340 | coverage_files = [ |
303 | | - self.project_root / "coverage.xml", |
304 | | - self.project_root / "subdirectory" / "test_coverage.xml", |
305 | | - self.project_root / "test_file.abc", |
306 | | - self.project_root / "subdirectory" / "another_file.abc", |
307 | | - self.project_root / ".tox" / "another_file.abc", |
| 341 | + project_root / "coverage.xml", |
| 342 | + project_root / "subdirectory" / "test_coverage.xml", |
| 343 | + project_root / "test_file.abc", |
| 344 | + project_root / "subdirectory" / "another_file.abc", |
| 345 | + project_root / ".tox" / "another_file.abc", |
308 | 346 | ] |
309 | | - (self.project_root / "subdirectory").mkdir() |
310 | | - (self.project_root / ".tox").mkdir() |
| 347 | + (project_root / "subdirectory").mkdir() |
| 348 | + (project_root / ".tox").mkdir() |
311 | 349 | for file in coverage_files: |
312 | 350 | file.touch() |
313 | 351 |
|
314 | | - self.coverage_file_finder.explicitly_listed_files = [ |
315 | | - self.project_root / ".tox" / "another_file.abc", |
| 352 | + coverage_file_finder.explicitly_listed_files = [ |
| 353 | + project_root / ".tox" / "another_file.abc", |
| 354 | + ] |
| 355 | + result = sorted( |
| 356 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
| 357 | + ) |
| 358 | + |
| 359 | + expected = [ |
| 360 | + UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")), |
| 361 | + UploadCollectionResultFile( |
| 362 | + Path(f"{project_root}/subdirectory/test_coverage.xml") |
| 363 | + ), |
| 364 | + UploadCollectionResultFile(Path(f"{project_root}/.tox/another_file.abc")), |
316 | 365 | ] |
| 366 | + expected_paths = sorted([file.get_filename() for file in expected]) |
| 367 | + |
| 368 | + assert result == expected_paths |
| 369 | + |
| 370 | + def test_find_coverage_files_with_user_specified_files_in_excluded( |
| 371 | + self, capsys, coverage_file_finder_fixture |
| 372 | + ): |
| 373 | + ( |
| 374 | + project_root, |
| 375 | + coverage_file_finder, |
| 376 | + ) = coverage_file_finder_fixture |
| 377 | + |
| 378 | + # Create some sample coverage coverage_file_finder_fixture |
| 379 | + coverage_files = [ |
| 380 | + project_root / "coverage.xml", |
| 381 | + project_root / "subdirectory" / "test_coverage.xml", |
| 382 | + project_root / "test_file.abc", |
| 383 | + project_root / "subdirectory" / "another_file.abc", |
| 384 | + project_root / "subdirectory" / "another_file.bash", |
| 385 | + project_root / ".tox" / "another_file.abc", |
| 386 | + ] |
| 387 | + (project_root / "subdirectory").mkdir() |
| 388 | + (project_root / ".tox").mkdir() |
| 389 | + for file in coverage_files: |
| 390 | + file.touch() |
| 391 | + |
| 392 | + coverage_file_finder.explicitly_listed_files.append( |
| 393 | + project_root / "subdirectory" / "another_file.bash" |
| 394 | + ) |
317 | 395 | result = sorted( |
318 | | - [file.get_filename() for file in self.coverage_file_finder.find_files()] |
| 396 | + [file.get_filename() for file in coverage_file_finder.find_files()] |
319 | 397 | ) |
320 | 398 |
|
321 | 399 | expected = [ |
322 | | - UploadCollectionResultFile(Path(f"{self.project_root}/coverage.xml")), |
| 400 | + UploadCollectionResultFile(Path(f"{project_root}/coverage.xml")), |
323 | 401 | UploadCollectionResultFile( |
324 | | - Path(f"{self.project_root}/subdirectory/test_coverage.xml") |
| 402 | + Path(f"{project_root}/subdirectory/test_coverage.xml") |
325 | 403 | ), |
| 404 | + UploadCollectionResultFile(Path(f"{project_root}/test_file.abc")), |
326 | 405 | UploadCollectionResultFile( |
327 | | - Path(f"{self.project_root}/.tox/another_file.abc") |
| 406 | + Path(f"{project_root}/subdirectory/another_file.abc") |
| 407 | + ), |
| 408 | + UploadCollectionResultFile( |
| 409 | + Path(f"{project_root}/subdirectory/another_file.bash") |
328 | 410 | ), |
329 | 411 | ] |
330 | 412 | expected_paths = sorted([file.get_filename() for file in expected]) |
331 | | - self.assertEqual(result, expected_paths) |
| 413 | + |
| 414 | + assert result == expected_paths |
| 415 | + |
| 416 | + assert ( |
| 417 | + "Some files being explicitly added are found in the list of excluded files for upload. We are still going to search for the explicitly added files." |
| 418 | + in capsys.readouterr().err |
| 419 | + ) |
0 commit comments