Skip to content

Commit 8cc86ec

Browse files
committed
fix: port collocated tests pattern matching from main
When tests_root overlaps module_root (common in JS monorepos), use filename pattern matching instead of directory-based filtering to avoid excluding all source files.
1 parent ca149fa commit 8cc86ec

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

codeflash/discovery/functions_to_optimize.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,36 @@ def is_test_file(file_path_normalized: str) -> bool:
868868
return {k: v for k, v in filtered_modified_functions.items() if v}, functions_count
869869

870870

871+
def _is_test_file_by_pattern(file_path: Path) -> bool:
872+
"""Check if a file is a test file using naming conventions.
873+
874+
Used when tests_root overlaps with module_root, so directory-based filtering would
875+
incorrectly exclude all source files. Falls back to filename and directory patterns.
876+
"""
877+
name = file_path.name.lower()
878+
if name.startswith("test_") or name == "conftest.py":
879+
return True
880+
test_name_patterns = (".test.", ".spec.", "_test.", "_spec.")
881+
if any(p in name for p in test_name_patterns):
882+
return True
883+
path_str = str(file_path).lower()
884+
test_dir_patterns = (os.sep + "test" + os.sep, os.sep + "tests" + os.sep, os.sep + "__tests__" + os.sep)
885+
return any(p in path_str for p in test_dir_patterns)
886+
887+
871888
def filter_files_optimized(file_path: Path, tests_root: Path, ignore_paths: list[Path], module_root: Path) -> bool:
872889
"""Optimized version of the filter_functions function above.
873890
874891
Takes in file paths and returns the count of files that are to be optimized.
875892
"""
876893
submodule_paths = None
877-
if file_path.is_relative_to(tests_root):
894+
# When tests_root overlaps module_root (e.g., both are "src"), use pattern matching
895+
# instead of directory matching to avoid filtering out all source files.
896+
tests_root_overlaps = tests_root == module_root or module_root.is_relative_to(tests_root)
897+
if tests_root_overlaps:
898+
if _is_test_file_by_pattern(file_path):
899+
return False
900+
elif file_path.is_relative_to(tests_root):
878901
return False
879902
if file_path in ignore_paths or any(file_path.is_relative_to(ignore_path) for ignore_path in ignore_paths):
880903
return False

0 commit comments

Comments
 (0)