Skip to content

Commit f0e36b8

Browse files
authored
Merge branch 'main' into limit-install-version
2 parents 0571115 + a2963cb commit f0e36b8

2 files changed

Lines changed: 387 additions & 1 deletion

File tree

codeflash/discovery/functions_to_optimize.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,53 @@ def filter_functions(
864864
# Normalize paths for case-insensitive comparison on Windows
865865
tests_root_str = os.path.normcase(str(tests_root))
866866
module_root_str = os.path.normcase(str(module_root))
867+
project_root_str = os.path.normcase(str(project_root))
868+
869+
# Check if tests_root overlaps with module_root or project_root
870+
# In this case, we need to use file pattern matching instead of directory matching
871+
tests_root_overlaps_source = (
872+
tests_root_str == module_root_str
873+
or tests_root_str == project_root_str
874+
or module_root_str.startswith(tests_root_str + os.sep)
875+
)
876+
877+
# Test file patterns for when tests_root overlaps with source
878+
test_file_name_patterns = (
879+
".test.",
880+
".spec.",
881+
"_test.",
882+
"_spec.",
883+
)
884+
test_dir_patterns = (
885+
os.sep + "test" + os.sep,
886+
os.sep + "tests" + os.sep,
887+
os.sep + "__tests__" + os.sep,
888+
)
889+
890+
def is_test_file(file_path_normalized: str) -> bool:
891+
"""Check if a file is a test file based on patterns."""
892+
if tests_root_overlaps_source:
893+
# Use file pattern matching when tests_root overlaps with source
894+
file_lower = file_path_normalized.lower()
895+
# Check filename patterns (e.g., .test.ts, .spec.ts)
896+
if any(pattern in file_lower for pattern in test_file_name_patterns):
897+
return True
898+
# Check directory patterns, but only within the project root
899+
# to avoid false positives from parent directories
900+
relative_path = file_lower
901+
if project_root_str and file_lower.startswith(project_root_str.lower()):
902+
relative_path = file_lower[len(project_root_str):]
903+
return any(pattern in relative_path for pattern in test_dir_patterns)
904+
else:
905+
# Use directory-based filtering when tests are in a separate directory
906+
return file_path_normalized.startswith(tests_root_str + os.sep)
867907

868908
# We desperately need Python 3.10+ only support to make this code readable with structural pattern matching
869909
for file_path_path, functions in modified_functions.items():
870910
_functions = functions
871911
file_path = str(file_path_path)
872912
file_path_normalized = os.path.normcase(file_path)
873-
if file_path_normalized.startswith(tests_root_str + os.sep):
913+
if is_test_file(file_path_normalized):
874914
test_functions_removed_count += len(_functions)
875915
continue
876916
if file_path in ignore_paths or any(

0 commit comments

Comments
 (0)