Skip to content
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

Branch coverage #466

Merged
merged 10 commits into from
Oct 5, 2024
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ To run the end-to-end tests, you'll need:
- Please be aware that the tests will launch `gh auth setup-git` which might be
surprising if you use `https` remotes (sadly, setting `GIT_CONFIG_GLOBAL`
seems not to be enough to isolate tests.)

## Coverage labs

### Computing the coverage rate

The coverage rate is `covered_lines / total_lines` (as one would expect).
In case "branch coverage" is enabled, the coverage rate is
`(covered_lines + covered_branches) / (total_lines + total_branches)`.
In order to display coverage rates, we need to round the values. Depending on
the situation, we either round to 0 or 2 decimal places. Rounding rules are:
- We always round down (truncate) the value.
- We don't display the trailing zeros in the decimal part (nor the decimal point
if the decimal part is 0).
84 changes: 46 additions & 38 deletions coverage_comment/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import pathlib
from collections.abc import Sequence
from typing import cast

from coverage_comment import log, subprocess

Expand All @@ -28,10 +29,10 @@ class CoverageInfo:
percent_covered: decimal.Decimal
missing_lines: int
excluded_lines: int
num_branches: int | None
num_partial_branches: int | None
covered_branches: int | None
missing_branches: int | None
num_branches: int = 0
num_partial_branches: int = 0
covered_branches: int = 0
missing_branches: int = 0


@dataclasses.dataclass
Expand All @@ -41,6 +42,8 @@ class FileCoverage:
missing_lines: list[int]
excluded_lines: list[int]
info: CoverageInfo
executed_branches: list[list[int]] | None = None
missing_branches: list[list[int]] | None = None
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved


@dataclasses.dataclass
Expand Down Expand Up @@ -82,10 +85,20 @@ class DiffCoverage:
files: dict[pathlib.Path, FileDiffCoverage]


def compute_coverage(num_covered: int, num_total: int) -> decimal.Decimal:
if num_total == 0:
def compute_coverage(
num_covered: int,
num_total: int,
num_branches_covered: int = 0,
num_branches_total: int = 0,
) -> decimal.Decimal:
"""Compute the coverage percentage, with or without branch coverage."""
num_branches_covered = cast(int, num_branches_covered)
num_branches_total = cast(int, num_branches_total)
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved
numerator = decimal.Decimal(num_covered + num_branches_covered)
denominator = decimal.Decimal(num_total + num_branches_total)
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved
if denominator == 0:
return decimal.Decimal("1")
return decimal.Decimal(num_covered) / decimal.Decimal(num_total)
return numerator / denominator


def get_coverage_info(
Expand Down Expand Up @@ -138,6 +151,26 @@ def generate_coverage_markdown(coverage_path: pathlib.Path) -> str:
)


def _make_coverage_info(data: dict) -> CoverageInfo:
"""Build a CoverageInfo object from a "summary" or "totals" key."""
return CoverageInfo(
covered_lines=data["covered_lines"],
num_statements=data["num_statements"],
percent_covered=compute_coverage(
num_covered=data["covered_lines"],
num_total=data["num_statements"],
num_branches_covered=data.get("covered_branches", 0),
num_branches_total=data.get("num_branches", 0),
),
missing_lines=data["missing_lines"],
excluded_lines=data["excluded_lines"],
num_branches=data.get("num_branches"),
num_partial_branches=data.get("num_partial_branches"),
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved
covered_branches=data.get("covered_branches", 0),
missing_branches=data.get("missing_branches", 0),
)
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved


