|
14 | 14 | from dataclasses import dataclass |
15 | 15 | from enum import Enum |
16 | 16 | from pathlib import Path |
17 | | -from typing import TYPE_CHECKING, Any |
18 | | - |
19 | | -if TYPE_CHECKING: |
20 | | - pass |
21 | 17 |
|
22 | 18 | logger = logging.getLogger(__name__) |
23 | 19 |
|
@@ -198,23 +194,23 @@ def _extract_java_version_from_pom(root: ET.Element, ns: dict[str, str]) -> str |
198 | 194 | """ |
199 | 195 | # Check properties |
200 | 196 | for prop_name in ("maven.compiler.source", "java.version", "maven.compiler.release"): |
201 | | - for props in [root.find(f"m:properties", ns), root.find("properties")]: |
| 197 | + for props in [root.find("m:properties", ns), root.find("properties")]: |
202 | 198 | if props is not None: |
203 | 199 | for prop in [props.find(f"m:{prop_name}", ns), props.find(prop_name)]: |
204 | 200 | if prop is not None and prop.text: |
205 | 201 | return prop.text |
206 | 202 |
|
207 | 203 | # Check compiler plugin configuration |
208 | | - for build in [root.find(f"m:build", ns), root.find("build")]: |
| 204 | + for build in [root.find("m:build", ns), root.find("build")]: |
209 | 205 | if build is not None: |
210 | | - for plugins in [build.find(f"m:plugins", ns), build.find("plugins")]: |
| 206 | + for plugins in [build.find("m:plugins", ns), build.find("plugins")]: |
211 | 207 | if plugins is not None: |
212 | | - for plugin in plugins.findall(f"m:plugin", ns) + plugins.findall("plugin"): |
213 | | - artifact_id = plugin.find(f"m:artifactId", ns) or plugin.find("artifactId") |
| 208 | + for plugin in plugins.findall("m:plugin", ns) + plugins.findall("plugin"): |
| 209 | + artifact_id = plugin.find("m:artifactId", ns) or plugin.find("artifactId") |
214 | 210 | if artifact_id is not None and artifact_id.text == "maven-compiler-plugin": |
215 | | - config = plugin.find(f"m:configuration", ns) or plugin.find("configuration") |
| 211 | + config = plugin.find("m:configuration", ns) or plugin.find("configuration") |
216 | 212 | if config is not None: |
217 | | - source = config.find(f"m:source", ns) or config.find("source") |
| 213 | + source = config.find("m:source", ns) or config.find("source") |
218 | 214 | if source is not None and source.text: |
219 | 215 | return source.text |
220 | 216 |
|
@@ -554,9 +550,8 @@ def install_codeflash_runtime(project_root: Path, runtime_jar_path: Path) -> boo |
554 | 550 | if result.returncode == 0: |
555 | 551 | logger.info("Successfully installed codeflash-runtime to local Maven repository") |
556 | 552 | return True |
557 | | - else: |
558 | | - logger.error("Failed to install codeflash-runtime: %s", result.stderr) |
559 | | - return False |
| 553 | + logger.error("Failed to install codeflash-runtime: %s", result.stderr) |
| 554 | + return False |
560 | 555 |
|
561 | 556 | except Exception as e: |
562 | 557 | logger.exception("Failed to install codeflash-runtime: %s", e) |
@@ -633,6 +628,160 @@ def add_codeflash_dependency_to_pom(pom_path: Path) -> bool: |
633 | 628 | return False |
634 | 629 |
|
635 | 630 |
|
| 631 | +JACOCO_PLUGIN_VERSION = "0.8.11" |
| 632 | + |
| 633 | + |
| 634 | +def is_jacoco_configured(pom_path: Path) -> bool: |
| 635 | + """Check if JaCoCo plugin is already configured in pom.xml. |
| 636 | +
|
| 637 | + Args: |
| 638 | + pom_path: Path to the pom.xml file. |
| 639 | +
|
| 640 | + Returns: |
| 641 | + True if JaCoCo plugin is configured, False otherwise. |
| 642 | +
|
| 643 | + """ |
| 644 | + if not pom_path.exists(): |
| 645 | + return False |
| 646 | + |
| 647 | + try: |
| 648 | + tree = ET.parse(pom_path) |
| 649 | + root = tree.getroot() |
| 650 | + |
| 651 | + # Handle Maven namespace |
| 652 | + ns = {"m": "http://maven.apache.org/POM/4.0.0"} |
| 653 | + ns_prefix = "{http://maven.apache.org/POM/4.0.0}" |
| 654 | + |
| 655 | + # Check if namespace is used |
| 656 | + use_ns = root.tag.startswith("{") |
| 657 | + if not use_ns: |
| 658 | + ns_prefix = "" |
| 659 | + |
| 660 | + # Find build/plugins section |
| 661 | + build = root.find(f"{ns_prefix}build" if use_ns else "build") |
| 662 | + if build is None: |
| 663 | + return False |
| 664 | + |
| 665 | + plugins = build.find(f"{ns_prefix}plugins" if use_ns else "plugins") |
| 666 | + if plugins is None: |
| 667 | + return False |
| 668 | + |
| 669 | + # Check for JaCoCo plugin |
| 670 | + for plugin in plugins.findall(f"{ns_prefix}plugin" if use_ns else "plugin"): |
| 671 | + group_id = plugin.find(f"{ns_prefix}groupId" if use_ns else "groupId") |
| 672 | + artifact_id = plugin.find(f"{ns_prefix}artifactId" if use_ns else "artifactId") |
| 673 | + if artifact_id is not None and artifact_id.text == "jacoco-maven-plugin": |
| 674 | + # Verify groupId if present (it's optional for org.jacoco) |
| 675 | + if group_id is None or group_id.text == "org.jacoco": |
| 676 | + return True |
| 677 | + |
| 678 | + return False |
| 679 | + |
| 680 | + except ET.ParseError as e: |
| 681 | + logger.warning("Failed to parse pom.xml for JaCoCo check: %s", e) |
| 682 | + return False |
| 683 | + |
| 684 | + |
| 685 | +def add_jacoco_plugin_to_pom(pom_path: Path) -> bool: |
| 686 | + """Add JaCoCo Maven plugin to pom.xml for coverage collection. |
| 687 | +
|
| 688 | + Args: |
| 689 | + pom_path: Path to the pom.xml file. |
| 690 | +
|
| 691 | + Returns: |
| 692 | + True if plugin was added or already present, False on error. |
| 693 | +
|
| 694 | + """ |
| 695 | + if not pom_path.exists(): |
| 696 | + logger.error("pom.xml not found: %s", pom_path) |
| 697 | + return False |
| 698 | + |
| 699 | + # Check if already configured |
| 700 | + if is_jacoco_configured(pom_path): |
| 701 | + logger.info("JaCoCo plugin already configured in pom.xml") |
| 702 | + return True |
| 703 | + |
| 704 | + try: |
| 705 | + tree = ET.parse(pom_path) |
| 706 | + root = tree.getroot() |
| 707 | + |
| 708 | + # Handle Maven namespace |
| 709 | + ns_prefix = "{http://maven.apache.org/POM/4.0.0}" |
| 710 | + |
| 711 | + # Check if namespace is used |
| 712 | + use_ns = root.tag.startswith("{") |
| 713 | + if not use_ns: |
| 714 | + ns_prefix = "" |
| 715 | + |
| 716 | + # Find or create build section |
| 717 | + build = root.find(f"{ns_prefix}build" if use_ns else "build") |
| 718 | + if build is None: |
| 719 | + build = ET.SubElement(root, f"{ns_prefix}build" if use_ns else "build") |
| 720 | + |
| 721 | + # Find or create plugins section |
| 722 | + plugins = build.find(f"{ns_prefix}plugins" if use_ns else "plugins") |
| 723 | + if plugins is None: |
| 724 | + plugins = ET.SubElement(build, f"{ns_prefix}plugins" if use_ns else "plugins") |
| 725 | + |
| 726 | + # Create JaCoCo plugin element |
| 727 | + plugin = ET.SubElement(plugins, f"{ns_prefix}plugin" if use_ns else "plugin") |
| 728 | + |
| 729 | + group_id = ET.SubElement(plugin, f"{ns_prefix}groupId" if use_ns else "groupId") |
| 730 | + group_id.text = "org.jacoco" |
| 731 | + |
| 732 | + artifact_id = ET.SubElement(plugin, f"{ns_prefix}artifactId" if use_ns else "artifactId") |
| 733 | + artifact_id.text = "jacoco-maven-plugin" |
| 734 | + |
| 735 | + version = ET.SubElement(plugin, f"{ns_prefix}version" if use_ns else "version") |
| 736 | + version.text = JACOCO_PLUGIN_VERSION |
| 737 | + |
| 738 | + # Create executions section |
| 739 | + executions = ET.SubElement(plugin, f"{ns_prefix}executions" if use_ns else "executions") |
| 740 | + |
| 741 | + # Add prepare-agent execution |
| 742 | + exec1 = ET.SubElement(executions, f"{ns_prefix}execution" if use_ns else "execution") |
| 743 | + exec1_id = ET.SubElement(exec1, f"{ns_prefix}id" if use_ns else "id") |
| 744 | + exec1_id.text = "prepare-agent" |
| 745 | + exec1_goals = ET.SubElement(exec1, f"{ns_prefix}goals" if use_ns else "goals") |
| 746 | + exec1_goal = ET.SubElement(exec1_goals, f"{ns_prefix}goal" if use_ns else "goal") |
| 747 | + exec1_goal.text = "prepare-agent" |
| 748 | + |
| 749 | + # Add report execution |
| 750 | + exec2 = ET.SubElement(executions, f"{ns_prefix}execution" if use_ns else "execution") |
| 751 | + exec2_id = ET.SubElement(exec2, f"{ns_prefix}id" if use_ns else "id") |
| 752 | + exec2_id.text = "report" |
| 753 | + exec2_phase = ET.SubElement(exec2, f"{ns_prefix}phase" if use_ns else "phase") |
| 754 | + exec2_phase.text = "test" |
| 755 | + exec2_goals = ET.SubElement(exec2, f"{ns_prefix}goals" if use_ns else "goals") |
| 756 | + exec2_goal = ET.SubElement(exec2_goals, f"{ns_prefix}goal" if use_ns else "goal") |
| 757 | + exec2_goal.text = "report" |
| 758 | + |
| 759 | + # Write back to file |
| 760 | + tree.write(pom_path, xml_declaration=True, encoding="utf-8") |
| 761 | + logger.info("Added JaCoCo plugin to pom.xml") |
| 762 | + return True |
| 763 | + |
| 764 | + except ET.ParseError as e: |
| 765 | + logger.error("Failed to parse pom.xml: %s", e) |
| 766 | + return False |
| 767 | + except Exception as e: |
| 768 | + logger.exception("Failed to add JaCoCo plugin to pom.xml: %s", e) |
| 769 | + return False |
| 770 | + |
| 771 | + |
| 772 | +def get_jacoco_xml_path(project_root: Path) -> Path: |
| 773 | + """Get the expected path to the JaCoCo XML report. |
| 774 | +
|
| 775 | + Args: |
| 776 | + project_root: Root directory of the Maven project. |
| 777 | +
|
| 778 | + Returns: |
| 779 | + Path to the JaCoCo XML report file. |
| 780 | +
|
| 781 | + """ |
| 782 | + return project_root / "target" / "site" / "jacoco" / "jacoco.xml" |
| 783 | + |
| 784 | + |
636 | 785 | def find_test_root(project_root: Path) -> Path | None: |
637 | 786 | """Find the test root directory for a Java project. |
638 | 787 |
|
|
0 commit comments