2018-09-29 21:47:17 +02:00
|
|
|
import logging
|
2015-12-04 22:34:42 +01:00
|
|
|
import os.path
|
2014-06-13 13:28:05 +02:00
|
|
|
|
2017-05-16 12:16:30 +02:00
|
|
|
import pytest
|
2018-06-08 12:33:08 +02:00
|
|
|
from mock import Mock
|
|
|
|
from pip._vendor import html5lib, requests
|
2017-06-13 14:17:00 +02:00
|
|
|
|
2017-08-31 17:48:18 +02:00
|
|
|
from pip._internal.download import PipSession
|
2018-09-27 18:28:14 +02:00
|
|
|
from pip._internal.index import (
|
2018-10-12 21:16:38 +02:00
|
|
|
Link, PackageFinder, _determine_base_url, _egg_info_matches,
|
|
|
|
_get_html_page,
|
2018-09-27 18:28:14 +02:00
|
|
|
)
|
2011-02-10 02:28:34 +01:00
|
|
|
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2015-04-06 20:18:22 +02:00
|
|
|
def test_sort_locations_file_expand_dir(data):
|
2012-09-17 02:45:16 +02:00
|
|
|
"""
|
2015-04-06 20:18:22 +02:00
|
|
|
Test that a file:// dir gets listdir run with expand_dir
|
2012-09-17 02:45:16 +02:00
|
|
|
"""
|
2014-05-07 01:25:44 +02:00
|
|
|
finder = PackageFinder([data.find_links], [], session=PipSession())
|
2015-04-06 20:18:22 +02:00
|
|
|
files, urls = finder._sort_locations([data.find_links], expand_dir=True)
|
2014-01-28 15:17:51 +01:00
|
|
|
assert files and not urls, (
|
|
|
|
"files and not urls should have been found at find-links url: %s" %
|
|
|
|
data.find_links
|
|
|
|
)
|
2012-09-17 07:42:30 +02:00
|
|
|
|
|
|
|
|
2013-08-23 13:12:37 +02:00
|
|
|
def test_sort_locations_file_not_find_link(data):
|
2012-09-17 07:42:30 +02:00
|
|
|
"""
|
2014-01-28 15:17:51 +01:00
|
|
|
Test that a file:// url dir that's not a find-link, doesn't get a listdir
|
|
|
|
run
|
2012-09-17 07:42:30 +02:00
|
|
|
"""
|
2014-05-07 01:25:44 +02:00
|
|
|
finder = PackageFinder([], [], session=PipSession())
|
2015-12-04 22:36:52 +01:00
|
|
|
files, urls = finder._sort_locations([data.index_url("empty_with_pkg")])
|
2012-09-17 07:42:30 +02:00
|
|
|
assert urls and not files, "urls, but not files should have been found"
|
2012-09-17 02:45:16 +02:00
|
|
|
|
|
|
|
|
2015-12-04 22:34:42 +01:00
|
|
|
def test_sort_locations_non_existing_path():
|
|
|
|
"""
|
|
|
|
Test that a non-existing path is ignored.
|
|
|
|
"""
|
|
|
|
finder = PackageFinder([], [], session=PipSession())
|
|
|
|
files, urls = finder._sort_locations(
|
|
|
|
[os.path.join('this', 'doesnt', 'exist')])
|
|
|
|
assert not urls and not files, "nothing should have been found"
|
|
|
|
|
|
|
|
|
2013-11-15 01:35:24 +01:00
|
|
|
class TestLink(object):
|
|
|
|
|
|
|
|
def test_splitext(self):
|
|
|
|
assert ('wheel', '.whl') == Link('http://yo/wheel.whl').splitext()
|
|
|
|
|
2014-12-19 01:44:26 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("url", "expected"),
|
|
|
|
[
|
|
|
|
("http://yo/wheel.whl", "wheel.whl"),
|
|
|
|
("http://yo/wheel", "wheel"),
|
|
|
|
(
|
|
|
|
"http://yo/myproject-1.0%2Bfoobar.0-py2.py3-none-any.whl",
|
|
|
|
"myproject-1.0+foobar.0-py2.py3-none-any.whl",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_filename(self, url, expected):
|
|
|
|
assert Link(url).filename == expected
|
2013-11-15 01:35:24 +01:00
|
|
|
|
|
|
|
def test_no_ext(self):
|
|
|
|
assert '' == Link('http://yo/wheel').ext
|
|
|
|
|
|
|
|
def test_ext(self):
|
|
|
|
assert '.whl' == Link('http://yo/wheel.whl').ext
|
|
|
|
|
|
|
|
def test_ext_fragment(self):
|
|
|
|
assert '.whl' == Link('http://yo/wheel.whl#frag').ext
|
|
|
|
|
|
|
|
def test_ext_query(self):
|
|
|
|
assert '.whl' == Link('http://yo/wheel.whl?a=b').ext
|
2014-06-13 13:28:05 +02:00
|
|
|
|
2014-12-28 23:52:32 +01:00
|
|
|
def test_is_wheel(self):
|
|
|
|
assert Link('http://yo/wheel.whl').is_wheel
|
|
|
|
|
2015-03-06 23:46:44 +01:00
|
|
|
def test_is_wheel_false(self):
|
|
|
|
assert not Link('http://yo/not_a_wheel').is_wheel
|
|
|
|
|
2016-02-09 15:53:42 +01:00
|
|
|
def test_fragments(self):
|
|
|
|
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
|
|
|
|
|
2014-06-13 13:28:05 +02:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("html", "url", "expected"),
|
|
|
|
[
|
2016-10-30 06:35:28 +01:00
|
|
|
(b"<html></html>", "https://example.com/", "https://example.com/"),
|
2014-06-13 13:28:05 +02:00
|
|
|
(
|
2016-10-30 06:35:28 +01:00
|
|
|
b"<html><head>"
|
|
|
|
b"<base href=\"https://foo.example.com/\">"
|
|
|
|
b"</head></html>",
|
2014-06-13 13:28:05 +02:00
|
|
|
"https://example.com/",
|
|
|
|
"https://foo.example.com/",
|
|
|
|
),
|
|
|
|
(
|
2016-10-30 06:35:28 +01:00
|
|
|
b"<html><head>"
|
|
|
|
b"<base><base href=\"https://foo.example.com/\">"
|
|
|
|
b"</head></html>",
|
2014-06-13 13:28:05 +02:00
|
|
|
"https://example.com/",
|
|
|
|
"https://foo.example.com/",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2018-09-28 10:48:08 +02:00
|
|
|
def test_determine_base_url(html, url, expected):
|
2018-09-28 08:56:34 +02:00
|
|
|
document = html5lib.parse(
|
|
|
|
html, transport_encoding=None, namespaceHTMLElements=False,
|
|
|
|
)
|
2018-09-28 10:48:08 +02:00
|
|
|
assert _determine_base_url(document, url) == expected
|
2014-08-11 06:21:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MockLogger(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.called = False
|
|
|
|
|
2014-12-10 01:40:05 +01:00
|
|
|
def warning(self, *args, **kwargs):
|
2014-08-11 06:21:02 +02:00
|
|
|
self.called = True
|
|
|
|
|
|
|
|
|
2014-12-10 01:40:05 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("location", "trusted", "expected"),
|
|
|
|
[
|
2018-04-13 14:08:51 +02:00
|
|
|
("http://pypi.org/something", [], True),
|
|
|
|
("https://pypi.org/something", [], False),
|
|
|
|
("git+http://pypi.org/something", [], True),
|
|
|
|
("git+https://pypi.org/something", [], False),
|
|
|
|
("git+ssh://git@pypi.org/something", [], False),
|
2014-12-10 01:40:05 +01:00
|
|
|
("http://localhost", [], False),
|
|
|
|
("http://127.0.0.1", [], False),
|
|
|
|
("http://example.com/something/", [], True),
|
|
|
|
("http://example.com/something/", ["example.com"], False),
|
2016-01-29 22:31:19 +01:00
|
|
|
("http://eXample.com/something/", ["example.cOm"], False),
|
2014-12-10 01:40:05 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_secure_origin(location, trusted, expected):
|
|
|
|
finder = PackageFinder([], [], session=[], trusted_hosts=trusted)
|
|
|
|
logger = MockLogger()
|
|
|
|
finder._validate_secure_origin(logger, location)
|
|
|
|
assert logger.called == expected
|
2018-04-27 06:33:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_formatted_locations_basic_auth():
|
|
|
|
"""
|
|
|
|
Test that basic authentication credentials defined in URL
|
|
|
|
is not included in formatted output.
|
|
|
|
"""
|
|
|
|
index_urls = [
|
|
|
|
'https://pypi.org/simple',
|
|
|
|
'https://user:pass@repo.domain.com',
|
|
|
|
]
|
|
|
|
finder = PackageFinder([], index_urls, session=[])
|
|
|
|
|
|
|
|
result = finder.get_formatted_locations()
|
|
|
|
assert 'user' not in result and 'pass' not in result
|
2018-09-27 18:28:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2018-10-12 21:16:38 +02:00
|
|
|
("egg_info", "canonical_name", "expected"),
|
2018-09-27 18:28:14 +02:00
|
|
|
[
|
|
|
|
# Trivial.
|
|
|
|
("pip-18.0", "pip", "18.0"),
|
2018-10-12 21:16:38 +02:00
|
|
|
("zope-interface-4.5.0", "zope-interface", "4.5.0"),
|
2018-09-27 18:28:14 +02:00
|
|
|
|
2018-10-12 21:16:38 +02:00
|
|
|
# Canonicalized name match non-canonicalized egg info. (pypa/pip#5870)
|
2018-09-27 18:28:14 +02:00
|
|
|
("Jinja2-2.10", "jinja2", "2.10"),
|
2018-10-12 21:16:38 +02:00
|
|
|
("zope.interface-4.5.0", "zope-interface", "4.5.0"),
|
|
|
|
("zope_interface-4.5.0", "zope-interface", "4.5.0"),
|
2018-09-27 18:28:14 +02:00
|
|
|
|
2018-10-12 21:16:38 +02:00
|
|
|
# Should be smart enough to parse ambiguous names from the provided
|
|
|
|
# package name.
|
2018-09-27 18:28:14 +02:00
|
|
|
("foo-2-2", "foo", "2-2"),
|
|
|
|
("foo-2-2", "foo-2", "2"),
|
|
|
|
|
2018-10-12 21:16:38 +02:00
|
|
|
# Invalid.
|
2018-09-27 18:28:14 +02:00
|
|
|
("the-package-name-8.19", "does-not-match", None),
|
|
|
|
],
|
|
|
|
)
|
2018-10-12 21:16:38 +02:00
|
|
|
def test_egg_info_matches(egg_info, canonical_name, expected):
|
2018-09-27 18:28:14 +02:00
|
|
|
link = None # Only used for reporting.
|
2018-10-12 21:16:38 +02:00
|
|
|
version = _egg_info_matches(egg_info, canonical_name, link)
|
2018-09-27 18:28:14 +02:00
|
|
|
assert version == expected
|
2018-06-08 12:33:08 +02:00
|
|
|
|
|
|
|
|
2018-10-02 12:21:13 +02:00
|
|
|
def test_request_http_error(caplog):
|
2018-09-29 21:47:17 +02:00
|
|
|
caplog.set_level(logging.DEBUG)
|
2018-06-08 12:33:08 +02:00
|
|
|
link = Link('http://localhost')
|
|
|
|
session = Mock(PipSession)
|
|
|
|
session.get.return_value = resp = Mock()
|
2018-10-02 12:21:13 +02:00
|
|
|
resp.raise_for_status.side_effect = requests.HTTPError('Http error')
|
|
|
|
assert _get_html_page(link, session=session) is None
|
|
|
|
assert (
|
|
|
|
'Could not fetch URL http://localhost: Http error - skipping'
|
|
|
|
in caplog.text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_request_retries(caplog):
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
link = Link('http://localhost')
|
|
|
|
session = Mock(PipSession)
|
|
|
|
session.get.side_effect = requests.exceptions.RetryError('Retry error')
|
|
|
|
assert _get_html_page(link, session=session) is None
|
2018-06-08 12:33:08 +02:00
|
|
|
assert (
|
2018-10-02 12:21:13 +02:00
|
|
|
'Could not fetch URL http://localhost: Retry error - skipping'
|
2018-06-08 12:33:08 +02:00
|
|
|
in caplog.text
|
|
|
|
)
|