def extract_info(data: dict, coverage_path: pathlib.Path) -> Coverage:
"""
{
Expand Down Expand Up @@ -191,39 +224,13 @@ def extract_info(data: dict, coverage_path: pathlib.Path) -> Coverage:
excluded_lines=file_data["excluded_lines"],
executed_lines=file_data["executed_lines"],
missing_lines=file_data["missing_lines"],
info=CoverageInfo(
covered_lines=file_data["summary"]["covered_lines"],
num_statements=file_data["summary"]["num_statements"],
percent_covered=compute_coverage(
file_data["summary"]["covered_lines"],
file_data["summary"]["num_statements"],
),
missing_lines=file_data["summary"]["missing_lines"],
excluded_lines=file_data["summary"]["excluded_lines"],
num_branches=file_data["summary"].get("num_branches"),
num_partial_branches=file_data["summary"].get(
"num_partial_branches"
),
covered_branches=file_data["summary"].get("covered_branches"),
missing_branches=file_data["summary"].get("missing_branches"),
),
executed_branches=file_data.get("executed_branches"),
missing_branches=file_data.get("missing_branches"),
info=_make_coverage_info(file_data["summary"]),
)
for path, file_data in data["files"].items()
},
info=CoverageInfo(
covered_lines=data["totals"]["covered_lines"],
num_statements=data["totals"]["num_statements"],
percent_covered=compute_coverage(
data["totals"]["covered_lines"],
data["totals"]["num_statements"],
),
missing_lines=data["totals"]["missing_lines"],
excluded_lines=data["totals"]["excluded_lines"],
num_branches=data["totals"].get("num_branches"),
num_partial_branches=data["totals"].get("num_partial_branches"),
covered_branches=data["totals"].get("covered_branches"),
missing_branches=data["totals"].get("missing_branches"),
),
info=_make_coverage_info(data["totals"]),
)


Expand Down Expand Up @@ -256,7 +263,8 @@ def get_diff_coverage_info(
total_num_violations += count_missing

percent_covered = compute_coverage(
num_covered=count_executed, num_total=count_total
num_covered=count_executed,
num_total=count_total,
)

files[path] = FileDiffCoverage(
Expand Down
28 changes: 14 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ def _(code: str, has_branches: bool = True) -> coverage_module.Coverage:
percent_covered=decimal.Decimal("1.0"),
missing_lines=0,
excluded_lines=0,
num_branches=0 if has_branches else None,
num_partial_branches=0 if has_branches else None,
covered_branches=0 if has_branches else None,
missing_branches=0 if has_branches else None,
num_branches=0,
num_partial_branches=0,
covered_branches=0,
missing_branches=0,
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved
),
files={},
)
Expand Down Expand Up @@ -313,10 +313,10 @@ def _(code: str, has_branches: bool = True) -> coverage_module.Coverage:
percent_covered=decimal.Decimal("1.0"),
missing_lines=0,
excluded_lines=0,
num_branches=0 if has_branches else None,
num_partial_branches=0 if has_branches else None,
covered_branches=0 if has_branches else None,
missing_branches=0 if has_branches else None,
num_branches=0,
num_partial_branches=0,
covered_branches=0,
missing_branches=0,
),
)
if set(line.split()) & {
Expand All @@ -340,7 +340,6 @@ def _(code: str, has_branches: bool = True) -> coverage_module.Coverage:
coverage_obj.files[current_file].excluded_lines.append(line_number)
coverage_obj.files[current_file].info.excluded_lines += 1
coverage_obj.info.excluded_lines += 1

if has_branches and "branch" in line:
coverage_obj.files[current_file].info.num_branches += 1
coverage_obj.info.num_branches += 1
Expand All @@ -353,21 +352,22 @@ def _(code: str, has_branches: bool = True) -> coverage_module.Coverage:
elif "branch missing" in line:
coverage_obj.files[current_file].info.missing_branches += 1
coverage_obj.info.missing_branches += 1

info = coverage_obj.files[current_file].info
coverage_obj.files[
current_file
].info.percent_covered = coverage_module.compute_coverage(
num_covered=info.covered_lines,
num_total=info.num_statements,
num_branches_covered=info.covered_branches,
num_branches_total=info.num_branches,
)

info = coverage_obj.info
coverage_obj.info.percent_covered = coverage_module.compute_coverage(
num_covered=info.covered_lines,
num_total=info.num_statements,
num_branches_covered=info.covered_branches,
num_branches_total=info.num_branches,
)

return coverage_obj

return _
Expand Down Expand Up @@ -446,7 +446,7 @@ def coverage_json():
"summary": {
"covered_lines": 6,
"num_statements": 10,
"percent_covered": 60.0,
"percent_covered": 53.84615384615384615384615385,
"missing_lines": 4,
"excluded_lines": 0,
"num_branches": 3,
Expand All @@ -461,7 +461,7 @@ def coverage_json():
"totals": {
"covered_lines": 6,
"num_statements": 10,
"percent_covered": 60.0,
"percent_covered": 53.84615384615384615384615385,
ferdnyc marked this conversation as resolved.
Show resolved Hide resolved
"missing_lines": 4,
"excluded_lines": 0,
"num_branches": 3,
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def test_compute_coverage(num_covered, num_total, expected_coverage):
)


@pytest.mark.parametrize(
"num_covered, num_total, branch_covered, branch_total, expected_coverage",
[
(0, 10, 0, 15, "0"),
(0, 0, 0, 0, "1"),
(5, 0, 5, 0, "1"),
(5, 10, 5, 10, "0.5"),
(1, 50, 1, 50, "0.02"),
],
)
def test_compute_coverage_with_branches(
num_covered, num_total, branch_covered, branch_total, expected_coverage
):
assert coverage.compute_coverage(
num_covered, num_total, branch_covered, branch_total
) == decimal.Decimal(expected_coverage)


def test_get_coverage_info(mocker, coverage_json, coverage_obj):
run = mocker.patch(
"coverage_comment.subprocess.run", return_value=json.dumps(coverage_json)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_get_comment_markdown(coverage_obj, diff_coverage_obj):
.split(maxsplit=4)
)

expected = ["92%", "60%", "50%", "bar", "<!-- foo -->"]
expected = ["92%", "53.84%", "50%", "bar", "<!-- foo -->"]

assert result == expected

Expand Down Expand Up @@ -79,17 +79,17 @@ def test_template(coverage_obj, diff_coverage_obj):
expected = """## Coverage report (foo)


