Skip to content

Commit

Permalink
Merge pull request #435 from py-cov-action/long-numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim authored Jun 28, 2024
2 parents 6471543 + 1dda329 commit 1fb87f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
11 changes: 11 additions & 0 deletions coverage_comment/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def delta(val: int) -> str:
return f"({sign(val)}{val})"


def compact(val: int) -> str:
if val < 1_000:
return str(val)
if val < 10_000:
return f"{val / 1_000:.1f}k"
if val < 1_000_000:
return f"{val / 1_000:.0f}k"
return f"{val / 1_000_000:.0f}M"


def remove_exponent(val: decimal.Decimal) -> decimal.Decimal:
# From https://docs.python.org/3/library/decimal.html#decimal-faq
return (
Expand Down Expand Up @@ -136,6 +146,7 @@ def get_comment_markdown(
env.filters["get_evolution_color"] = badge.get_evolution_badge_color
env.filters["generate_badge"] = badge.get_static_badge_url
env.filters["pluralize"] = pluralize
env.filters["compact"] = compact
env.filters["file_url"] = functools.partial(
get_file_url, repo_name=repo_name, pr_number=pr_number
)
Expand Down
10 changes: 5 additions & 5 deletions coverage_comment/template_files/comment.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{% set color = "007ec6" -%}
{% set message = statements_count %}
{% endif -%}
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ statements_count | string | generate_badge(message=(message | delta), color=color) }}"></a></td>
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ statements_count | compact | generate_badge(message=(message | delta), color=color) }}"></a></td>

{%- endmacro -%}

Expand All @@ -65,7 +65,7 @@
{%- set message = missing_lines_count -%}
{%- endif -%}
{%- set color = message | get_evolution_color(up_is_good=false) -%}
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ missing_lines_count | string | generate_badge(message=(message | delta), color=color) }}"></a></td>
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ missing_lines_count | compact | generate_badge(message=(message | delta), color=color) }}"></a></td>

{%- endmacro -%}

Expand All @@ -80,10 +80,10 @@
{%- set text = "This PR doesn't change the coverage rate in " ~ path ~ ", which is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ ")." -%}
{%- endif -%}
{%- set color = coverage_diff | get_evolution_color() -%}
{%- set message = "(" ~ previous_covered_statements_count ~ "/" ~ previous_statements_count ~ " > " ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%}
{%- set message = "(" ~ previous_covered_statements_count | compact ~ "/" ~ previous_statements_count | compact ~ " > " ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%}
{%- else -%}
{%- set text = "The coverage rate of " ~ path ~ " is " ~ percent_covered | pct ~ " (" ~ covered_statements_count ~ "/" ~ statements_count ~ "). The file did not seem to exist on the base branch." -%}
{%- set message = "(" ~ covered_statements_count ~ "/" ~ statements_count ~ ")" -%}
{%- set message = "(" ~ covered_statements_count | compact ~ "/" ~ statements_count | compact ~ ")" -%}
{%- set color = percent_covered | x100 | get_badge_color -%}
{%- endif -%}
<td align="center"><a href="{{ path | file_url }}"><img title="{{ text }}" src="{{ percent_covered | pct(precision=0) | generate_badge(message=message, color=color) }}"></a></td>
Expand All @@ -94,7 +94,7 @@
{% if added_statements_count -%}
{% set text = "In this PR, " ~ (added_statements_count) ~ " new statements are added to " ~ path ~ ", " ~ covered_statements_count ~ " of which are covered (" ~ (percent_covered | pct) ~ ")." -%}
{% set label = (percent_covered | pct(precision=0)) -%}
{% set message = "(" ~ covered_statements_count ~ "/" ~ added_statements_count ~ ")" -%}
{% set message = "(" ~ covered_statements_count | compact ~ "/" ~ added_statements_count | compact ~ ")" -%}
{%- set color = (percent_covered | x100 | get_badge_color()) -%}
{% else -%}
{% set text = "This PR does not seem to add statements to " ~ path ~ "." -%}
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,20 @@ def test_get_log_message():
subproject_id="foo",
)
assert result.startswith("Coverage info for foo:")


@pytest.mark.parametrize(
"value, expected",
[
(0, "0"),
(1, "1"),
(999, "999"),
(1_042, "1.0k"),
(9_900, "9.9k"),
(12_345, "12k"),
(999_999, "1000k"),
(1_234_567, "1M"),
],
)
def test_compact(value, expected):
assert template.compact(value) == expected

0 comments on commit 1fb87f8

Please sign in to comment.