|
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,236 @@ 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 | + Checks both the main build section and any profile build sections. |
| 638 | +
|
| 639 | + Args: |
| 640 | + pom_path: Path to the pom.xml file. |
| 641 | +
|
| 642 | + Returns: |
| 643 | + True if JaCoCo plugin is configured anywhere in the pom.xml, False otherwise. |
| 644 | +
|
| 645 | + """ |
| 646 | + if not pom_path.exists(): |
| 647 | + return False |
| 648 | + |
| 649 | + try: |
| 650 | + tree = ET.parse(pom_path) |
| 651 | + root = tree.getroot() |
| 652 | + |
| 653 | + # Handle Maven namespace |
| 654 | + ns_prefix = "{http://maven.apache.org/POM/4.0.0}" |
| 655 | + |
| 656 | + # Check if namespace is used |
| 657 | + use_ns = root.tag.startswith("{") |
| 658 | + if not use_ns: |
| 659 | + ns_prefix = "" |
| 660 | + |
| 661 | + # Search all build/plugins sections (including those in profiles) |
| 662 | + # Using .// to search recursively for all plugin elements |
| 663 | + for plugin in root.findall(f".//{ns_prefix}plugin" if use_ns else ".//plugin"): |
| 664 | + artifact_id = plugin.find(f"{ns_prefix}artifactId" if use_ns else "artifactId") |
| 665 | + if artifact_id is not None and artifact_id.text == "jacoco-maven-plugin": |
| 666 | + group_id = plugin.find(f"{ns_prefix}groupId" if use_ns else "groupId") |
| 667 | + # Verify groupId if present (it's optional for org.jacoco) |
| 668 | + if group_id is None or group_id.text == "org.jacoco": |
| 669 | + return True |
| 670 | + |
| 671 | + return False |
| 672 | + |
| 673 | + except ET.ParseError as e: |
| 674 | + logger.warning("Failed to parse pom.xml for JaCoCo check: %s", e) |
| 675 | + return False |
| 676 | + |
| 677 | + |
| 678 | +def add_jacoco_plugin_to_pom(pom_path: Path) -> bool: |
| 679 | + """Add JaCoCo Maven plugin to pom.xml for coverage collection. |
| 680 | +
|
| 681 | + Uses string manipulation to preserve the original XML format and avoid |
| 682 | + namespace prefix issues that ElementTree causes. |
| 683 | +
|
| 684 | + Args: |
| 685 | + pom_path: Path to the pom.xml file. |
| 686 | +
|
| 687 | + Returns: |
| 688 | + True if plugin was added or already present, False on error. |
| 689 | +
|
| 690 | + """ |
| 691 | + if not pom_path.exists(): |
| 692 | + logger.error("pom.xml not found: %s", pom_path) |
| 693 | + return False |
| 694 | + |
| 695 | + # Check if already configured |
| 696 | + if is_jacoco_configured(pom_path): |
| 697 | + logger.info("JaCoCo plugin already configured in pom.xml") |
| 698 | + return True |
| 699 | + |
| 700 | + try: |
| 701 | + content = pom_path.read_text(encoding="utf-8") |
| 702 | + |
| 703 | + # Basic validation that it's a Maven pom.xml |
| 704 | + if "</project>" not in content: |
| 705 | + logger.error("Invalid pom.xml: no closing </project> tag found") |
| 706 | + return False |
| 707 | + |
| 708 | + # JaCoCo plugin XML to insert (indented for typical pom.xml format) |
| 709 | + jacoco_plugin = """ |
| 710 | + <plugin> |
| 711 | + <groupId>org.jacoco</groupId> |
| 712 | + <artifactId>jacoco-maven-plugin</artifactId> |
| 713 | + <version>{version}</version> |
| 714 | + <executions> |
| 715 | + <execution> |
| 716 | + <id>prepare-agent</id> |
| 717 | + <goals> |
| 718 | + <goal>prepare-agent</goal> |
| 719 | + </goals> |
| 720 | + </execution> |
| 721 | + <execution> |
| 722 | + <id>report</id> |
| 723 | + <phase>verify</phase> |
| 724 | + <goals> |
| 725 | + <goal>report</goal> |
| 726 | + </goals> |
| 727 | + </execution> |
| 728 | + </executions> |
| 729 | + </plugin>""".format(version=JACOCO_PLUGIN_VERSION) |
| 730 | + |
| 731 | + # Find the main <build> section (not inside <profiles>) |
| 732 | + # We need to find a <build> that appears after </profiles> or before <profiles> |
| 733 | + # or if there's no profiles section at all |
| 734 | + profiles_start = content.find("<profiles>") |
| 735 | + profiles_end = content.find("</profiles>") |
| 736 | + |
| 737 | + # Find all <build> tags |
| 738 | + import re |
| 739 | + |
| 740 | + # Find the main build section - it's the one NOT inside profiles |
| 741 | + # Strategy: Look for <build> that comes after </profiles> or before <profiles> (or no profiles) |
| 742 | + if profiles_start == -1: |
| 743 | + # No profiles, any <build> is the main one |
| 744 | + build_start = content.find("<build>") |
| 745 | + build_end = content.find("</build>") |
| 746 | + else: |
| 747 | + # Has profiles - find <build> outside of profiles |
| 748 | + # Check for <build> before <profiles> |
| 749 | + build_before_profiles = content[:profiles_start].rfind("<build>") |
| 750 | + # Check for <build> after </profiles> |
| 751 | + build_after_profiles = content[profiles_end:].find("<build>") if profiles_end != -1 else -1 |
| 752 | + if build_after_profiles != -1: |
| 753 | + build_after_profiles += profiles_end |
| 754 | + |
| 755 | + if build_before_profiles != -1: |
| 756 | + build_start = build_before_profiles |
| 757 | + # Find corresponding </build> - need to handle nested builds |
| 758 | + build_end = _find_closing_tag(content, build_start, "build") |
| 759 | + elif build_after_profiles != -1: |
| 760 | + build_start = build_after_profiles |
| 761 | + build_end = _find_closing_tag(content, build_start, "build") |
| 762 | + else: |
| 763 | + build_start = -1 |
| 764 | + build_end = -1 |
| 765 | + |
| 766 | + if build_start != -1 and build_end != -1: |
| 767 | + # Found main build section, find plugins within it |
| 768 | + build_section = content[build_start:build_end + len("</build>")] |
| 769 | + plugins_start_in_build = build_section.find("<plugins>") |
| 770 | + plugins_end_in_build = build_section.rfind("</plugins>") |
| 771 | + |
| 772 | + if plugins_start_in_build != -1 and plugins_end_in_build != -1: |
| 773 | + # Insert before </plugins> within the main build section |
| 774 | + absolute_plugins_end = build_start + plugins_end_in_build |
| 775 | + content = content[:absolute_plugins_end] + jacoco_plugin + "\n " + content[absolute_plugins_end:] |
| 776 | + else: |
| 777 | + # No plugins section in main build, add one before </build> |
| 778 | + plugins_section = f"<plugins>{jacoco_plugin}\n </plugins>\n " |
| 779 | + content = content[:build_end] + plugins_section + content[build_end:] |
| 780 | + else: |
| 781 | + # No main build section found, add one before </project> |
| 782 | + project_end = content.rfind("</project>") |
| 783 | + build_section = f""" |
| 784 | + <build> |
| 785 | + <plugins>{jacoco_plugin} |
| 786 | + </plugins> |
| 787 | + </build> |
| 788 | +""" |
| 789 | + content = content[:project_end] + build_section + content[project_end:] |
| 790 | + |
| 791 | + pom_path.write_text(content, encoding="utf-8") |
| 792 | + logger.info("Added JaCoCo plugin to pom.xml") |
| 793 | + return True |
| 794 | + |
| 795 | + except Exception as e: |
| 796 | + logger.exception("Failed to add JaCoCo plugin to pom.xml: %s", e) |
| 797 | + return False |
| 798 | + |
| 799 | + |
| 800 | +def _find_closing_tag(content: str, start_pos: int, tag_name: str) -> int: |
| 801 | + """Find the position of the closing tag that matches the opening tag at start_pos. |
| 802 | +
|
| 803 | + Handles nested tags of the same name. |
| 804 | +
|
| 805 | + Args: |
| 806 | + content: The XML content. |
| 807 | + start_pos: Position of the opening tag. |
| 808 | + tag_name: Name of the tag. |
| 809 | +
|
| 810 | + Returns: |
| 811 | + Position of the closing tag, or -1 if not found. |
| 812 | +
|
| 813 | + """ |
| 814 | + open_tag = f"<{tag_name}>" |
| 815 | + open_tag_short = f"<{tag_name} " # For tags with attributes |
| 816 | + close_tag = f"</{tag_name}>" |
| 817 | + |
| 818 | + # Start searching after the opening tag we're matching |
| 819 | + depth = 1 # We've already found the opening tag at start_pos |
| 820 | + pos = start_pos + len(f"<{tag_name}") # Move past the opening tag |
| 821 | + |
| 822 | + while pos < len(content): |
| 823 | + next_open = content.find(open_tag, pos) |
| 824 | + next_open_short = content.find(open_tag_short, pos) |
| 825 | + next_close = content.find(close_tag, pos) |
| 826 | + |
| 827 | + if next_close == -1: |
| 828 | + return -1 |
| 829 | + |
| 830 | + # Find the earliest opening tag (if any) |
| 831 | + candidates = [x for x in [next_open, next_open_short] if x != -1 and x < next_close] |
| 832 | + next_open_any = min(candidates) if candidates else len(content) + 1 |
| 833 | + |
| 834 | + if next_open_any < next_close: |
| 835 | + # Found opening tag first - nested tag |
| 836 | + depth += 1 |
| 837 | + pos = next_open_any + 1 |
| 838 | + else: |
| 839 | + # Found closing tag first |
| 840 | + depth -= 1 |
| 841 | + if depth == 0: |
| 842 | + return next_close |
| 843 | + pos = next_close + len(close_tag) |
| 844 | + |
| 845 | + return -1 |
| 846 | + |
| 847 | + |
| 848 | +def get_jacoco_xml_path(project_root: Path) -> Path: |
| 849 | + """Get the expected path to the JaCoCo XML report. |
| 850 | +
|
| 851 | + Args: |
| 852 | + project_root: Root directory of the Maven project. |
| 853 | +
|
| 854 | + Returns: |
| 855 | + Path to the JaCoCo XML report file. |
| 856 | +
|
| 857 | + """ |
| 858 | + return project_root / "target" / "site" / "jacoco" / "jacoco.xml" |
| 859 | + |
| 860 | + |
636 | 861 | def find_test_root(project_root: Path) -> Path | None: |
637 | 862 | """Find the test root directory for a Java project. |
638 | 863 |
|
|
0 commit comments