|
11 | 11 | import subprocess |
12 | 12 | import unittest |
13 | 13 | from collections import defaultdict |
| 14 | +from functools import lru_cache |
14 | 15 | from pathlib import Path |
15 | 16 | from typing import TYPE_CHECKING, Callable, Optional, final |
16 | 17 |
|
|
35 | 36 | from codeflash.verification.verification_utils import TestConfig |
36 | 37 |
|
37 | 38 |
|
| 39 | +def existing_unit_test_count( |
| 40 | + func: FunctionToOptimize, project_root: Path, function_to_tests: dict[str, set[FunctionCalledInTest]] |
| 41 | +) -> int: |
| 42 | + key = f"{module_name_from_file_path_cached(func.file_path, project_root)}.{func.qualified_name}" |
| 43 | + tests = function_to_tests.get(key, set()) |
| 44 | + seen: set[tuple[Path, str | None, str]] = set() |
| 45 | + for t in tests: |
| 46 | + if t.tests_in_file.test_type != TestType.EXISTING_UNIT_TEST: |
| 47 | + continue |
| 48 | + tif = t.tests_in_file |
| 49 | + base_name = tif.test_function.split("[", 1)[0] |
| 50 | + seen.add((tif.test_file, tif.test_class, base_name)) |
| 51 | + return len(seen) |
| 52 | + |
| 53 | + |
38 | 54 | @final |
39 | 55 | class PytestExitCode(enum.IntEnum): # don't need to import entire pytest just for this |
40 | 56 | #: Tests passed. |
@@ -1079,3 +1095,9 @@ def process_test_files( |
1079 | 1095 | tests_cache.close() |
1080 | 1096 |
|
1081 | 1097 | return dict(function_to_test_map), num_discovered_tests, num_discovered_replay_tests |
| 1098 | + |
| 1099 | + |
| 1100 | +# Cache module name resolution to avoid repeated Path.resolve()/relative_to() calls |
| 1101 | +@lru_cache(maxsize=128) |
| 1102 | +def module_name_from_file_path_cached(file_path: Path, project_root: Path) -> str: |
| 1103 | + return module_name_from_file_path(file_path, project_root) |
0 commit comments