| from __future__ import annotations | |
| import os | |
| import posixpath | |
| import pytest | |
| from pip._internal.exceptions import InvalidEggFragment, PipError | |
| from pip._internal.models.link import ( | |
| Link, | |
| as_path_component, | |
| join_within_directory, | |
| links_equivalent, | |
| ) | |
| from pip._internal.utils.hashes import Hashes | |
| class TestLink: | |
| @pytest.mark.parametrize( | |
| "url, expected", | |
| [ | |
| ( | |
| "https://user:password@example.com/path/page.html", | |
| "<Link https://user:****@example.com/path/page.html>", | |
| ), | |
| ], | |
| ) | |
| def test_repr(self, url: str, expected: str) -> None: | |
| link = Link(url) | |
| assert repr(link) == expected | |
| @pytest.mark.parametrize( | |
| "url, expected", | |
| [ | |
| ("http://yo/wheel.whl", "wheel.whl"), | |
| ("http://yo/wheel", "wheel"), | |
| ("https://example.com/path/page.html", "page.html"), | |
| # Test a quoted character. | |
| ("https://example.com/path/page%231.html", "page#1.html"), | |
| # A doubly-encoded separator must stay encoded: the path is decoded | |
| # exactly once, so the file name keeps its literal "%2F" instead of | |
| # collapsing into a "/". | |
| ( | |
| "https://example.com/a%252Fb.whl", | |
| "a%2Fb.whl", | |
| ), | |
| ( | |
| "http://yo/myproject-1.0%2Bfoobar.0-py2.py3-none-any.whl", | |
| "myproject-1.0+foobar.0-py2.py3-none-any.whl", | |
| ), | |
| # Test a path that ends in a slash. | |
| ("https://example.com/path/", "path"), | |
| ("https://example.com/path//", "path"), | |
| # Test a url with no filename. | |
| ("https://example.com/", "example.com"), | |
| # Test a url with no filename and with auth information. | |
| ( | |
| "https://user:password@example.com/", | |
| "example.com", | |
| ), | |
| ], | |
| ) | |
| def test_filename(self, url: str, expected: str) -> None: | |
| link = Link(url) | |
| assert link.filename == expected | |
| @pytest.mark.parametrize( | |
| "url", | |
| [ | |
| "https://example.com/a%252Fb.whl", | |
| "https://example.com/%252e%252e%252fb.whl", | |
| ], | |
| ) | |
| def test_filename_decoded_once_stays_single_component(self, url: str) -> None: | |
| # The path is decoded exactly once, so an encoded separator stays | |
| # encoded and the file name remains a single path component rather | |
| # than collapsing into a "/"-separated path. | |
| filename = Link(url).filename | |
| assert not posixpath.isabs(filename) | |
| assert posixpath.basename(filename) == filename | |
| @pytest.mark.parametrize( | |
| "url", | |
| [ | |
| "https://example.com/..", | |
| "https://example.com/.", | |
| "https://example.com/foo/%2e%2e", | |
| ], | |
| ) | |
| def test_filename_parent_reference_falls_back_to_netloc(self, url: str) -> None: | |
| # A path that is only a "." or ".." reference has no usable file name, | |
| # so filename falls back to the netloc rather than handing back a | |
| # traversal component that could escape a download directory. | |
| assert Link(url).filename == "example.com" | |
| @pytest.mark.parametrize( | |
| "url", | |
| [ | |
| # A path-less URL whose authority looks like a traversal: the netloc | |
| # fallback must still reduce to a single path component. | |
| "http://..\\..\\..\\evil.whl", | |
| "http://../", | |
| "http://..", | |
| ], | |
| ) | |
| def test_filename_is_always_a_path_component(self, url: str) -> None: | |
| # filename must never carry a separator or parent reference, so joining | |
| # it onto a directory can never escape that directory. | |
| name = Link(url).filename | |
| assert os.path.basename(name) == name | |
| assert name not in (os.curdir, os.pardir) | |
| def test_splitext(self) -> None: | |
| assert ("wheel", ".whl") == Link("http://yo/wheel.whl").splitext() | |
| def test_no_ext(self) -> None: | |
| assert "" == Link("http://yo/wheel").ext | |
| def test_ext(self) -> None: | |
| assert ".whl" == Link("http://yo/wheel.whl").ext | |
| def test_ext_fragment(self) -> None: | |
| assert ".whl" == Link("http://yo/wheel.whl#frag").ext | |
| def test_ext_query(self) -> None: | |
| assert ".whl" == Link("http://yo/wheel.whl?a=b").ext | |
| def test_is_wheel(self) -> None: | |
| assert Link("http://yo/wheel.whl").is_wheel | |
| def test_is_wheel_false(self) -> None: | |
| assert not Link("http://yo/not_a_wheel").is_wheel | |
| def test_fragments(self) -> None: | |
| url = "git+https://example.com/package#egg=eggname" | |
| assert "eggname" == Link(url).egg_fragment | |
| assert None is Link(url).subdirectory_fragment | |
| url = "git+https://example.com/package#egg=eggname&subdirectory=subdir" | |
| assert "eggname" == Link(url).egg_fragment | |
| assert "subdir" == Link(url).subdirectory_fragment | |
| url = "git+https://example.com/package#subdirectory=subdir&egg=eggname" | |
| assert "eggname" == Link(url).egg_fragment | |
| assert "subdir" == Link(url).subdirectory_fragment | |
| @pytest.mark.parametrize( | |
| "fragment", | |
| [ | |
| # Package names in egg fragments must be in PEP 508 form. | |
| "~invalid~package~name~", | |
| # Version specifiers are not valid in egg fragments. | |
| "eggname==1.2.3", | |
| "eggname>=1.2.3", | |
| # Extras are also prohibited. | |
| "eggname[!]", | |
| "eggname[extra]", | |
| "eggname[extra1,extra2]", | |
| "eggmame[]", | |
| "eggname[extra]==1000", | |
| ], | |
| ) | |
| def test_invalid_egg_fragments(self, fragment: str) -> None: | |
| url = f"git+https://example.com/package#egg={fragment}" | |
| with pytest.raises(PipError): | |
| Link(url) | |
| def test_invalid_egg_fragment_with_extras_and_version_hint(self) -> None: | |
| """Test that fragments with extras and version specifiers get proper hint.""" | |
| url = "git+https://example.com/package#egg=eggname[extra]==1.0" | |
| with pytest.raises(InvalidEggFragment) as exc_info: | |
| Link(url) | |
| # The hint should suggest Direct URL syntax, not just "remove version | |
| # specifiers" because the extras require Direct URL syntax anyway. | |
| hint = str(exc_info.value.hint_stmt) | |
| assert r"name\[extra] @ URL" in hint | |
| assert "Version specifiers are silently ignored" in hint | |
| @pytest.mark.parametrize( | |
| "yanked_reason, expected", | |
| [ | |
| (None, False), | |
| ("", True), | |
| ("there was a mistake", True), | |
| ], | |
| ) | |
| def test_is_yanked(self, yanked_reason: str | None, expected: bool) -> None: | |
| link = Link( | |
| "https://example.com/wheel.whl", | |
| yanked_reason=yanked_reason, | |
| ) | |
| assert link.is_yanked == expected | |
| @pytest.mark.parametrize( | |
| "hash_name, hex_digest, expected", | |
| [ | |
| # Test a value that matches but with the wrong hash_name. | |
| ("sha384", 128 * "a", False), | |
| # Test matching values, including values other than the first. | |
| ("sha512", 128 * "a", True), | |
| ("sha512", 128 * "b", True), | |
| # Test a matching hash_name with a value that doesn't match. | |
| ("sha512", 128 * "c", False), | |
| # Test a link without a hash value. | |
| ("sha512", "", False), | |
| ], | |
| ) | |
| def test_is_hash_allowed( | |
| self, hash_name: str, hex_digest: str, expected: bool | |
| ) -> None: | |
| url = f"https://example.com/wheel.whl#{hash_name}={hex_digest}" | |
| link = Link(url) | |
| hashes_data = { | |
| "sha512": [128 * "a", 128 * "b"], | |
| } | |
| hashes = Hashes(hashes_data) | |
| assert link.is_hash_allowed(hashes) == expected | |
| def test_is_hash_allowed__no_hash(self) -> None: | |
| link = Link("https://example.com/wheel.whl") | |
| hashes_data = { | |
| "sha512": [128 * "a"], | |
| } | |
| hashes = Hashes(hashes_data) | |
| assert not link.is_hash_allowed(hashes) | |
| @pytest.mark.parametrize( | |
| "hashes, expected", | |
| [ | |
| (None, False), | |
| # Also test a success case to show the test is correct. | |
| (Hashes({"sha512": [128 * "a"]}), True), | |
| ], | |
| ) | |
| def test_is_hash_allowed__none_hashes( | |
| self, hashes: Hashes | None, expected: bool | |
| ) -> None: | |
| url = "https://example.com/wheel.whl#sha512={}".format(128 * "a") | |
| link = Link(url) | |
| assert link.is_hash_allowed(hashes) == expected | |
| @pytest.mark.parametrize( | |
| "url, expected", | |
| [ | |
| ("git+https://github.com/org/repo", True), | |
| ("bzr+http://bzr.myproject.org/MyProject/trunk/#egg=MyProject", True), | |
| ("hg+file://hg.company.com/repo", True), | |
| ("https://example.com/some.whl", False), | |
| ("file://home/foo/some.whl", False), | |
| ], | |
| ) | |
| def test_is_vcs(self, url: str, expected: bool) -> None: | |
| link = Link(url) | |
| assert link.is_vcs is expected | |
| @pytest.mark.parametrize( | |
| "url1, url2", | |
| [ | |
| pytest.param( | |
| "https://example.com/foo#egg=foo", | |
| "https://example.com/foo", | |
| id="drop-egg", | |
| ), | |
| pytest.param( | |
| "https://example.com/foo#subdirectory=bar&egg=foo", | |
| "https://example.com/foo#subdirectory=bar&egg=bar", | |
| id="drop-egg-only", | |
| ), | |
| pytest.param( | |
| "https://example.com/foo#subdirectory=bar&egg=foo", | |
| "https://example.com/foo#egg=foo&subdirectory=bar", | |
| id="fragment-ordering", | |
| ), | |
| pytest.param( | |
| "https://example.com/foo?a=1&b=2", | |
| "https://example.com/foo?b=2&a=1", | |
| id="query-opordering", | |
| ), | |
| ], | |
| ) | |
| def test_links_equivalent(url1: str, url2: str) -> None: | |
| assert links_equivalent(Link(url1), Link(url2)) | |
| @pytest.mark.parametrize( | |
| "url1, url2", | |
| [ | |
| pytest.param( | |
| "https://example.com/foo#sha512=1234567890abcdef", | |
| "https://example.com/foo#sha512=abcdef1234567890", | |
| id="different-keys", | |
| ), | |
| pytest.param( | |
| "https://example.com/foo#sha512=1234567890abcdef", | |
| "https://example.com/foo#md5=1234567890abcdef", | |
| id="different-values", | |
| ), | |
| pytest.param( | |
| "https://example.com/foo#subdirectory=bar&egg=foo", | |
| "https://example.com/foo#subdirectory=rex", | |
| id="drop-egg-still-different", | |
| ), | |
| ], | |
| ) | |
| def test_links_equivalent_false(url1: str, url2: str) -> None: | |
| assert not links_equivalent(Link(url1), Link(url2)) | |
| @pytest.mark.parametrize( | |
| "name", | |
| [ | |
| "wheel.whl", | |
| "myproject-1.0+foobar.0-py2.py3-none-any.whl", | |
| # A literal "%2F" is a normal file name, not a separator. | |
| "a%2Fb.whl", | |
| ], | |
| ) | |
| def test_as_path_component_keeps_plain_name(name: str) -> None: | |
| assert as_path_component(name) == name | |
| @pytest.mark.parametrize( | |
| "name", | |
| [ | |
| os.path.join(os.sep, "abs", "pkg.whl"), | |
| os.path.join("..", "pkg.whl"), | |
| os.path.join("nested", "pkg.whl"), | |
| ], | |
| ) | |
| def test_as_path_component_reduces_to_basename(name: str) -> None: | |
| # A name carrying directory components is reduced to its basename, so the | |
| # result always stays inside the directory it is later joined onto. | |
| assert as_path_component(name) == os.path.basename(name) | |
| @pytest.mark.parametrize("name", ["", ".", "..", "/", os.path.join("sub", "..")]) | |
| def test_as_path_component_rejects_empty_or_parent_reference(name: str) -> None: | |
| with pytest.raises(ValueError): | |
| as_path_component(name) | |
| @pytest.mark.parametrize( | |
| "name", | |
| [ | |
| "pkg.whl", | |
| # A literal "%2F" is a normal file name, not a separator. | |
| "a%2Fb.whl", | |
| ], | |
| ) | |
| def test_join_within_directory_stays_inside(name: str) -> None: | |
| # The component is joined onto the directory as its final element, so the | |
| # result stays inside the directory. | |
| directory = os.path.join("base", "downloads") | |
| joined = join_within_directory(directory, as_path_component(name)) | |
| assert joined == os.path.join(directory, name) | |
| assert os.path.basename(joined) == name |