diff --git a/pyproject.toml b/pyproject.toml index 4fcd1b7..38b2ff1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,7 @@ dependencies = [ "parsley", "pkginfo>=1.12", "readme-renderer[md]", - "simple-repository~=0.9", + "simple-repository~=0.10", "typing-extensions", "uvicorn", "authlib", diff --git a/simple_repository_browser/fetch_description.py b/simple_repository_browser/fetch_description.py index 8339221..c49a8a6 100644 --- a/simple_repository_browser/fetch_description.py +++ b/simple_repository_browser/fetch_description.py @@ -214,10 +214,10 @@ def _enhance_author_maintainer_info(info: pkginfo.Distribution) -> None: def extract_usernames(emails: str) -> str: names = [] - parsed = email.parser.Parser(policy=email.policy.default).parsestr( + parsed = email.parser.Parser(policy=email.policy.default).parsestr( # type: ignore[arg-type] f"To: {emails}", ) - for address in parsed["to"].addresses: + for address in getattr(parsed["to"], "addresses", []): names.append(address.display_name) return ", ".join(names) diff --git a/simple_repository_browser/filesize_enrichment.py b/simple_repository_browser/filesize_enrichment.py index 0d9a43d..2c06bea 100644 --- a/simple_repository_browser/filesize_enrichment.py +++ b/simple_repository_browser/filesize_enrichment.py @@ -56,7 +56,7 @@ async def get_project_page( self, project_name: str, *, - request_context: model.RequestContext = model.RequestContext.DEFAULT, + request_context: model.RequestContext | None = None, ) -> model.ProjectDetail: """ Get project page with file sizes enriched. diff --git a/simple_repository_browser/tests/test_fetch_description.py b/simple_repository_browser/tests/test_fetch_description.py new file mode 100644 index 0000000..8f5af88 --- /dev/null +++ b/simple_repository_browser/tests/test_fetch_description.py @@ -0,0 +1,118 @@ +import pkginfo +import pytest + +from ..fetch_description import _enhance_author_maintainer_info + + +@pytest.mark.parametrize( + [ + "author", + "author_email", + "maintainer", + "maintainer_email", + "expected_author", + "expected_maintainer", + ], + [ + # Test extracting author from email when no author + (None, "John Doe ", None, None, "John Doe", None), + # Test extracting maintainer from email when no maintainer + (None, None, None, "Jane Smith ", None, "Jane Smith"), + # Test extracting both author and maintainer + ( + None, + "John Doe ", + None, + "Jane Smith ", + "John Doe", + "Jane Smith", + ), + # Test preserving existing author name + ( + "Existing Author", + "John Doe ", + None, + None, + "Existing Author", + None, + ), + # Test preserving existing maintainer name + ( + None, + None, + "Existing Maintainer", + "Jane Smith ", + None, + "Existing Maintainer", + ), + # Test handling empty author email + (None, "", None, None, None, None), + # Test handling None author email + (None, None, None, None, None, None), + # Test handling multiple emails + ( + None, + "John Doe , Jane Smith ", + None, + None, + "John Doe, Jane Smith", + None, + ), + # Test handling email without display name + (None, "john@example.com", None, None, "", None), + # Test handling mixed email formats + ( + None, + "John Doe , jane@example.com", + None, + None, + "John Doe, ", + None, + ), + # Test handling empty string author (should be treated as missing) + ("", "John Doe ", None, None, "John Doe", None), + # Test handling empty string maintainer (should be treated as missing) + (None, None, "", "Jane Smith ", None, "Jane Smith"), + # Test complex real-world scenario + ( + "", + "John Doe , Support Team ", + "", + "Jane Smith ", + "John Doe, Support Team", + "Jane Smith", + ), + # Test whitespace in emails + (None, " John Doe ", None, None, "John Doe", None), + # Test no changes needed + ( + "John Doe", + "john@example.com", + "Jane Smith", + "jane@example.com", + "John Doe", + "Jane Smith", + ), + ], +) +def test_enhance_author_maintainer_info( + author, + author_email, + maintainer, + maintainer_email, + expected_author, + expected_maintainer, +): + """Test _enhance_author_maintainer_info with various email and name combinations.""" + # Create a real pkginfo.Distribution instance + info = pkginfo.Distribution() + info.name = "test-package" + info.author = author + info.author_email = author_email + info.maintainer = maintainer + info.maintainer_email = maintainer_email + + _enhance_author_maintainer_info(info) + + assert info.author == expected_author + assert info.maintainer == expected_maintainer