Skip to content

Commit ef535b8

Browse files
Merge pull request #2065 from codeflash-ai/fix/gradle-configure-on-demand
fix: add --configure-on-demand to all Gradle commands
2 parents d8b6236 + a7371b5 commit ef535b8

2 files changed

Lines changed: 92 additions & 8 deletions

File tree

codeflash/languages/java/gradle_strategy.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,15 @@ def install_multi_module_deps(self, build_root: Path, test_module: str | None, e
533533
logger.error("Gradle not found — cannot pre-install multi-module dependencies")
534534
return False
535535

536-
cmd = [gradle, f":{test_module}:testClasses", "-x", "test", "--build-cache", "--no-daemon"]
536+
cmd = [
537+
gradle,
538+
f":{test_module}:testClasses",
539+
"-x",
540+
"test",
541+
"--build-cache",
542+
"--no-daemon",
543+
"--configure-on-demand",
544+
]
537545
cmd.extend(["--init-script", _get_skip_validation_init_script()])
538546

539547
logger.info("Pre-installing multi-module dependencies: %s (module: %s)", build_root, test_module)
@@ -568,9 +576,9 @@ def compile_tests(
568576
return subprocess.CompletedProcess(args=["gradle"], returncode=-1, stdout="", stderr="Gradle not found")
569577

570578
if test_module:
571-
cmd = [gradle, f":{test_module}:testClasses", "--no-daemon"]
579+
cmd = [gradle, f":{test_module}:testClasses", "--no-daemon", "--configure-on-demand"]
572580
else:
573-
cmd = [gradle, "testClasses", "--no-daemon"]
581+
cmd = [gradle, "testClasses", "--no-daemon", "--configure-on-demand"]
574582
cmd.extend(["--init-script", _get_skip_validation_init_script()])
575583

576584
logger.debug("Compiling tests: %s in %s", " ".join(cmd), build_root)
@@ -592,9 +600,9 @@ def compile_source_only(
592600
return subprocess.CompletedProcess(args=["gradle"], returncode=-1, stdout="", stderr="Gradle not found")
593601

594602
if test_module:
595-
cmd = [gradle, f":{test_module}:classes", "--no-daemon"]
603+
cmd = [gradle, f":{test_module}:classes", "--no-daemon", "--configure-on-demand"]
596604
else:
597-
cmd = [gradle, "classes", "--no-daemon"]
605+
cmd = [gradle, "classes", "--no-daemon", "--configure-on-demand"]
598606
cmd.extend(["--init-script", _get_skip_validation_init_script()])
599607

600608
logger.debug("Compiling source only: %s in %s", " ".join(cmd), build_root)
@@ -638,7 +646,7 @@ def _get_classpath_uncached(
638646
else:
639647
task = "codeflashPrintClasspath"
640648

641-
cmd = [gradle, "--init-script", init_script_path, task, "-q", "--no-daemon"]
649+
cmd = [gradle, "--init-script", init_script_path, task, "-q", "--no-daemon", "--configure-on-demand"]
642650

643651
logger.debug("Getting classpath: %s", " ".join(cmd))
644652

@@ -789,7 +797,7 @@ def run_tests_via_build_tool(
789797
with os.fdopen(init_fd, "w", encoding="utf-8") as f:
790798
f.write(init_script_content)
791799

792-
cmd = [gradle, task, "--no-daemon", "--rerun", "--init-script", init_path]
800+
cmd = [gradle, task, "--no-daemon", "--rerun", "--configure-on-demand", "--init-script", init_path]
793801
cmd.extend(["--init-script", _get_skip_validation_init_script()])
794802

795803
for class_filter in test_filter.split(","):
@@ -1044,7 +1052,7 @@ def get_test_run_command(self, project_root: Path, test_classes: list[str] | Non
10441052
raise ValueError(msg)
10451053

10461054
gradle = self.find_executable(project_root) or "gradle"
1047-
cmd = [gradle, "test", "--no-daemon"]
1055+
cmd = [gradle, "test", "--no-daemon", "--configure-on-demand"]
10481056
if test_classes:
10491057
for cls in test_classes:
10501058
cmd.extend(["--tests", cls])
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
from unittest.mock import MagicMock, patch
5+
6+
from codeflash.languages.java.gradle_strategy import GradleStrategy
7+
8+
if TYPE_CHECKING:
9+
from pathlib import Path
10+
11+
COD_FLAG = "--configure-on-demand"
12+
MOCK_TARGET = "codeflash.languages.java.test_runner._run_cmd_kill_pg_on_timeout"
13+
14+
15+
class TestConfigureOnDemand:
16+
def test_compile_tests_includes_configure_on_demand(self, tmp_path: Path) -> None:
17+
strategy = GradleStrategy()
18+
with patch.object(strategy, "find_executable", return_value="gradlew"), patch(MOCK_TARGET) as mock_run:
19+
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
20+
strategy.compile_tests(tmp_path, {}, test_module=None)
21+
cmd = mock_run.call_args[0][0]
22+
assert COD_FLAG in cmd
23+
24+
def test_compile_tests_multimodule_includes_configure_on_demand(self, tmp_path: Path) -> None:
25+
strategy = GradleStrategy()
26+
with patch.object(strategy, "find_executable", return_value="gradlew"), patch(MOCK_TARGET) as mock_run:
27+
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
28+
strategy.compile_tests(tmp_path, {}, test_module="core")
29+
cmd = mock_run.call_args[0][0]
30+
assert COD_FLAG in cmd
31+
assert ":core:testClasses" in cmd
32+
33+
def test_compile_source_only_includes_configure_on_demand(self, tmp_path: Path) -> None:
34+
strategy = GradleStrategy()
35+
with patch.object(strategy, "find_executable", return_value="gradlew"), patch(MOCK_TARGET) as mock_run:
36+
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
37+
strategy.compile_source_only(tmp_path, {}, test_module=None)
38+
cmd = mock_run.call_args[0][0]
39+
assert COD_FLAG in cmd
40+
41+
def test_get_test_run_command_includes_configure_on_demand(self, tmp_path: Path) -> None:
42+
strategy = GradleStrategy()
43+
with patch.object(strategy, "find_executable", return_value="gradlew"):
44+
cmd = strategy.get_test_run_command(tmp_path)
45+
assert COD_FLAG in cmd
46+
47+
def test_install_multi_module_deps_includes_configure_on_demand(self, tmp_path: Path) -> None:
48+
strategy = GradleStrategy()
49+
with (
50+
patch.object(strategy, "find_executable", return_value="gradlew"),
51+
patch(MOCK_TARGET) as mock_run,
52+
patch("codeflash.languages.java.gradle_strategy._multimodule_deps_installed", set()),
53+
):
54+
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
55+
strategy.install_multi_module_deps(tmp_path, test_module="core", env={})
56+
cmd = mock_run.call_args[0][0]
57+
assert COD_FLAG in cmd
58+
assert ":core:testClasses" in cmd
59+
60+
def test_run_tests_via_build_tool_includes_configure_on_demand(self, tmp_path: Path) -> None:
61+
strategy = GradleStrategy()
62+
reports_dir = tmp_path / "build" / "test-results" / "test"
63+
reports_dir.mkdir(parents=True, exist_ok=True)
64+
65+
with patch.object(strategy, "find_executable", return_value="gradlew"), patch(MOCK_TARGET) as mock_run:
66+
mock_run.return_value = MagicMock(returncode=0, stdout="", stderr="")
67+
strategy.run_tests_via_build_tool(
68+
build_root=tmp_path,
69+
test_paths=["com.example.TestFoo"],
70+
env={},
71+
timeout=60,
72+
mode="behavior",
73+
test_module=None,
74+
)
75+
cmd = mock_run.call_args[0][0]
76+
assert COD_FLAG in cmd

0 commit comments

Comments
 (0)