Skip to content

Fix: Handle test paths outside tests_root in verifier.py#1991

Merged
Saga4 merged 3 commits intomainfrom
fix/verifier-path-validation
Apr 4, 2026
Merged

Fix: Handle test paths outside tests_root in verifier.py#1991
Saga4 merged 3 commits intomainfrom
fix/verifier-path-validation

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where JavaScript/TypeScript optimization runs crash with when test files are generated outside the configured .

Problem

JavaScript support's method places test files in subdirectories adjacent to source files (e.g., ). However, assumes test files are within and calls without handling the that occurs when this assumption is violated.

Root Cause

  1. JavaScript support generates tests at: /workspace/target/src/gateway/server/__tests__/codeflash-generated/test_foo.test.ts
  2. Configured tests_project_rootdir is: /workspace/target/test
  3. Test file is NOT within tests_root, so relative_to() raises ValueError
  4. Optimization run crashes

Solution

Added try-except handling with fallback to using just the filename, matching the pattern already used in .

Changes

  • codeflash/verification/verifier.py:37-48: Added try-except for path computation with explanatory comment
  • tests/verification/test_verifier_path_handling.py: Added 2 unit tests to verify the fix

Testing

✅ Unit tests pass
uv run prek passes (no linting/type errors)
✅ Verified with actual rerun: codeflash --rerun 84f5467f-8acf-427f-b468-02cb3342097e

Trace ID

84f5467f-8acf-427f-b468-02cb3342097e

🤖 Generated with Claude Code

When JavaScript/TypeScript support generates test files in __tests__
subdirectories adjacent to source files (e.g., src/foo/__tests__/codeflash-generated/),
these test files are not within the configured tests_project_rootdir.

Previously, verifier.py:37 called module_name_from_file_path() without
handling the ValueError that occurs when the test path is outside tests_root,
causing optimization runs to crash.

This fix adds try-except handling with a fallback to using just the filename,
matching the pattern already used in javascript/parse.py:330-333.

Fixes trace ID: 84f5467f-8acf-427f-b468-02cb3342097e

Changes:
- codeflash/verification/verifier.py:37-48: Added try-except for path computation
- tests/verification/test_verifier_path_handling.py: Added unit tests

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 4, 2026

Claude finished @mohammedahmed18's task in 2m 39s —— View job


PR Review Summary

Prek Checks

  • ruff: passed
  • mypy: two test methods were missing -> None return type annotations — fixed and pushed in commit ba0d2bc9

Code Review

The fix in verifier.py:41-50 is correct. It wraps the existing module_name_from_file_path(..., traverse_up=True) call in a try-except ValueError and falls back to Path(test_path.name). This is consistent with the same fallback pattern already used in codeflash/verification/parse_test_output.py:325-327 and codeflash/languages/javascript/parse.py.

One observation (not blocking): The second test test_module_name_from_file_path_with_fallback_succeeds doesn't actually exercise the fixed code path in generate_tests() — it reimplements the try-except inline in the test body, testing Python language behavior rather than the production fix. It still has value as a characterization test, but a test that calls generate_tests() with a mocked aiservice_client and asserts no exception is raised would give more confidence. This is low priority given the PR is small and the fix is straightforward.

Note: function_optimizer.py:2175-2176 has the same pre-existing vulnerability (calls module_name_from_file_path without a try-except), but that's out of scope for this PR.

Duplicate Detection

No duplicates detected. The fallback pattern (Path(test_path.name)) is already used in parse_test_output.py and javascript/parse.py, and this PR brings verifier.py into line with that convention.


Last updated: 2026-04-04T07:52Z

@Saga4 Saga4 merged commit 81be416 into main Apr 4, 2026
27 of 29 checks passed
@Saga4 Saga4 deleted the fix/verifier-path-validation branch April 4, 2026 11:01
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.

2 participants