Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit d48ceab

Browse files
authored
fix: Tweak bundle badge (#1236)
1 parent 3b18f44 commit d48ceab

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

graphs/helpers/badge.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,39 @@ def format_coverage_precision(coverage: float | None, precision: int):
5252
return ("%%.%sf" % precision) % coverage
5353

5454

55-
def get_bundle_badge(bundle_size_bytes: int, precision: int):
55+
def get_bundle_badge(bundle_size_bytes: int | None, precision: int):
56+
if bundle_size_bytes is None:
57+
# Returns text 'unknown' instead of bundle size
58+
return """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
59+
<linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
60+
<stop offset="0" stop-color="#bbb" stop-opacity=".1" />
61+
<stop offset="1" stop-opacity=".1" />
62+
</linearGradient>
63+
<mask id="CodecovBadgeMask106px">
64+
<rect width="106" height="20" rx="3" fill="#fff" />
65+
</mask>
66+
<g mask="url(#CodecovBadgeMask106px)">
67+
<path fill="#555" d="M0 0h47v20H0z" />
68+
<path fill="#2C2433" d="M47 0h59v20H47z" />
69+
<path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
70+
</g>
71+
<g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
72+
<text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
73+
<text x="5" y="14">bundle</text>
74+
<text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
75+
<text x="52" y="14">unknown</text>
76+
</g>
77+
</svg>
78+
"""
79+
5680
bundle_size_string = format_bundle_bytes(bundle_size_bytes, precision)
5781
char_width = 7 # approximate, looks good on all reasonable inputs
5882
width_in_pixels = len(bundle_size_string) * char_width
5983
static_width = 57 # width of static elements in the svg (text + margins)
6084

6185
width = static_width + width_in_pixels
6286

63-
return f""" <svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20">
87+
return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20">
6488
<linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
6589
<stop offset="0" stop-color="#bbb" stop-opacity=".1" />
6690
<stop offset="1" stop-opacity=".1" />
@@ -70,7 +94,7 @@ def get_bundle_badge(bundle_size_bytes: int, precision: int):
7094
</mask>
7195
<g mask="url(#CodecovBadgeMask{width}px)">
7296
<path fill="#555" d="M0 0h47v20H0z" />
73-
<path fill="#2C2433" d="M47 0h{width - static_width + 10}v20H47z" />
97+
<path fill="#2C2433" d="M47 0h{width_in_pixels + 10}v20H47z" />
7498
<path fill="url(#CodecovBadgeGradient)" d="M0 0h{width}v20H0z" />
7599
</g>
76100
<g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">

graphs/tests/test_helpers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,37 @@ def test_bundle_badge_large(self):
256256
expected_badge = [line.strip() for line in expected_badge.split("\n")]
257257
assert expected_badge == _badge
258258

259+
def test_bundle_badge_unknown(self):
260+
bundle_size_bytes = None
261+
precision = 2
262+
263+
expected_badge = """<svg xmlns="http://www.w3.org/2000/svg" width="106" height="20">
264+
<linearGradient id="CodecovBadgeGradient" x2="0" y2="100%">
265+
<stop offset="0" stop-color="#bbb" stop-opacity=".1" />
266+
<stop offset="1" stop-opacity=".1" />
267+
</linearGradient>
268+
<mask id="CodecovBadgeMask106px">
269+
<rect width="106" height="20" rx="3" fill="#fff" />
270+
</mask>
271+
<g mask="url(#CodecovBadgeMask106px)">
272+
<path fill="#555" d="M0 0h47v20H0z" />
273+
<path fill="#2C2433" d="M47 0h59v20H47z" />
274+
<path fill="url(#CodecovBadgeGradient)" d="M0 0h106v20H0z" />
275+
</g>
276+
<g fill="#fff" text-anchor="left" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
277+
<text x="5" y="15" fill="#010101" fill-opacity=".3">bundle</text>
278+
<text x="5" y="14">bundle</text>
279+
<text x="52" y="15" fill="#010101" fill-opacity=".3">unknown</text>
280+
<text x="52" y="14">unknown</text>
281+
</g>
282+
</svg>
283+
"""
284+
285+
_badge = get_bundle_badge(bundle_size_bytes, precision)
286+
_badge = [line.strip() for line in _badge.split("\n")]
287+
expected_badge = [line.strip() for line in expected_badge.split("\n")]
288+
assert expected_badge == _badge
289+
259290
def test_format_bundle_bytes_0_precision(self):
260291
bundle_sizes = [
261292
7,

0 commit comments

Comments
 (0)