|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from collections import Counter, defaultdict |
| 4 | +from functools import lru_cache |
4 | 5 | from typing import TYPE_CHECKING |
5 | 6 |
|
6 | 7 | import libcst as cst |
@@ -372,22 +373,51 @@ def add(self, test_file: TestFile) -> None: |
372 | 373 | raise ValueError(msg) |
373 | 374 |
|
374 | 375 | def get_by_original_file_path(self, file_path: Path) -> TestFile | None: |
375 | | - return next((test_file for test_file in self.test_files if test_file.original_file_path == file_path), None) |
| 376 | + normalized = self._normalize_path_for_comparison(file_path) |
| 377 | + for test_file in self.test_files: |
| 378 | + if test_file.original_file_path is None: |
| 379 | + continue |
| 380 | + normalized_test_path = self._normalize_path_for_comparison(test_file.original_file_path) |
| 381 | + if normalized == normalized_test_path: |
| 382 | + return test_file |
| 383 | + return None |
376 | 384 |
|
377 | 385 | def get_test_type_by_instrumented_file_path(self, file_path: Path) -> TestType | None: |
378 | | - return next( |
379 | | - ( |
380 | | - test_file.test_type |
381 | | - for test_file in self.test_files |
382 | | - if (file_path in (test_file.instrumented_behavior_file_path, test_file.benchmarking_file_path)) |
383 | | - ), |
384 | | - None, |
385 | | - ) |
| 386 | + normalized = self._normalize_path_for_comparison(file_path) |
| 387 | + for test_file in self.test_files: |
| 388 | + normalized_behavior_path = self._normalize_path_for_comparison(test_file.instrumented_behavior_file_path) |
| 389 | + if normalized == normalized_behavior_path: |
| 390 | + return test_file.test_type |
| 391 | + if test_file.benchmarking_file_path is not None: |
| 392 | + normalized_benchmark_path = self._normalize_path_for_comparison(test_file.benchmarking_file_path) |
| 393 | + if normalized == normalized_benchmark_path: |
| 394 | + return test_file.test_type |
| 395 | + return None |
386 | 396 |
|
387 | 397 | def get_test_type_by_original_file_path(self, file_path: Path) -> TestType | None: |
388 | | - return next( |
389 | | - (test_file.test_type for test_file in self.test_files if test_file.original_file_path == file_path), None |
390 | | - ) |
| 398 | + normalized = self._normalize_path_for_comparison(file_path) |
| 399 | + for test_file in self.test_files: |
| 400 | + if test_file.original_file_path is None: |
| 401 | + continue |
| 402 | + normalized_test_path = self._normalize_path_for_comparison(test_file.original_file_path) |
| 403 | + if normalized == normalized_test_path: |
| 404 | + return test_file.test_type |
| 405 | + return None |
| 406 | + |
| 407 | + @staticmethod |
| 408 | + @lru_cache(maxsize=4096) |
| 409 | + def _normalize_path_for_comparison(path: Path) -> str: |
| 410 | + """Normalize a path for cross-platform comparison. |
| 411 | +
|
| 412 | + Resolves the path to an absolute path and handles Windows case-insensitivity. |
| 413 | + """ |
| 414 | + try: |
| 415 | + resolved = str(path.resolve()) |
| 416 | + except (OSError, RuntimeError): |
| 417 | + # If resolve fails (e.g., file doesn't exist), use absolute path |
| 418 | + resolved = str(path.absolute()) |
| 419 | + # Only lowercase on Windows where filesystem is case-insensitive |
| 420 | + return resolved.lower() if sys.platform == "win32" else resolved |
391 | 421 |
|
392 | 422 | def __iter__(self) -> Iterator[TestFile]: |
393 | 423 | return iter(self.test_files) |
|
0 commit comments