Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,15 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
- name: Archive build results
id: archive
uses: actions/upload-artifact@v7
with:
name: coverage
path: target/coverage
continue-on-error: true
- name: Coverage summary
if: ${{ !cancelled() }}
continue-on-error: true
run: python3 dev-support/ci/coverage_summary.py >> "$GITHUB_STEP_SUMMARY"
env:
COVERAGE_REPORT_URL: ${{ steps.archive.outputs.artifact-url }}
22 changes: 22 additions & 0 deletions dev-support/ci/coverage_summary.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

@test "coverage_summary.py unit tests" {
cd "${BATS_TEST_DIRNAME}"
run python3 -m unittest test_coverage_summary
[ "$status" -eq 0 ]
Comment on lines +18 to +21

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.

For follow-up: I think we should add a separate check for Python tests to avoid the need to hook these into the bats check.

@kjs1095 kjs1095 Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

While checking dev-support/ci/selective_ci_checks.sh, contributors need to understand that any new Python utility requires its own .bats file. (Or the test code will not be executed). It's unnecessary cognitive load.

Happy to take this as a follow-up!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

File HDDS-17840.

}
161 changes: 161 additions & 0 deletions dev-support/ci/coverage_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env python3
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Parse the JaCoCo XML report generated by coverage.sh and print a Markdown coverage summary.

Intended for GitHub Actions: coverage_summary.py [--report FILE] >> "$GITHUB_STEP_SUMMARY"
Exits 0: a missing or unreadable report is logged to stderr and nothing is written to stdout.

JaCoCo XML report structure (as produced for Ozone's all.xml):

<report name="...">
<sessioninfo id="..." start="..." dump="..."/> # one per test JVM
<package name="org/apache/hadoop/ozone/om">
<class name="org/apache/.../OzoneManager" # include inner classes (Foo$Bar)
sourcefilename="OzoneManager.java">
<method name="<init>" desc="(...)V" line="70">
<counter .../>
</method>
<counter .../>
</class>
<sourcefile name="OzoneManager.java"> # one per .java file
<line nr="30" mi="0" ci="3" mb="0" cb="0"/> # per-line: missed/covered
# instructions & branches
<counter .../>
</sourcefile>
<counter .../> # package totals
</package>
<counter .../> # report (overall) totals
</report>

<counter type="LINE" missed="27270" covered="148570"/>
type: INSTRUCTION | BRANCH | COMPLEXITY | LINE | METHOD | CLASS
Ref: https://www.jacoco.org/jacoco/trunk/doc/counters.html

A counter is omitted when its total is 0 (e.g. a method with no branches), so a
missing counter means n/a, not 0% coverage.

Every level carries its own aggregated <counter> elements, so we read the direct
children of <report> and of each <package> instead of summing.
"""
import argparse
import dataclasses
import os
import sys
import xml.etree.ElementTree as ET

DEFAULT_REPORT = os.path.join("target", "coverage", "all.xml")

# INSTRUCTION and COMPLEXITY are left out on purpose: they track LINE and BRANCH
# within a fraction of a percent, so they would only add noise to the summary.
JACOCO_LINE = "LINE"
JACOCO_BRANCH = "BRANCH"
JACOCO_METHOD = "METHOD"
JACOCO_CLASS = "CLASS"


@dataclasses.dataclass
class Counter:
missed: int = 0
covered: int = 0

@property
def total(self):
return self.missed + self.covered


@dataclasses.dataclass
class Coverage:
"""Counters of one JaCoCo report node, keyed by counter type."""
name: str
counters: dict

def counter(self, counter_type):
return self.counters.get(counter_type, Counter())

@property
def missed(self):
# sort key for the package table: amount of untested code, not ratio, so tiny packages do not top the list
return self.counter(JACOCO_LINE).missed + self.counter(JACOCO_BRANCH).missed

@property
def combined(self):
# same formula as SonarCloud's "coverage" metric: (covered lines + covered conditions) / (lines + conditions),
# where JaCoCo branches are Sonar's conditions
lines, branches = self.counter(JACOCO_LINE), self.counter(JACOCO_BRANCH)
return percent(lines.covered + branches.covered, lines.total + branches.total)


def percent(covered, total):
return "%.1f%%" % (100.0 * covered / total) if total else "n/a"


def read_counters(elem):
# Only the direct children: <package>, <class> and <method> nest their own counters.
return {c.get("type"): Counter(int(c.get("missed") or 0), int(c.get("covered") or 0))
for c in elem.findall("counter")}


def parse_report(xml_path):
root = ET.parse(xml_path).getroot()
packages = [Coverage(p.get("name", "").replace("/", "."), read_counters(p)) for p in root.findall("package")]
return Coverage("", read_counters(root)), packages


def render_summary(overall, packages):
lines, branches = overall.counter(JACOCO_LINE), overall.counter(JACOCO_BRANCH)
out = ["## Coverage Summary", ""]
out.append(f"**Overall coverage: {overall.combined}** (lines and branches combined, same formula as SonarCloud)")
out.append("")
out.append("|Counter|Coverage|Missed|Total|")
out.append("|---|---|---|---|")
out.append(f"|Lines|{percent(lines.covered, lines.total)}|{lines.missed}|{lines.total}|")
out.append(f"|Branches|{percent(branches.covered, branches.total)}|{branches.missed}|{branches.total}|")
out.append("")
out.append(f"Untested: {overall.counter(JACOCO_METHOD).missed} methods, "
f"{overall.counter(JACOCO_CLASS).missed} classes")
out.append("")
report_url = os.environ.get("COVERAGE_REPORT_URL")
if report_url:
out.extend([f"📦 Full HTML report: [download artifact]({report_url})", ""])
if packages:
out.extend([f"<details><summary><b>Coverage by package ({len(packages)})</b></summary>", ""])
out.append("|Package|Coverage|Lines|Missed lines|Branches|Missed branches|")
out.append("|---|---|---|---|---|---|")
for p in sorted(packages, key=lambda p: (-p.missed, p.name)):
p_line, p_branch = p.counter(JACOCO_LINE), p.counter(JACOCO_BRANCH)
out.append(f"|{p.name}|{p.combined}|{percent(p_line.covered, p_line.total)}|{p_line.missed}|"
f"{percent(p_branch.covered, p_branch.total)}|{p_branch.missed}|")
out.extend(["", "</details>", ""])
return "\n".join(out) + "\n"


def main(argv=None):
parser = argparse.ArgumentParser(description="Print a Markdown summary of a JaCoCo XML coverage report.")
parser.add_argument("--report", default=DEFAULT_REPORT, help="path to the JaCoCo XML report")
args = parser.parse_args(argv)
try:
overall, packages = parse_report(args.report)
print(render_summary(overall, packages), end="")
except Exception as e:
print(f"coverage_summary failed: {e}", file=sys.stderr)
return 0


if __name__ == "__main__":
sys.exit(main())
Loading