fix: resolve test module path for JS when tests are outside tests_project_rootdir
Some checks are pending
Lint / prek (pull_request) Waiting to run
Claude Code / pr-review (pull_request) Waiting to run
Claude Code / claude-mention (pull_request) Waiting to run
CodeFlash / Optimize new Python code (pull_request) Waiting to run
E2E - Async / async-optimization (pull_request) Waiting to run
E2E - Bubble Sort Benchmark / benchmark-bubble-sort-optimization (pull_request) Waiting to run
E2E - Bubble Sort Pytest (No Git) / bubble-sort-optimization-pytest-no-git (pull_request) Waiting to run
E2E - Bubble Sort Unittest / bubble-sort-optimization-unittest (pull_request) Waiting to run
Coverage E2E / end-to-end-test-coverage (pull_request) Waiting to run
E2E - Futurehouse Structure / futurehouse-structure (pull_request) Waiting to run
E2E - Init Optimization / init-optimization (pull_request) Waiting to run
E2E - Java Fibonacci (No Git) / java-fibonacci-optimization-no-git (pull_request) Waiting to run
E2E - JS CommonJS Function / js-cjs-function-optimization (pull_request) Waiting to run
E2E - JS ESM Async / js-esm-async-optimization (pull_request) Waiting to run
E2E - JS TypeScript Class / js-ts-class-optimization (pull_request) Waiting to run
E2E - Topological Sort (Worktree) / topological-sort-worktree-optimization (pull_request) Waiting to run
E2E - Tracer Replay / tracer-replay (pull_request) Waiting to run
Java E2E Tests / java-e2e (pull_request) Waiting to run
PR Labeler / label-workflow-changes (pull_request) Waiting to run
Mypy Type Checking for CLI / type-check-cli (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.10) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.11) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.12) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.13) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.14) (pull_request) Waiting to run
unit-tests / unit-tests (ubuntu-latest, 3.9) (pull_request) Waiting to run
unit-tests / unit-tests (windows-latest, 3.13) (pull_request) Waiting to run

For JavaScript projects, generated tests are placed near the source file
(e.g., code_to_optimize/js/.../tests/codeflash-generated/) rather than
under tests_project_rootdir. This caused module_name_from_file_path to
crash with ValueError. Fall back to project_root_path when the test path
isn't under tests_project_rootdir.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
aseembits93 2026-03-18 14:52:00 -07:00
parent 32decd746c
commit 71d6e5e093
3 changed files with 26 additions and 4 deletions

View file

@ -2105,9 +2105,14 @@ class FunctionOptimizer:
},
)
test_module_path = Path(
module_name_from_file_path(gt.behavior_file_path, self.test_cfg.tests_project_rootdir)
)
try:
test_module_path = Path(
module_name_from_file_path(gt.behavior_file_path, self.test_cfg.tests_project_rootdir)
)
except ValueError:
test_module_path = Path(
module_name_from_file_path(gt.behavior_file_path, self.test_cfg.project_root_path)
)
repair_result = self.aiservice_client.repair_generated_tests(
test_source=gt.generated_original_test_source,
functions_to_repair=review.functions_to_repair,

View file

@ -33,7 +33,11 @@ def generate_tests(
# TODO: Sometimes this recreates the original Class definition. This overrides and messes up the original
# class import. Remove the recreation of the class definition
start_time = time.perf_counter()
test_module_path = Path(module_name_from_file_path(test_path, test_cfg.tests_project_rootdir))
try:
test_module_path = Path(module_name_from_file_path(test_path, test_cfg.tests_project_rootdir))
except ValueError:
# For JS/TS, generated tests may be placed near the source file (not under tests_project_rootdir)
test_module_path = Path(module_name_from_file_path(test_path, test_cfg.project_root_path))
# Detect module system via language support (non-None for JS/TS, None for Python)
lang_support = current_language_support()

View file

@ -99,6 +99,19 @@ def test_module_name_from_file_path_with_root_as_file() -> None:
assert module_name == "code_utils"
def test_module_name_from_file_path_not_under_root() -> None:
project_root_path = Path("/project/tests")
file_path = Path("/project/code_to_optimize/js/tests/codeflash-generated/test_calc.test.js")
with pytest.raises(ValueError, match="is not within the project root"):
module_name_from_file_path(file_path, project_root_path)
# But it should work with the actual project root
broader_root = Path("/project")
module_name = module_name_from_file_path(file_path, broader_root)
assert module_name == "code_to_optimize.js.tests.codeflash-generated.test_calc.test"
def test_get_imports_from_file_with_file_path(tmp_path: Path) -> None:
test_file = tmp_path / "test_file.py"
test_file.write_text("import os\nfrom sys import path\n")