From de5fc7220ce687857a5248de1cdc22c5a6faece5 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Tue, 7 May 2024 17:27:52 +0200 Subject: [PATCH] PNG badges in coverage comment --- coverage_comment/badge.py | 10 ++++++++-- coverage_comment/template.py | 6 +++++- tests/end_to_end/test_all.py | 4 ++-- tests/unit/test_badge.py | 4 +++- tests/unit/test_template.py | 17 +++++++++-------- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/coverage_comment/badge.py b/coverage_comment/badge.py index cd2d2305..3ccc5794 100644 --- a/coverage_comment/badge.py +++ b/coverage_comment/badge.py @@ -8,6 +8,7 @@ import decimal import json import urllib.parse +from typing import Literal import httpx @@ -67,13 +68,18 @@ def compute_badge_image( ).text -def get_static_badge_url(label: str, message: str, color: str) -> str: +def get_static_badge_url( + label: str, + message: str, + color: str, + format: Literal["svg", "png"] = "png", +) -> str: if not color or not message: raise ValueError("color and message are required") code = "-".join( e.replace("_", "__").replace("-", "--") for e in (label, message, color) if e ) - return "https://img.shields.io/badge/" + urllib.parse.quote(f"{code}.svg") + return "https://img.shields.io/badge/" + urllib.parse.quote(f"{code}.{format}") def get_endpoint_url(endpoint_url: str) -> str: diff --git a/coverage_comment/template.py b/coverage_comment/template.py index 7f682aab..51610d74 100644 --- a/coverage_comment/template.py +++ b/coverage_comment/template.py @@ -8,6 +8,7 @@ import pathlib from collections.abc import Callable from importlib import resources +from typing import Literal import jinja2 from jinja2.sandbox import SandboxedEnvironment @@ -127,6 +128,7 @@ def get_comment_markdown( subproject_id: str | None = None, custom_template: str | None = None, pr_targets_default_branch: bool = True, + image_format: Literal["svg", "png"] = "png", ): loader = CommentLoader(base_template=base_template, custom_template=custom_template) env = SandboxedEnvironment(loader=loader) @@ -134,7 +136,9 @@ def get_comment_markdown( env.filters["delta"] = delta env.filters["x100"] = x100 env.filters["get_evolution_color"] = badge.get_evolution_badge_color - env.filters["generate_badge"] = badge.get_static_badge_url + env.filters["generate_badge"] = functools.partial( + badge.get_static_badge_url, format=image_format + ) env.filters["pluralize"] = pluralize env.filters["file_url"] = functools.partial( get_file_url, repo_name=repo_name, pr_number=pr_number diff --git a/tests/end_to_end/test_all.py b/tests/end_to_end/test_all.py index 5ca63c2f..b4dcf11c 100644 --- a/tests/end_to_end/test_all.py +++ b/tests/end_to_end/test_all.py @@ -183,7 +183,7 @@ def test_public_repo( fail_value="\n", ) - assert "-brightgreen.svg" in ext_comment + assert "-brightgreen.png" in ext_comment @pytest.mark.repo_suffix("private") @@ -274,7 +274,7 @@ def test_private_repo( "--jq=.comments[0].body", fail_value="\n", ) - assert "-brightgreen.svg" in comment + assert "-brightgreen.png" in comment # Let's merge the PR and see if everything works fine gh_me("pr", "merge", "1", "--merge") diff --git a/tests/unit/test_badge.py b/tests/unit/test_badge.py index 34d27731..3f178497 100644 --- a/tests/unit/test_badge.py +++ b/tests/unit/test_badge.py @@ -66,7 +66,9 @@ def test_compute_badge_image(session): def test_get_static_badge_url(): - result = badge.get_static_badge_url(label="a-b", message="c_d e", color="green") + result = badge.get_static_badge_url( + label="a-b", message="c_d e", color="green", format="svg" + ) assert result == "https://img.shields.io/badge/a--b-c__d%20e-green.svg" diff --git a/tests/unit/test_template.py b/tests/unit/test_template.py index 1e60c18c..9c72dd2d 100644 --- a/tests/unit/test_template.py +++ b/tests/unit/test_template.py @@ -74,6 +74,7 @@ def test_template(coverage_obj, diff_coverage_obj): custom_template="""{% extends "base" %} {% block emoji_coverage_down %}:sob:{% endblock emoji_coverage_down %} """, + image_format="svg", ) print(result) expected = """## Coverage report (foo) @@ -202,25 +203,25 @@ def test_template_full(make_coverage, make_coverage_and_diff): expected = """## Coverage report -
Click to see where and how coverage changed +
Click to see where and how coverage changed
- + - + - + - +
FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  codebase
  code.py12-14, 22
12-14, 22
  other.py
  third.py
Project Total  
@@ -264,17 +265,17 @@ def test_template__no_previous(coverage_obj_no_branch, diff_coverage_obj): expected = """## Coverage report -
Click to see where and how coverage changed +
Click to see where and how coverage changed
- + - +
FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  codebase
  code.py6-8
6-8
Project Total