<img title="Coverage for the whole project went from 92% to 60%" src="https://img.shields.io/badge/Coverage%20evolution-92%25%20%3E%2060%25-red.svg"> <img title="50% of the statement lines added by this PR are covered" src="https://img.shields.io/badge/PR%20Coverage-50%25-orange.svg"><details><summary>Click to see where and how coverage changed</summary><table><thead>
<img title="Coverage for the whole project went from 92% to 53.84%" src="https://img.shields.io/badge/Coverage%20evolution-92%25%20%3E%2053%25-red.svg"> <img title="50% of the statement lines added by this PR are covered" src="https://img.shields.io/badge/PR%20Coverage-50%25-orange.svg"><details><summary>Click to see where and how coverage changed</summary><table><thead>
<tr><th>File</th><th>Statements</th><th>Missing</th><th>Coverage</th><th>Coverage<br>(new stmts)</th><th>Lines missing</th></tr>
</thead>
<tbody><tr>
<td colspan="6">&nbsp;&nbsp;<b>codebase</b></td><tr>
<td>&nbsp;&nbsp;<a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3">code.py</a></td>
<td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="This PR adds 10 statements to codebase/code.py. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/10-%28%2B10%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="This PR adds 4 statements missing coverage to codebase/code.py. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/4-%28%2B4%29-red.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="The coverage rate of codebase/code.py is 60% (6/10). The file did not seem to exist on the base branch." src="https://img.shields.io/badge/60%25-%286/10%29-orange.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="In this PR, 4 new statements are added to codebase/code.py, 2 of which are covered (50%)." src="https://img.shields.io/badge/50%25-%282/4%29-orange.svg"></a></td><td><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3R6-R8">6-8</a></td></tbody>
<td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="This PR adds 10 statements to codebase/code.py. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/10-%28%2B10%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="This PR adds 4 statements missing coverage to codebase/code.py. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/4-%28%2B4%29-red.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="The coverage rate of codebase/code.py is 53.84% (6/10). The file did not seem to exist on the base branch." src="https://img.shields.io/badge/53%25-%286/10%29-orange.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3"><img title="In this PR, 4 new statements are added to codebase/code.py, 2 of which are covered (50%)." src="https://img.shields.io/badge/50%25-%282/4%29-orange.svg"></a></td><td><a href="https://github.com/org/repo/pull/5/files#diff-c05d5557f0c1ff3761df2f49e3b541cfc161f4f0d63e2a66d568f090065bc3d3R6-R8">6-8</a></td></tbody>
<tfoot>
<tr>
<td><b>Project Total</b></td>
<td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 10 statements to the whole project. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/10-%28%2B10%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 4 statements missing coverage to the whole project. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/4-%28%2B4%29-red.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="The coverage rate of the whole project is 60% (6/10). The file did not seem to exist on the base branch." src="https://img.shields.io/badge/60%25-%286/10%29-orange.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="In this PR, 4 new statements are added to the whole project, 2 of which are covered (50%)." src="https://img.shields.io/badge/50%25-%282/4%29-orange.svg"></a></td><td>&nbsp;</td>
<td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 10 statements to the whole project. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/10-%28%2B10%29-007ec6.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="This PR adds 4 statements missing coverage to the whole project. The file did not seem to exist on the base branch." src="https://img.shields.io/badge/4-%28%2B4%29-red.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="The coverage rate of the whole project is 53.84% (6/10). The file did not seem to exist on the base branch." src="https://img.shields.io/badge/53%25-%286/10%29-orange.svg"></a></td><td align="center"><a href="https://github.com/org/repo/pull/5/files#diff-4b0bf2efa3367c0072ac2bf1e234e703dc46b47aaa4fe9d3b01737b1a15752b1"><img title="In this PR, 4 new statements are added to the whole project, 2 of which are covered (50%)." src="https://img.shields.io/badge/50%25-%282/4%29-orange.svg"></a></td><td>&nbsp;</td>
</tr>
</tfoot>
</table>
Expand Down
Loading