Skip to content

fix: prevent false positive test file detection for projects in /tests/ folders#1320

Merged
Saga4 merged 1 commit intomainfrom
fix/test-file-pattern-matching-parent-dirs
Feb 3, 2026
Merged

fix: prevent false positive test file detection for projects in /tests/ folders#1320
Saga4 merged 1 commit intomainfrom
fix/test-file-pattern-matching-parent-dirs

Conversation

@Saga4
Copy link
Contributor

@Saga4 Saga4 commented Feb 3, 2026

Summary

Fixes incorrect test file filtering when a project is located inside a directory named "tests" (e.g., /home/user/tests/myproject).

Problem

When running codeflash on a project stored in a path containing /tests/, source files were incorrectly filtered as test files:

Ignored functions and files
└── Test functions removed: 1
INFO     Found 0 function to optimize

The issue was in is_test_file() which checked for /tests/ pattern in the full path instead of the relative path from project root.

Root Cause

# Before (buggy):
relative_path = file_lower  # Falls back to full path if condition fails
if project_root_str and file_lower.startswith(project_root_str.lower()):
    relative_path = file_lower[len(project_root_str):]
return any(pattern in relative_path for pattern in test_dir_patterns)

If the condition failed for any reason, relative_path remained the full path, which could contain /tests/ from parent directories.

Solution

# After (fixed):
if project_root_str and file_lower.startswith(project_root_str.lower()):
    relative_path = file_lower[len(project_root_str):]
    return any(pattern in relative_path for pattern in test_dir_patterns)
# If we can't compute relative path, don't check directory patterns
return False

Test Results

Before fix:

Ignored functions and files
└── Test functions removed: 1
INFO     Found 0 function to optimize

After fix:

  • Function is discovered and processed correctly
  • Tests are generated
  • Optimization proceeds

Test plan

  • Tested on n8n repo at /Users/saga4/orgs/tests/n8n
  • Function compareValues is now discovered (was filtered before)
  • Add unit test for this edge case

Generated with Claude Code

…s/ folders

When a project is located inside a directory named "tests" (e.g.,
/home/user/tests/myproject), source files were incorrectly filtered
as test files because the full path contained "/tests/".

The fix ensures directory pattern matching (/test/, /tests/, /__tests__/)
only applies to the relative path from project root, not parent directories.
If the relative path cannot be computed, we now skip directory pattern
matching entirely instead of checking the full path.

Example:
- Before: /home/user/tests/myproject/src/utils.ts was filtered as test file
- After: Only checks if /src/utils.ts contains /tests/ (correctly returns false)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Saga4 Saga4 merged commit 953b0fb into main Feb 3, 2026
26 of 27 checks passed
@Saga4 Saga4 deleted the fix/test-file-pattern-matching-parent-dirs branch February 3, 2026 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant