|
| 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