Skip to content

fix: set tests_project_rootdir to tests_root for Java projects (Bug #2)#1341

Merged
mashraf-222 merged 1 commit into
omni-javafrom
fix/java-tests-project-rootdir
Feb 5, 2026
Merged

fix: set tests_project_rootdir to tests_root for Java projects (Bug #2)#1341
mashraf-222 merged 1 commit into
omni-javafrom
fix/java-tests-project-rootdir

Conversation

@mashraf-222

Copy link
Copy Markdown

Bug #2: Test File Name Mapping Returns Null for Java

Problem

Java E2E optimizations were failing with "Could not find the test for file name" errors, blocking all optimizations.

Root Cause

  • Maven Surefire JUnit XML doesn't include "file" attributes in test suite elements
  • Code falls back to resolve_test_file_from_class_path() with base_dir = tests_project_rootdir
  • For Java, tests_project_rootdir was set to project root (e.g., /project) instead of tests root (e.g., /project/src/test/java)
  • This caused file resolution to fail when looking for instrumented test files
  • JavaScript had this fix (line 654 in discover_unit_tests.py), but Java was missing it

Solution

Added Java to the language check that sets tests_project_rootdir = tests_root:

```python
if is_javascript():
cfg.tests_project_rootdir = cfg.tests_root
if is_java(): # NEW
cfg.tests_project_rootdir = cfg.tests_root
```

This ensures instrumented test files can be found at their actual location:
`/project/src/test/java/com/example/Test__perfinstrumented.java`

Code Changes

codeflash/discovery/discover_unit_tests.py:

  • Line 644: Added `is_java` import from `codeflash.languages`
  • Lines 656-657: Added Java check to set `tests_project_rootdir = tests_root`

tests/test_java_tests_project_rootdir.py:

  • New test file with 2 comprehensive test cases
  • `test_java_tests_project_rootdir_set_to_tests_root`: Verifies fix for Java
  • `test_python_tests_project_rootdir_unchanged`: Ensures Python behavior unchanged

Testing

```bash
pytest tests/test_java_tests_project_rootdir.py -v

✓ 2 passed in 0.03s

```

Impact

  • Unblocks Java E2E optimizations
  • Fixes test file resolution for all Java projects
  • No impact on Python or JavaScript

🤖 Generated with Claude Sonnet 4.5

Bug #2: Test file name mapping returns null for Java tests

Root cause: For Java projects, tests_project_rootdir was incorrectly set
to the project root instead of the actual tests directory. This caused
test file resolution to fail in parse_test_xml when parsing JUnit XML
from Maven Surefire, which doesn't include file attributes.

JavaScript already had this fix (line 654), but Java was missing it.

Fix: Add Java to the language check that sets tests_project_rootdir
equal to tests_root, ensuring instrumented test files can be found at
src/test/java/com/example/Test__perfinstrumented.java

Changes:
- Added is_java import to discover_unit_tests.py
- Added Java check: if is_java(): cfg.tests_project_rootdir = cfg.tests_root
- Added comprehensive test coverage with 2 test cases

Tests:
- test_java_tests_project_rootdir_set_to_tests_root: verifies fix for Java
- test_python_tests_project_rootdir_unchanged: verifies Python unchanged

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@mashraf-222

Copy link
Copy Markdown
Author

E2E Test Results

Goal: Fix test file resolution for Java by setting tests_project_rootdir correctly.

Result: ✅ PASS

What was tested

Ran E2E optimization on Factorial.factorial() function in the Java project using the local AI service. Verified that test discovery, instrumentation, and file resolution work correctly without "Could not find test" errors.

Test commands

# Checkout PR branch
cd /home/ubuntu/code/codeflash
git checkout fix/java-tests-project-rootdir

# Run unit tests
pytest tests/test_java_tests_project_rootdir.py -v
# Result: 2/2 tests passed

# Run E2E optimization
cd /home/ubuntu/code/Java
CODEFLASH_AIS_SERVER=local uv run codeflash \
  --file src/main/java/com/thealgorithms/maths/Factorial.java \
  --function factorial --no-pr -v

Evidence

1. Unit tests pass:

test_java_tests_project_rootdir_set_to_tests_root PASSED
test_python_tests_project_rootdir_unchanged PASSED
====== 2 passed in 0.03s ======

2. Test discovery succeeded:

INFO Discovered 6 existing unit tests and 0 replay tests in 4.3s 
     at /home/ubuntu/code/Java/src/test/java

3. Test instrumentation succeeded:

INFO Instrumented 2 existing unit test files, 0 replay test files, 
     and 0 concolic coverage test files

4. Instrumented test files written to correct paths:

[PIPELINE] Wrote instrumented behavior test to 
  /home/ubuntu/code/Java/src/test/java/com/thealgorithms/maths/FactorialTest__perfinstrumented.java
[PIPELINE] Wrote instrumented perf test to 
  /home/ubuntu/code/Java/src/test/java/com/thealgorithms/maths/FactorialTest__perfonlyinstrumented.java

5. No "could not find test" errors:

# Checked for the specific error this PR fixes
grep -i "could not find.*test" /tmp/pr1341-test-local.log
# Result: No output (error does not appear)

Verification

The fix correctly sets tests_project_rootdir = tests_root for Java projects (matching existing JavaScript behavior). This ensures instrumented test files can be found at their actual location (e.g., /project/src/test/java/...) rather than being searched from project root.

Code change verified:

if is_java():
    cfg.tests_project_rootdir = cfg.tests_root

This unblocks Java E2E optimizations by fixing test file resolution during instrumentation.


Tested with Claude Code

@mashraf-222 mashraf-222 requested review from a team and Saga4 February 4, 2026 23:42
from codeflash.languages import is_java, is_javascript, is_python

# Detect language from functions being optimized
language = _detect_language_from_functions(file_to_funcs_to_optimize)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should check when we have language detection, it should automatically select the configuration.

@mashraf-222 mashraf-222 merged commit e88730e into omni-java Feb 5, 2026
21 of 30 checks passed
@mashraf-222 mashraf-222 deleted the fix/java-tests-project-rootdir branch February 5, 2026 16:30
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.

3 participants