Skip to content

Commit 81be416

Browse files
authored
Merge pull request #1991 from codeflash-ai/fix/verifier-path-validation
Fix: Handle test paths outside tests_root in verifier.py
2 parents c0942b1 + ba0d2bc commit 81be416

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

codeflash/verification/verifier.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ def generate_tests(
3434
# TODO: Sometimes this recreates the original Class definition. This overrides and messes up the original
3535
# class import. Remove the recreation of the class definition
3636
start_time = time.perf_counter()
37-
# Use traverse_up=True to handle co-located __tests__ directories that may be outside
38-
# the configured tests_root (e.g., src/gateway/__tests__/ when tests_root is test/)
39-
test_module_path = Path(module_name_from_file_path(test_path, test_cfg.tests_project_rootdir, traverse_up=True))
37+
38+
# Compute test module path - handle case where test file is outside tests_project_rootdir
39+
# (e.g., JavaScript/TypeScript tests generated in __tests__ subdirectories adjacent to source files)
40+
# Similar to javascript/parse.py:330-333 fallback pattern
41+
try:
42+
# Use traverse_up=True to handle co-located __tests__ directories that may be outside
43+
# the configured tests_root (e.g., src/gateway/__tests__/ when tests_root is test/)
44+
test_module_path = Path(module_name_from_file_path(test_path, test_cfg.tests_project_rootdir, traverse_up=True))
45+
except ValueError:
46+
# Test file is not within tests_project_rootdir - use just the filename
47+
# This can happen for JavaScript/TypeScript when get_test_dir_for_source()
48+
# places tests adjacent to source files (e.g., in src/foo/__tests__/)
49+
# instead of within the configured tests_root
50+
test_module_path = Path(test_path.name)
4051

4152
# Detect module system via language support (non-None for JS/TS, None for Python)
4253
lang_support = current_language_support()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Test that verifier.py handles test files outside tests_project_rootdir gracefully.
2+
3+
This tests the fix for the bug where JavaScript/TypeScript test files generated
4+
in __tests__ subdirectories (adjacent to source files) caused ValueError when
5+
verifier.py tried to compute their module path relative to tests_project_rootdir.
6+
7+
Trace ID: 84f5467f-8acf-427f-b468-02cb3342097e
8+
"""
9+
10+
from pathlib import Path
11+
12+
import pytest
13+
14+
from codeflash.code_utils.code_utils import module_name_from_file_path
15+
16+
17+
class TestVerifierPathHandling:
18+
"""Test path handling in verifier.py for test files outside tests_root."""
19+
20+
def test_module_name_from_file_path_raises_valueerror_when_outside_root(self) -> None:
21+
"""Verify that module_name_from_file_path raises ValueError when file is outside root.
22+
23+
This is the current behavior that causes the bug in verifier.py line 37.
24+
25+
Scenario:
26+
- JavaScript support generates test at: /workspace/target/src/gateway/server/__tests__/codeflash-generated/test_foo.test.ts
27+
- tests_project_rootdir is: /workspace/target/test
28+
- Test file is NOT within tests_root, so relative_to() fails
29+
"""
30+
test_path = Path("/workspace/target/src/gateway/server/__tests__/codeflash-generated/test_foo.test.ts")
31+
tests_root = Path("/workspace/target/test")
32+
33+
# This should raise ValueError before the fix
34+
with pytest.raises(ValueError, match="is not within the project root"):
35+
module_name_from_file_path(test_path, tests_root)
36+
37+
def test_module_name_from_file_path_with_fallback_succeeds(self) -> None:
38+
"""Test that adding a fallback (try-except) allows graceful handling.
39+
40+
This is the pattern used in javascript/parse.py:330-333 that should
41+
also be applied to verifier.py:37.
42+
"""
43+
test_path = Path("/workspace/target/src/gateway/server/__tests__/codeflash-generated/test_foo.test.ts")
44+
tests_root = Path("/workspace/target/test")
45+
46+
# Simulate the fix: try-except with fallback to filename
47+
try:
48+
test_module_path = module_name_from_file_path(test_path, tests_root)
49+
except ValueError:
50+
# Fallback: use just the filename (or relative path from parent)
51+
# This is what javascript/parse.py does
52+
test_module_path = test_path.name
53+
54+
# After fallback, we should have a valid path
55+
assert test_module_path == "test_foo.test.ts"

0 commit comments

Comments
 (0)