Skip to content

Commit ae47870

Browse files
CopilotChipWolf
andauthored
[WIP] Fix badges without custom URLs to hyperlink correctly (#100)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ChipWolf <3164166+ChipWolf@users.noreply.github.com>
1 parent a395ebd commit ae47870

2 files changed

Lines changed: 265 additions & 1 deletion

File tree

badgesort/icons.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ def step (r,g,b, repetitions=1, rotate=0, invert=False):
542542
# Use custom URL if provided
543543
badges += f'[{md_badge}]({icon["custom_url"]})\n'
544544
else:
545-
badges += md_badge + '\n'
545+
# Wrap badge with link to # so clicking doesn't open the image
546+
badges += f'[{md_badge}](#)\n'
546547
elif args.format == 'html':
547548
if icon["slug"] == 'badgesort':
548549
badges += ' <a href="https://github.com/ChipWolf/BadgeSort">'

tests/test_badge_hyperlinks.py

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Tests for badge hyperlink behavior.
5+
6+
Tests that badges without custom URLs are hyperlinked to # in both Markdown and HTML formats.
7+
"""
8+
9+
import argparse
10+
import tempfile
11+
import os
12+
from badgesort.icons import run
13+
14+
15+
def test_markdown_badges_without_custom_url_hyperlinked():
16+
"""Test that Markdown badges without custom URLs are hyperlinked to #."""
17+
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
18+
f.write("""# Test File
19+
20+
<!-- start chipwolf/badgesort test -->
21+
<!-- end chipwolf/badgesort test -->
22+
""")
23+
temp_file = f.name
24+
25+
try:
26+
args = argparse.Namespace(
27+
slugs=['github', 'python'],
28+
random=1,
29+
output=temp_file,
30+
id='test',
31+
format='markdown',
32+
badge_style='flat',
33+
color_sort='hilbert',
34+
hue_rotate=0,
35+
no_thanks=True, # Exclude BadgeSort badge for simpler test
36+
reverse=False,
37+
provider='shields',
38+
verify=False,
39+
embed_svg=False,
40+
skip_logo_check=True
41+
)
42+
43+
run(args)
44+
45+
with open(temp_file, 'r') as f:
46+
result = f.read()
47+
48+
# Badges without custom URLs should be wrapped with [...]`(#)`
49+
# Pattern: [![Title](badge_url)](#)
50+
assert '[![GitHub]' in result or '[![Python]' in result, \
51+
"Markdown badges should start with [!["
52+
assert '(#)' in result, \
53+
"Markdown badges without custom URLs should have (#) at the end"
54+
55+
# Should NOT have unwrapped badges (just ![Title](url) without link wrapper)
56+
# Count occurrences - if badges are properly wrapped, we should have:
57+
# - One ![GitHub](...) inside [![GitHub](...)](#)
58+
# - One ![Python](...) inside [![Python](...)](#)
59+
lines = result.split('\n')
60+
badge_lines = [line for line in lines if '![' in line and 'img.shields.io' in line]
61+
62+
for line in badge_lines:
63+
# Each badge line should have the pattern: [![...](...)](#)
64+
if '![GitHub]' in line:
65+
assert line.strip().startswith('[![GitHub]'), \
66+
f"GitHub badge should be wrapped with link: {line}"
67+
assert line.strip().endswith('(#)'), \
68+
f"GitHub badge link should end with (#): {line}"
69+
if '![Python]' in line:
70+
assert line.strip().startswith('[![Python]'), \
71+
f"Python badge should be wrapped with link: {line}"
72+
assert line.strip().endswith('(#)'), \
73+
f"Python badge link should end with (#): {line}"
74+
75+
finally:
76+
os.unlink(temp_file)
77+
78+
79+
def test_markdown_badges_with_custom_url_not_double_wrapped():
80+
"""Test that Markdown badges with custom URLs are not double-wrapped."""
81+
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
82+
f.write("""# Test File
83+
84+
<!-- start chipwolf/badgesort test -->
85+
<!-- end chipwolf/badgesort test -->
86+
""")
87+
temp_file = f.name
88+
89+
try:
90+
# Badge with custom URL parameter
91+
args = argparse.Namespace(
92+
slugs=['github?url=https://github.com/ChipWolf'],
93+
random=1,
94+
output=temp_file,
95+
id='test',
96+
format='markdown',
97+
badge_style='flat',
98+
color_sort='hilbert',
99+
hue_rotate=0,
100+
no_thanks=True,
101+
reverse=False,
102+
provider='shields',
103+
verify=False,
104+
embed_svg=False,
105+
skip_logo_check=True
106+
)
107+
108+
run(args)
109+
110+
with open(temp_file, 'r') as f:
111+
result = f.read()
112+
113+
# Badge with custom URL should be wrapped with custom URL, not (#)
114+
assert 'https://github.com/ChipWolf' in result, \
115+
"Badge should use custom URL"
116+
117+
# Should NOT have (#) when custom URL is provided
118+
badge_lines = [line for line in result.split('\n') if '![GitHub]' in line]
119+
for line in badge_lines:
120+
assert '(#)' not in line, \
121+
f"Badge with custom URL should not have (#): {line}"
122+
assert 'https://github.com/ChipWolf' in line, \
123+
f"Badge should use custom URL: {line}"
124+
125+
finally:
126+
os.unlink(temp_file)
127+
128+
129+
def test_badgesort_badge_has_correct_link():
130+
"""Test that the BadgeSort badge itself has the correct GitHub link."""
131+
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
132+
f.write("""# Test File
133+
134+
<!-- start chipwolf/badgesort test -->
135+
<!-- end chipwolf/badgesort test -->
136+
""")
137+
temp_file = f.name
138+
139+
try:
140+
args = argparse.Namespace(
141+
slugs=['github'],
142+
random=1,
143+
output=temp_file,
144+
id='test',
145+
format='markdown',
146+
badge_style='flat',
147+
color_sort='hilbert',
148+
hue_rotate=0,
149+
no_thanks=True, # Note: no_thanks=True means INCLUDE the badge (action='store_false')
150+
reverse=False,
151+
provider='shields',
152+
verify=False,
153+
embed_svg=False,
154+
skip_logo_check=True
155+
)
156+
157+
run(args)
158+
159+
with open(temp_file, 'r') as f:
160+
result = f.read()
161+
162+
# BadgeSort badge should link to GitHub repo
163+
assert 'https://github.com/ChipWolf/BadgeSort' in result, \
164+
"BadgeSort badge should link to GitHub repository"
165+
166+
# Regular badges (without custom URL) should link to #
167+
badge_lines = result.split('\n')
168+
github_badge_lines = [line for line in badge_lines if '![GitHub]' in line and 'BadgeSort' not in line]
169+
170+
for line in github_badge_lines:
171+
assert '(#)' in line, \
172+
f"Regular GitHub badge should link to #: {line}"
173+
174+
finally:
175+
os.unlink(temp_file)
176+
177+
178+
def test_html_badges_without_custom_url_hyperlinked():
179+
"""Test that HTML badges without custom URLs are hyperlinked to # (existing behavior)."""
180+
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
181+
f.write("""# Test File
182+
183+
<!-- start chipwolf/badgesort test -->
184+
<!-- end chipwolf/badgesort test -->
185+
""")
186+
temp_file = f.name
187+
188+
try:
189+
args = argparse.Namespace(
190+
slugs=['github', 'python'],
191+
random=1,
192+
output=temp_file,
193+
id='test',
194+
format='html',
195+
badge_style='flat',
196+
color_sort='hilbert',
197+
hue_rotate=0,
198+
no_thanks=True,
199+
reverse=False,
200+
provider='shields',
201+
verify=False,
202+
embed_svg=False,
203+
skip_logo_check=True
204+
)
205+
206+
run(args)
207+
208+
with open(temp_file, 'r') as f:
209+
result = f.read()
210+
211+
# HTML badges should be wrapped with <a href="#">
212+
assert '<a href="#">' in result, \
213+
"HTML badges without custom URLs should have <a href='#'>"
214+
215+
# All badge images should be inside anchor tags
216+
badge_lines = [line for line in result.split('\n') if '<img alt=' in line]
217+
for line in badge_lines:
218+
assert '<a href="' in line, \
219+
f"HTML badge should be inside anchor tag: {line}"
220+
221+
finally:
222+
os.unlink(temp_file)
223+
224+
225+
def test_html_badges_with_custom_url():
226+
"""Test that HTML badges with custom URLs use the custom URL."""
227+
with tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False) as f:
228+
f.write("""# Test File
229+
230+
<!-- start chipwolf/badgesort test -->
231+
<!-- end chipwolf/badgesort test -->
232+
""")
233+
temp_file = f.name
234+
235+
try:
236+
args = argparse.Namespace(
237+
slugs=['github?url=https://github.com/ChipWolf'],
238+
random=1,
239+
output=temp_file,
240+
id='test',
241+
format='html',
242+
badge_style='flat',
243+
color_sort='hilbert',
244+
hue_rotate=0,
245+
no_thanks=True,
246+
reverse=False,
247+
provider='shields',
248+
verify=False,
249+
embed_svg=False,
250+
skip_logo_check=True
251+
)
252+
253+
run(args)
254+
255+
with open(temp_file, 'r') as f:
256+
result = f.read()
257+
258+
# HTML badge with custom URL should use that URL
259+
assert '<a href="https://github.com/ChipWolf">' in result, \
260+
"HTML badge should use custom URL"
261+
262+
finally:
263+
os.unlink(temp_file)

0 commit comments

Comments
 (0)