test: add fname_to_url tests for Unix, Windows, and edge cases

This commit is contained in:
Paul Gauthier (aider) 2024-10-21 14:28:28 -07:00
parent 67d538a499
commit 671f3078c1
2 changed files with 41 additions and 7 deletions

View file

@ -64,7 +64,7 @@ def fname_to_url(filepath):
return ""
# Join the remaining parts
url_path = "/".join(relevant_parts)
url_path = '/'.join(relevant_parts)
# Handle index.md and other .md files
if url_path.endswith(index):
@ -73,7 +73,7 @@ def fname_to_url(filepath):
url_path = url_path[:-len(md)] + ".html"
# Ensure the URL starts and ends with '/'
url_path = url_path.strip("/")
url_path = url_path.strip('/')
return f"https://aider.chat/{url_path}"

View file

@ -1,10 +1,12 @@
import unittest
from unittest.mock import MagicMock
import os
from pathlib import Path
import aider
from aider.coders import Coder
from aider.commands import Commands
from aider.help import Help
from aider.help import Help, fname_to_url
from aider.io import InputOutput
from aider.models import Model
@ -54,6 +56,38 @@ class TestHelp(unittest.TestCase):
# Assert that there are more than 5 <doc> entries
self.assertGreater(result.count("<doc"), 5)
def test_fname_to_url_unix(self):
# Test relative Unix-style paths
self.assertEqual(fname_to_url('website/docs/index.md'), 'https://aider.chat/docs/')
self.assertEqual(fname_to_url('website/docs/usage.md'), 'https://aider.chat/docs/usage.html')
self.assertEqual(fname_to_url('website/_includes/header.md'), '')
# Test absolute Unix-style paths
self.assertEqual(fname_to_url('/home/user/project/website/docs/index.md'), 'https://aider.chat/docs/')
self.assertEqual(fname_to_url('/home/user/project/website/docs/usage.md'), 'https://aider.chat/docs/usage.html')
self.assertEqual(fname_to_url('/home/user/project/website/_includes/header.md'), '')
def test_fname_to_url_windows(self):
# Test relative Windows-style paths
self.assertEqual(fname_to_url(r'website\docs\index.md'), 'https://aider.chat/docs/')
self.assertEqual(fname_to_url(r'website\docs\usage.md'), 'https://aider.chat/docs/usage.html')
self.assertEqual(fname_to_url(r'website\_includes\header.md'), '')
# Test absolute Windows-style paths
self.assertEqual(fname_to_url(r'C:\Users\user\project\website\docs\index.md'), 'https://aider.chat/docs/')
self.assertEqual(fname_to_url(r'C:\Users\user\project\website\docs\usage.md'), 'https://aider.chat/docs/usage.html')
self.assertEqual(fname_to_url(r'C:\Users\user\project\website\_includes\header.md'), '')
def test_fname_to_url_edge_cases(self):
# Test paths that don't contain 'website'
self.assertEqual(fname_to_url('/home/user/project/docs/index.md'), '')
self.assertEqual(fname_to_url(r'C:\Users\user\project\docs\index.md'), '')
# Test empty path
self.assertEqual(fname_to_url(''), '')
# Test path with 'website' in the wrong place
self.assertEqual(fname_to_url('/home/user/website_project/docs/index.md'), '')
if __name__ == "__main__":
unittest.main()