Repository navigation
HDDS-17463. Publish GitHub Actions summary for coverage check #12054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec3aa8a
HDDS-17463. Introduce JaCoCo coverage report parser to summary markdown
kjs1095 40ff117
HDDS-17463. Introduce bats wrapper to run coverage_summary unit tests…
kjs1095 4988bfe
HDDS-17463. Add coverage summary to the coverage job step summary
kjs1095 11b54c6
HDDS-17463. Introduce package-level coverage table
kjs1095 5e66f0c
HDDS-17463. Never fail the coverage summary step on a missing or brok…
kjs1095 60df3f0
HDDS-17463. Revise 'Always exits 0' in the doc string due to unknown …
kjs1095 2240eb2
HDDS-17463. Revise test cases for 'sorted by missed value'
kjs1095 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.batsfile. (Or the test code will not be executed). It's unnecessary cognitive load.Happy to take this as a follow-up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File HDDS-17840.