codeflash/tests/test_java_tests_project_rootdir.py
Kevin Turcios eceac13fc3 Merge remote-tracking branch 'origin/main' into omni-java
# Conflicts:
#	.claude/rules/architecture.md
#	.claude/rules/code-style.md
#	.github/workflows/claude.yml
#	.github/workflows/duplicate-code-detector.yml
#	codeflash/api/aiservice.py
#	codeflash/cli_cmds/console.py
#	codeflash/cli_cmds/logging_config.py
#	codeflash/code_utils/deduplicate_code.py
#	codeflash/discovery/discover_unit_tests.py
#	codeflash/languages/base.py
#	codeflash/languages/code_replacer.py
#	codeflash/languages/javascript/mocha_runner.py
#	codeflash/languages/javascript/support.py
#	codeflash/languages/python/support.py
#	codeflash/optimization/function_optimizer.py
#	codeflash/verification/parse_test_output.py
#	codeflash/verification/verification_utils.py
#	codeflash/verification/verifier.py
#	packages/codeflash/package-lock.json
#	packages/codeflash/package.json
#	tests/languages/javascript/test_support_dispatch.py
#	tests/test_codeflash_capture.py
#	tests/test_languages/test_javascript_test_runner.py
#	tests/test_multi_file_code_replacement.py
2026-03-04 01:52:32 -05:00

82 lines
3.2 KiB
Python

"""Test that tests_project_rootdir is set correctly for Java projects."""
from pathlib import Path
from unittest.mock import MagicMock, patch
from codeflash.discovery.discover_unit_tests import discover_unit_tests
from codeflash.languages.base import Language
from codeflash.languages.current import reset_current_language, set_current_language
from codeflash.verification.verification_utils import TestConfig
def test_java_tests_project_rootdir_set_to_tests_root(tmp_path):
"""Test that for Java projects, tests_project_rootdir is set to tests_root."""
# Create a mock Java project structure
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pom.xml").touch()
tests_root = project_root / "src" / "test" / "java"
tests_root.mkdir(parents=True)
# Create test config with tests_project_rootdir initially set to project root
# (simulating what happens before the fix)
test_cfg = TestConfig(
tests_root=tests_root,
project_root_path=project_root,
tests_project_rootdir=project_root, # Initially set to project root
)
# Create a mock Java function to ensure language detection works
mock_java_function = MagicMock()
mock_java_function.language = "java"
file_to_funcs = {Path("dummy.java"): [mock_java_function]}
# Set current language to Java so is_python() returns False and
# current_language_support() returns JavaSupport with its
# adjust_test_config_for_discovery implementation
set_current_language(Language.JAVA)
try:
with patch("codeflash.discovery.discover_unit_tests.discover_tests_for_language") as mock_discover:
mock_discover.return_value = ({}, 0, 0)
# Call discover_unit_tests
discover_unit_tests(test_cfg, file_to_funcs_to_optimize=file_to_funcs)
finally:
reset_current_language()
# Verify that tests_project_rootdir was updated to tests_root
assert test_cfg.tests_project_rootdir == tests_root, (
f"Expected tests_project_rootdir to be {tests_root}, but got {test_cfg.tests_project_rootdir}"
)
def test_python_tests_project_rootdir_unchanged(tmp_path):
"""Test that for Python projects, tests_project_rootdir behavior is unchanged."""
# Setup Python environment
set_current_language(Language.PYTHON)
# Create a mock Python project structure
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pyproject.toml").touch()
tests_root = project_root / "tests"
tests_root.mkdir()
# Create test config
original_tests_project_rootdir = project_root / "some" / "other" / "dir"
test_cfg = TestConfig(
tests_root=tests_root, project_root_path=project_root, tests_project_rootdir=original_tests_project_rootdir
)
# Mock pytest discovery
with patch("codeflash.discovery.discover_unit_tests.discover_tests_pytest") as mock_discover:
mock_discover.return_value = ({}, 0, 0)
# Call discover_unit_tests
discover_unit_tests(test_cfg, file_to_funcs_to_optimize={})
# For Python, tests_project_rootdir should remain unchanged
# (the function doesn't modify it for Python projects)
assert test_cfg.tests_project_rootdir == original_tests_project_rootdir