1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Update tests to use new names

This commit is contained in:
Donald Stufft 2022-06-25 14:45:45 -04:00
parent cdc2582ac9
commit 21a99e4b87

View file

@ -18,10 +18,10 @@ from pip._internal.index.collector import (
_clean_link, _clean_link,
_clean_url_path, _clean_url_path,
_determine_base_url, _determine_base_url,
_get_html_page, _get_index_content,
_get_html_response, _get_simple_response,
_make_html_page, _make_index_content,
_NotHTML, _NotAPIContent,
_NotHTTP, _NotHTTP,
parse_links, parse_links,
) )
@ -40,13 +40,13 @@ from tests.lib import TestData, make_test_link_collector
"file:///opt/data/pip-18.0.tar.gz", "file:///opt/data/pip-18.0.tar.gz",
], ],
) )
def test_get_html_response_archive_to_naive_scheme(url: str) -> None: def test_get_simple_response_archive_to_naive_scheme(url: str) -> None:
""" """
`_get_html_response()` should error on an archive-like URL if the scheme `_get_simple_response()` should error on an archive-like URL if the scheme
does not allow "poking" without getting data. does not allow "poking" without getting data.
""" """
with pytest.raises(_NotHTTP): with pytest.raises(_NotHTTP):
_get_html_response(url, session=mock.Mock(PipSession)) _get_simple_response(url, session=mock.Mock(PipSession))
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -57,12 +57,12 @@ def test_get_html_response_archive_to_naive_scheme(url: str) -> None:
], ],
) )
@mock.patch("pip._internal.index.collector.raise_for_status") @mock.patch("pip._internal.index.collector.raise_for_status")
def test_get_html_response_archive_to_http_scheme( def test_get_simple_response_archive_to_http_scheme(
mock_raise_for_status: mock.Mock, url: str, content_type: str mock_raise_for_status: mock.Mock, url: str, content_type: str
) -> None: ) -> None:
""" """
`_get_html_response()` should send a HEAD request on an archive-like URL `_get_simple_response()` should send a HEAD request on an archive-like URL
if the scheme supports it, and raise `_NotHTML` if the response isn't HTML. if the scheme supports it, and raise `_NotAPIContent` if the response isn't HTML.
""" """
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
session.head.return_value = mock.Mock( session.head.return_value = mock.Mock(
@ -72,8 +72,8 @@ def test_get_html_response_archive_to_http_scheme(
} }
) )
with pytest.raises(_NotHTML) as ctx: with pytest.raises(_NotAPIContent) as ctx:
_get_html_response(url, session=session) _get_simple_response(url, session=session)
session.assert_has_calls( session.assert_has_calls(
[ [
@ -91,10 +91,10 @@ def test_get_html_response_archive_to_http_scheme(
("file:///opt/data/pip-18.0.tar.gz"), ("file:///opt/data/pip-18.0.tar.gz"),
], ],
) )
def test_get_html_page_invalid_content_type_archive( def test_get_index_content_invalid_content_type_archive(
caplog: pytest.LogCaptureFixture, url: str caplog: pytest.LogCaptureFixture, url: str
) -> None: ) -> None:
"""`_get_html_page()` should warn if an archive URL is not HTML """`_get_index_content()` should warn if an archive URL is not HTML
and therefore cannot be used for a HEAD request. and therefore cannot be used for a HEAD request.
""" """
caplog.set_level(logging.WARNING) caplog.set_level(logging.WARNING)
@ -102,7 +102,7 @@ def test_get_html_page_invalid_content_type_archive(
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
assert _get_html_page(link, session=session) is None assert _get_index_content(link, session=session) is None
assert ( assert (
"pip._internal.index.collector", "pip._internal.index.collector",
logging.WARNING, logging.WARNING,
@ -119,11 +119,11 @@ def test_get_html_page_invalid_content_type_archive(
], ],
) )
@mock.patch("pip._internal.index.collector.raise_for_status") @mock.patch("pip._internal.index.collector.raise_for_status")
def test_get_html_response_archive_to_http_scheme_is_html( def test_get_simple_response_archive_to_http_scheme_is_html(
mock_raise_for_status: mock.Mock, url: str mock_raise_for_status: mock.Mock, url: str
) -> None: ) -> None:
""" """
`_get_html_response()` should work with archive-like URLs if the HEAD `_get_simple_response()` should work with archive-like URLs if the HEAD
request is responded with text/html. request is responded with text/html.
""" """
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
@ -135,7 +135,7 @@ def test_get_html_response_archive_to_http_scheme_is_html(
) )
session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"}) session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"})
resp = _get_html_response(url, session=session) resp = _get_simple_response(url, session=session)
assert resp is not None assert resp is not None
assert session.mock_calls == [ assert session.mock_calls == [
@ -163,9 +163,11 @@ def test_get_html_response_archive_to_http_scheme_is_html(
], ],
) )
@mock.patch("pip._internal.index.collector.raise_for_status") @mock.patch("pip._internal.index.collector.raise_for_status")
def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -> None: def test_get_simple_response_no_head(
mock_raise_for_status: mock.Mock, url: str
) -> None:
""" """
`_get_html_response()` shouldn't send a HEAD request if the URL does not `_get_simple_response()` shouldn't send a HEAD request if the URL does not
look like an archive, only the GET request that retrieves data. look like an archive, only the GET request that retrieves data.
""" """
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
@ -179,7 +181,7 @@ def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -
) )
) )
resp = _get_html_response(url, session=session) resp = _get_simple_response(url, session=session)
assert resp is not None assert resp is not None
assert session.head.call_count == 0 assert session.head.call_count == 0
@ -197,11 +199,11 @@ def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -
@mock.patch("pip._internal.index.collector.raise_for_status") @mock.patch("pip._internal.index.collector.raise_for_status")
def test_get_html_response_dont_log_clear_text_password( def test_get_simple_response_dont_log_clear_text_password(
mock_raise_for_status: mock.Mock, caplog: pytest.LogCaptureFixture mock_raise_for_status: mock.Mock, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
""" """
`_get_html_response()` should redact the password from the index URL `_get_simple_response()` should redact the password from the index URL
in its DEBUG log message. in its DEBUG log message.
""" """
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
@ -217,7 +219,7 @@ def test_get_html_response_dont_log_clear_text_password(
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
resp = _get_html_response( resp = _get_simple_response(
"https://user:my_password@example.com/simple/", session=session "https://user:my_password@example.com/simple/", session=session
) )
@ -428,6 +430,7 @@ def _test_parse_links_data_attribute(
html_bytes = html.encode("utf-8") html_bytes = html.encode("utf-8")
page = IndexContent( page = IndexContent(
html_bytes, html_bytes,
"text/html",
encoding=None, encoding=None,
# parse_links() is cached by url, so we inject a random uuid to ensure # parse_links() is cached by url, so we inject a random uuid to ensure
# the page content isn't cached. # the page content isn't cached.
@ -505,6 +508,7 @@ def test_parse_links_caches_same_page_by_url() -> None:
page_1 = IndexContent( page_1 = IndexContent(
html_bytes, html_bytes,
"text/html",
encoding=None, encoding=None,
url=url, url=url,
) )
@ -512,6 +516,7 @@ def test_parse_links_caches_same_page_by_url() -> None:
# because the page was cached by url. # because the page was cached by url.
page_2 = IndexContent( page_2 = IndexContent(
b"", b"",
"text/html",
encoding=None, encoding=None,
url=url, url=url,
) )
@ -520,6 +525,7 @@ def test_parse_links_caches_same_page_by_url() -> None:
# verify that the result is not cached. # verify that the result is not cached.
page_3 = IndexContent( page_3 = IndexContent(
re.sub(b"pkg1", b"pkg2", html_bytes), re.sub(b"pkg1", b"pkg2", html_bytes),
"text/html",
encoding=None, encoding=None,
url=url, url=url,
cache_link_parsing=False, cache_link_parsing=False,
@ -541,7 +547,9 @@ def test_parse_links_caches_same_page_by_url() -> None:
def test_parse_link_handles_deprecated_usage_properly() -> None: def test_parse_link_handles_deprecated_usage_properly() -> None:
html = b'<a href="/pkg1-1.0.tar.gz"></a><a href="/pkg1-2.0.tar.gz"></a>' html = b'<a href="/pkg1-1.0.tar.gz"></a><a href="/pkg1-2.0.tar.gz"></a>'
url = "https://example.com/simple/" url = "https://example.com/simple/"
page = IndexContent(html, encoding=None, url=url, cache_link_parsing=False) page = IndexContent(
html, "text/html", encoding=None, url=url, cache_link_parsing=False
)
parsed_links = list(parse_links(page, use_deprecated_html5lib=True)) parsed_links = list(parse_links(page, use_deprecated_html5lib=True))
@ -559,7 +567,7 @@ def test_request_http_error(
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
session.get.return_value = mock.Mock() session.get.return_value = mock.Mock()
mock_raise_for_status.side_effect = NetworkConnectionError("Http error") mock_raise_for_status.side_effect = NetworkConnectionError("Http error")
assert _get_html_page(link, session=session) is None assert _get_index_content(link, session=session) is None
assert "Could not fetch URL http://localhost: Http error - skipping" in caplog.text assert "Could not fetch URL http://localhost: Http error - skipping" in caplog.text
@ -568,11 +576,11 @@ def test_request_retries(caplog: pytest.LogCaptureFixture) -> None:
link = Link("http://localhost") link = Link("http://localhost")
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
session.get.side_effect = requests.exceptions.RetryError("Retry error") session.get.side_effect = requests.exceptions.RetryError("Retry error")
assert _get_html_page(link, session=session) is None assert _get_index_content(link, session=session) is None
assert "Could not fetch URL http://localhost: Retry error - skipping" in caplog.text assert "Could not fetch URL http://localhost: Retry error - skipping" in caplog.text
def test_make_html_page() -> None: def test_make_index_content() -> None:
headers = {"Content-Type": "text/html; charset=UTF-8"} headers = {"Content-Type": "text/html; charset=UTF-8"}
response = mock.Mock( response = mock.Mock(
content=b"<content>", content=b"<content>",
@ -580,7 +588,7 @@ def test_make_html_page() -> None:
headers=headers, headers=headers,
) )
actual = _make_html_page(response) actual = _make_index_content(response)
assert actual.content == b"<content>" assert actual.content == b"<content>"
assert actual.encoding == "UTF-8" assert actual.encoding == "UTF-8"
assert actual.url == "https://example.com/index.html" assert actual.url == "https://example.com/index.html"
@ -593,15 +601,15 @@ def test_make_html_page() -> None:
("git+https://github.com/pypa/pip.git", "git"), ("git+https://github.com/pypa/pip.git", "git"),
], ],
) )
def test_get_html_page_invalid_scheme( def test_get_index_content_invalid_scheme(
caplog: pytest.LogCaptureFixture, url: str, vcs_scheme: str caplog: pytest.LogCaptureFixture, url: str, vcs_scheme: str
) -> None: ) -> None:
"""`_get_html_page()` should error if an invalid scheme is given. """`_get_index_content()` should error if an invalid scheme is given.
Only file:, http:, https:, and ftp: are allowed. Only file:, http:, https:, and ftp: are allowed.
""" """
with caplog.at_level(logging.WARNING): with caplog.at_level(logging.WARNING):
page = _get_html_page(Link(url), session=mock.Mock(PipSession)) page = _get_index_content(Link(url), session=mock.Mock(PipSession))
assert page is None assert page is None
assert caplog.record_tuples == [ assert caplog.record_tuples == [
@ -622,12 +630,12 @@ def test_get_html_page_invalid_scheme(
], ],
) )
@mock.patch("pip._internal.index.collector.raise_for_status") @mock.patch("pip._internal.index.collector.raise_for_status")
def test_get_html_page_invalid_content_type( def test_get_index_content_invalid_content_type(
mock_raise_for_status: mock.Mock, mock_raise_for_status: mock.Mock,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
content_type: str, content_type: str,
) -> None: ) -> None:
"""`_get_html_page()` should warn if an invalid content-type is given. """`_get_index_content()` should warn if an invalid content-type is given.
Only text/html is allowed. Only text/html is allowed.
""" """
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
@ -641,7 +649,7 @@ def test_get_html_page_invalid_content_type(
"headers": {"Content-Type": content_type}, "headers": {"Content-Type": content_type},
} }
) )
assert _get_html_page(link, session=session) is None assert _get_index_content(link, session=session) is None
mock_raise_for_status.assert_called_once_with(session.get.return_value) mock_raise_for_status.assert_called_once_with(session.get.return_value)
assert ( assert (
"pip._internal.index.collector", "pip._internal.index.collector",
@ -667,8 +675,8 @@ def make_fake_html_response(url: str) -> mock.Mock:
return mock.Mock(content=content, url=url, headers={}) return mock.Mock(content=content, url=url, headers={})
def test_get_html_page_directory_append_index(tmpdir: Path) -> None: def test_get_index_content_directory_append_index(tmpdir: Path) -> None:
"""`_get_html_page()` should append "index.html" to a directory URL.""" """`_get_index_content()` should append "index.html" to a directory URL."""
dirpath = tmpdir / "something" dirpath = tmpdir / "something"
dirpath.mkdir() dirpath.mkdir()
dir_url = dirpath.as_uri() dir_url = dirpath.as_uri()
@ -676,10 +684,10 @@ def test_get_html_page_directory_append_index(tmpdir: Path) -> None:
session = mock.Mock(PipSession) session = mock.Mock(PipSession)
fake_response = make_fake_html_response(expected_url) fake_response = make_fake_html_response(expected_url)
mock_func = mock.patch("pip._internal.index.collector._get_html_response") mock_func = mock.patch("pip._internal.index.collector._get_simple_response")
with mock_func as mock_func: with mock_func as mock_func:
mock_func.return_value = fake_response mock_func.return_value = fake_response
actual = _get_html_page(Link(dir_url), session=session) actual = _get_index_content(Link(dir_url), session=session)
assert mock_func.mock_calls == [ assert mock_func.mock_calls == [
mock.call(expected_url, session=session), mock.call(expected_url, session=session),
], f"actual calls: {mock_func.mock_calls}" ], f"actual calls: {mock_func.mock_calls}"
@ -779,16 +787,16 @@ def check_links_include(links: List[Link], names: List[str]) -> None:
class TestLinkCollector: class TestLinkCollector:
@mock.patch("pip._internal.index.collector._get_html_response") @mock.patch("pip._internal.index.collector._get_simple_response")
def test_fetch_page(self, mock_get_html_response: mock.Mock) -> None: def test_fetch_response(self, mock_get_simple_response: mock.Mock) -> None:
url = "https://pypi.org/simple/twine/" url = "https://pypi.org/simple/twine/"
fake_response = make_fake_html_response(url) fake_response = make_fake_html_response(url)
mock_get_html_response.return_value = fake_response mock_get_simple_response.return_value = fake_response
location = Link(url, cache_link_parsing=False) location = Link(url, cache_link_parsing=False)
link_collector = make_test_link_collector() link_collector = make_test_link_collector()
actual = link_collector.fetch_page(location) actual = link_collector.fetch_response(location)
assert actual is not None assert actual is not None
assert actual.content == fake_response.content assert actual.content == fake_response.content
@ -797,8 +805,8 @@ class TestLinkCollector:
assert actual.cache_link_parsing == location.cache_link_parsing assert actual.cache_link_parsing == location.cache_link_parsing
# Also check that the right session object was passed to # Also check that the right session object was passed to
# _get_html_response(). # _get_simple_response().
mock_get_html_response.assert_called_once_with( mock_get_simple_response.assert_called_once_with(
url, url,
session=link_collector.session, session=link_collector.session,
) )