Skip to content

Commit 0ddd200

Browse files
CopilotChipWolf
andauthored
[WIP] Add functionality to create output file if it doesn't exist (#104)
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 0822fa5 commit 0ddd200

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

badgesort/icons.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,15 @@ def step (r,g,b, repetitions=1, rotate=0, invert=False):
572572

573573
# if output file is specified, write badges to file
574574
if args.output:
575-
with open(args.output, 'r') as f:
576-
output_content = f.read()
575+
# Check if the output file exists
576+
if os.path.exists(args.output):
577+
with open(args.output, 'r') as f:
578+
output_content = f.read()
579+
else:
580+
# File doesn't exist, create it with empty content
581+
logger.info(f'Output file "{args.output}" does not exist. Creating new file.')
582+
output_content = ''
583+
577584
# replace existing badges between the badge header and footer with the new ones
578585
# but skip any markers inside markdown codeblocks
579586
output_content, markers_found = _replace_badges_outside_codeblocks(output_content, badges_header, badges_footer, badges)

tests/test_integration.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,58 @@ def test_integration_append_preserves_newlines():
350350

351351
finally:
352352
os.unlink(temp_file)
353+
354+
355+
def test_integration_create_output_file_if_not_exists():
356+
"""Integration test: create the output file if it doesn't exist."""
357+
# Generate a path for a file that doesn't exist
358+
temp_dir = tempfile.gettempdir()
359+
temp_file = os.path.join(temp_dir, f'badgesort_test_nonexistent_{os.getpid()}.md')
360+
361+
# Ensure the file doesn't exist
362+
if os.path.exists(temp_file):
363+
os.unlink(temp_file)
364+
365+
try:
366+
args = argparse.Namespace(
367+
slugs=['github', 'python'],
368+
random=1,
369+
output=temp_file,
370+
id='default',
371+
format='markdown',
372+
badge_style='flat',
373+
color_sort='hilbert',
374+
hue_rotate=0,
375+
no_thanks=True,
376+
reverse=False,
377+
provider='shields',
378+
verify=False,
379+
embed_svg=False,
380+
skip_logo_check=True
381+
)
382+
383+
# Run BadgeSort - should create the file
384+
run(args)
385+
386+
# Verify the file was created
387+
assert os.path.exists(temp_file), "Output file should be created"
388+
389+
# Read the result
390+
with open(temp_file, 'r') as f:
391+
result = f.read()
392+
393+
# Verify badges were generated with markers
394+
assert "<!-- start chipwolf/badgesort default -->" in result
395+
assert "<!-- end chipwolf/badgesort default -->" in result
396+
397+
# Verify badges were generated
398+
assert 'github' in result.lower() or 'python' in result.lower(), "Badges should be generated"
399+
400+
# Since the file was new, badges should be the only content
401+
lines = result.strip().split('\n')
402+
assert len(lines) >= 3, "Should have at least markers and badges"
403+
404+
finally:
405+
# Clean up
406+
if os.path.exists(temp_file):
407+
os.unlink(temp_file)

0 commit comments

Comments
 (0)