diff --git a/news/5821.trivial b/news/5821.trivial new file mode 100644 index 00000000000..25c6284e8b0 --- /dev/null +++ b/news/5821.trivial @@ -0,0 +1 @@ +Add unit tests for egg_info_matches. diff --git a/tests/unit/test_index.py b/tests/unit/test_index.py index d67aa4d0dd9..bca09a24e4b 100644 --- a/tests/unit/test_index.py +++ b/tests/unit/test_index.py @@ -4,7 +4,9 @@ from pip._vendor import html5lib from pip._internal.download import PipSession -from pip._internal.index import Link, PackageFinder, _determine_base_url +from pip._internal.index import ( + Link, PackageFinder, _determine_base_url, egg_info_matches, +) def test_sort_locations_file_expand_dir(data): @@ -158,3 +160,33 @@ def test_get_formatted_locations_basic_auth(): result = finder.get_formatted_locations() assert 'user' not in result and 'pass' not in result + + +@pytest.mark.parametrize( + ("egg_info", "search_name", "expected"), + [ + # Trivial. + ("pip-18.0", "pip", "18.0"), + ("pip-18.0", None, "18.0"), + + # Non-canonical names. + ("Jinja2-2.10", "jinja2", "2.10"), + ("jinja2-2.10", "Jinja2", "2.10"), + + # Ambiguous names. Should be smart enough if the package name is + # provided, otherwise make a guess. + ("foo-2-2", "foo", "2-2"), + ("foo-2-2", "foo-2", "2"), + ("foo-2-2", None, "2-2"), + ("im-valid", None, "valid"), + + # Invalid names. + ("invalid", None, None), + ("im_invalid", None, None), + ("the-package-name-8.19", "does-not-match", None), + ], +) +def test_egg_info_matches(egg_info, search_name, expected): + link = None # Only used for reporting. + version = egg_info_matches(egg_info, search_name, link) + assert version == expected