Skip to content

Commit 21a99e4

Browse files
committed
Update tests to use new names
1 parent cdc2582 commit 21a99e4

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

tests/unit/test_collector.py

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
_clean_link,
1919
_clean_url_path,
2020
_determine_base_url,
21-
_get_html_page,
22-
_get_html_response,
23-
_make_html_page,
24-
_NotHTML,
21+
_get_index_content,
22+
_get_simple_response,
23+
_make_index_content,
24+
_NotAPIContent,
2525
_NotHTTP,
2626
parse_links,
2727
)
@@ -40,13 +40,13 @@
4040
"file:///opt/data/pip-18.0.tar.gz",
4141
],
4242
)
43-
def test_get_html_response_archive_to_naive_scheme(url: str) -> None:
43+
def test_get_simple_response_archive_to_naive_scheme(url: str) -> None:
4444
"""
45-
`_get_html_response()` should error on an archive-like URL if the scheme
45+
`_get_simple_response()` should error on an archive-like URL if the scheme
4646
does not allow "poking" without getting data.
4747
"""
4848
with pytest.raises(_NotHTTP):
49-
_get_html_response(url, session=mock.Mock(PipSession))
49+
_get_simple_response(url, session=mock.Mock(PipSession))
5050

5151

5252
@pytest.mark.parametrize(
@@ -57,12 +57,12 @@ def test_get_html_response_archive_to_naive_scheme(url: str) -> None:
5757
],
5858
)
5959
@mock.patch("pip._internal.index.collector.raise_for_status")
60-
def test_get_html_response_archive_to_http_scheme(
60+
def test_get_simple_response_archive_to_http_scheme(
6161
mock_raise_for_status: mock.Mock, url: str, content_type: str
6262
) -> None:
6363
"""
64-
`_get_html_response()` should send a HEAD request on an archive-like URL
65-
if the scheme supports it, and raise `_NotHTML` if the response isn't HTML.
64+
`_get_simple_response()` should send a HEAD request on an archive-like URL
65+
if the scheme supports it, and raise `_NotAPIContent` if the response isn't HTML.
6666
"""
6767
session = mock.Mock(PipSession)
6868
session.head.return_value = mock.Mock(
@@ -72,8 +72,8 @@ def test_get_html_response_archive_to_http_scheme(
7272
}
7373
)
7474

75-
with pytest.raises(_NotHTML) as ctx:
76-
_get_html_response(url, session=session)
75+
with pytest.raises(_NotAPIContent) as ctx:
76+
_get_simple_response(url, session=session)
7777

7878
session.assert_has_calls(
7979
[
@@ -91,18 +91,18 @@ def test_get_html_response_archive_to_http_scheme(
9191
("file:///opt/data/pip-18.0.tar.gz"),
9292
],
9393
)
94-
def test_get_html_page_invalid_content_type_archive(
94+
def test_get_index_content_invalid_content_type_archive(
9595
caplog: pytest.LogCaptureFixture, url: str
9696
) -> None:
97-
"""`_get_html_page()` should warn if an archive URL is not HTML
97+
"""`_get_index_content()` should warn if an archive URL is not HTML
9898
and therefore cannot be used for a HEAD request.
9999
"""
100100
caplog.set_level(logging.WARNING)
101101
link = Link(url)
102102

103103
session = mock.Mock(PipSession)
104104

105-
assert _get_html_page(link, session=session) is None
105+
assert _get_index_content(link, session=session) is None
106106
assert (
107107
"pip._internal.index.collector",
108108
logging.WARNING,
@@ -119,11 +119,11 @@ def test_get_html_page_invalid_content_type_archive(
119119
],
120120
)
121121
@mock.patch("pip._internal.index.collector.raise_for_status")
122-
def test_get_html_response_archive_to_http_scheme_is_html(
122+
def test_get_simple_response_archive_to_http_scheme_is_html(
123123
mock_raise_for_status: mock.Mock, url: str
124124
) -> None:
125125
"""
126-
`_get_html_response()` should work with archive-like URLs if the HEAD
126+
`_get_simple_response()` should work with archive-like URLs if the HEAD
127127
request is responded with text/html.
128128
"""
129129
session = mock.Mock(PipSession)
@@ -135,7 +135,7 @@ def test_get_html_response_archive_to_http_scheme_is_html(
135135
)
136136
session.get.return_value = mock.Mock(headers={"Content-Type": "text/html"})
137137

138-
resp = _get_html_response(url, session=session)
138+
resp = _get_simple_response(url, session=session)
139139

140140
assert resp is not None
141141
assert session.mock_calls == [
@@ -163,9 +163,11 @@ def test_get_html_response_archive_to_http_scheme_is_html(
163163
],
164164
)
165165
@mock.patch("pip._internal.index.collector.raise_for_status")
166-
def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -> None:
166+
def test_get_simple_response_no_head(
167+
mock_raise_for_status: mock.Mock, url: str
168+
) -> None:
167169
"""
168-
`_get_html_response()` shouldn't send a HEAD request if the URL does not
170+
`_get_simple_response()` shouldn't send a HEAD request if the URL does not
169171
look like an archive, only the GET request that retrieves data.
170172
"""
171173
session = mock.Mock(PipSession)
@@ -179,7 +181,7 @@ def test_get_html_response_no_head(mock_raise_for_status: mock.Mock, url: str) -
179181
)
180182
)
181183

182-
resp = _get_html_response(url, session=session)
184+
resp = _get_simple_response(url, session=session)
183185

184186
assert resp is not None
185187
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) -
197199

198200

199201
@mock.patch("pip._internal.index.collector.raise_for_status")
200-
def test_get_html_response_dont_log_clear_text_password(
202+
def test_get_simple_response_dont_log_clear_text_password(
201203
mock_raise_for_status: mock.Mock, caplog: pytest.LogCaptureFixture
202204
) -> None:
203205
"""
204-
`_get_html_response()` should redact the password from the index URL
206+
`_get_simple_response()` should redact the password from the index URL
205207
in its DEBUG log message.
206208
"""
207209
session = mock.Mock(PipSession)
@@ -217,7 +219,7 @@ def test_get_html_response_dont_log_clear_text_password(
217219

218220
caplog.set_level(logging.DEBUG)
219221

220-
resp = _get_html_response(
222+
resp = _get_simple_response(
221223
"https://user:[email protected]/simple/", session=session
222224
)
223225

@@ -428,6 +430,7 @@ def _test_parse_links_data_attribute(
428430
html_bytes = html.encode("utf-8")
429431
page = IndexContent(
430432
html_bytes,
433+
"text/html",
431434
encoding=None,
432435
# parse_links() is cached by url, so we inject a random uuid to ensure
433436
# the page content isn't cached.
@@ -505,13 +508,15 @@ def test_parse_links_caches_same_page_by_url() -> None:
505508

506509
page_1 = IndexContent(
507510
html_bytes,
511+
"text/html",
508512
encoding=None,
509513
url=url,
510514
)
511515
# Make a second page with zero content, to ensure that it's not accessed,
512516
# because the page was cached by url.
513517
page_2 = IndexContent(
514518
b"",
519+
"text/html",
515520
encoding=None,
516521
url=url,
517522
)
@@ -520,6 +525,7 @@ def test_parse_links_caches_same_page_by_url() -> None:
520525
# verify that the result is not cached.
521526
page_3 = IndexContent(
522527
re.sub(b"pkg1", b"pkg2", html_bytes),
528+
"text/html",
523529
encoding=None,
524530
url=url,
525531
cache_link_parsing=False,
@@ -541,7 +547,9 @@ def test_parse_links_caches_same_page_by_url() -> None:
541547
def test_parse_link_handles_deprecated_usage_properly() -> None:
542548
html = b'<a href="/pkg1-1.0.tar.gz"></a><a href="/pkg1-2.0.tar.gz"></a>'
543549
url = "https://example.com/simple/"
544-
page = IndexContent(html, encoding=None, url=url, cache_link_parsing=False)
550+
page = IndexContent(
551+
html, "text/html", encoding=None, url=url, cache_link_parsing=False
552+
)
545553

546554
parsed_links = list(parse_links(page, use_deprecated_html5lib=True))
547555

@@ -559,7 +567,7 @@ def test_request_http_error(
559567
session = mock.Mock(PipSession)
560568
session.get.return_value = mock.Mock()
561569
mock_raise_for_status.side_effect = NetworkConnectionError("Http error")
562-
assert _get_html_page(link, session=session) is None
570+
assert _get_index_content(link, session=session) is None
563571
assert "Could not fetch URL http://localhost: Http error - skipping" in caplog.text
564572

565573

@@ -568,19 +576,19 @@ def test_request_retries(caplog: pytest.LogCaptureFixture) -> None:
568576
link = Link("http://localhost")
569577
session = mock.Mock(PipSession)
570578
session.get.side_effect = requests.exceptions.RetryError("Retry error")
571-
assert _get_html_page(link, session=session) is None
579+
assert _get_index_content(link, session=session) is None
572580
assert "Could not fetch URL http://localhost: Retry error - skipping" in caplog.text
573581

574582

575-
def test_make_html_page() -> None:
583+
def test_make_index_content() -> None:
576584
headers = {"Content-Type": "text/html; charset=UTF-8"}
577585
response = mock.Mock(
578586
content=b"<content>",
579587
url="https://example.com/index.html",
580588
headers=headers,
581589
)
582590

583-
actual = _make_html_page(response)
591+
actual = _make_index_content(response)
584592
assert actual.content == b"<content>"
585593
assert actual.encoding == "UTF-8"
586594
assert actual.url == "https://example.com/index.html"
@@ -593,15 +601,15 @@ def test_make_html_page() -> None:
593601
("git+https://github.com/pypa/pip.git", "git"),
594602
],
595603
)
596-
def test_get_html_page_invalid_scheme(
604+
def test_get_index_content_invalid_scheme(
597605
caplog: pytest.LogCaptureFixture, url: str, vcs_scheme: str
598606
) -> None:
599-
"""`_get_html_page()` should error if an invalid scheme is given.
607+
"""`_get_index_content()` should error if an invalid scheme is given.
600608
601609
Only file:, http:, https:, and ftp: are allowed.
602610
"""
603611
with caplog.at_level(logging.WARNING):
604-
page = _get_html_page(Link(url), session=mock.Mock(PipSession))
612+
page = _get_index_content(Link(url), session=mock.Mock(PipSession))
605613

606614
assert page is None
607615
assert caplog.record_tuples == [
@@ -622,12 +630,12 @@ def test_get_html_page_invalid_scheme(
622630
],
623631
)
624632
@mock.patch("pip._internal.index.collector.raise_for_status")
625-
def test_get_html_page_invalid_content_type(
633+
def test_get_index_content_invalid_content_type(
626634
mock_raise_for_status: mock.Mock,
627635
caplog: pytest.LogCaptureFixture,
628636
content_type: str,
629637
) -> None:
630-
"""`_get_html_page()` should warn if an invalid content-type is given.
638+
"""`_get_index_content()` should warn if an invalid content-type is given.
631639
Only text/html is allowed.
632640
"""
633641
caplog.set_level(logging.DEBUG)
@@ -641,7 +649,7 @@ def test_get_html_page_invalid_content_type(
641649
"headers": {"Content-Type": content_type},
642650
}
643651
)
644-
assert _get_html_page(link, session=session) is None
652+
assert _get_index_content(link, session=session) is None
645653
mock_raise_for_status.assert_called_once_with(session.get.return_value)
646654
assert (
647655
"pip._internal.index.collector",
@@ -667,19 +675,19 @@ def make_fake_html_response(url: str) -> mock.Mock:
667675
return mock.Mock(content=content, url=url, headers={})
668676

669677

670-
def test_get_html_page_directory_append_index(tmpdir: Path) -> None:
671-
"""`_get_html_page()` should append "index.html" to a directory URL."""
678+
def test_get_index_content_directory_append_index(tmpdir: Path) -> None:
679+
"""`_get_index_content()` should append "index.html" to a directory URL."""
672680
dirpath = tmpdir / "something"
673681
dirpath.mkdir()
674682
dir_url = dirpath.as_uri()
675683
expected_url = "{}/index.html".format(dir_url.rstrip("/"))
676684

677685
session = mock.Mock(PipSession)
678686
fake_response = make_fake_html_response(expected_url)
679-
mock_func = mock.patch("pip._internal.index.collector._get_html_response")
687+
mock_func = mock.patch("pip._internal.index.collector._get_simple_response")
680688
with mock_func as mock_func:
681689
mock_func.return_value = fake_response
682-
actual = _get_html_page(Link(dir_url), session=session)
690+
actual = _get_index_content(Link(dir_url), session=session)
683691
assert mock_func.mock_calls == [
684692
mock.call(expected_url, session=session),
685693
], f"actual calls: {mock_func.mock_calls}"
@@ -779,16 +787,16 @@ def check_links_include(links: List[Link], names: List[str]) -> None:
779787

780788

781789
class TestLinkCollector:
782-
@mock.patch("pip._internal.index.collector._get_html_response")
783-
def test_fetch_page(self, mock_get_html_response: mock.Mock) -> None:
790+
@mock.patch("pip._internal.index.collector._get_simple_response")
791+
def test_fetch_response(self, mock_get_simple_response: mock.Mock) -> None:
784792
url = "https://pypi.org/simple/twine/"
785793

786794
fake_response = make_fake_html_response(url)
787-
mock_get_html_response.return_value = fake_response
795+
mock_get_simple_response.return_value = fake_response
788796

789797
location = Link(url, cache_link_parsing=False)
790798
link_collector = make_test_link_collector()
791-
actual = link_collector.fetch_page(location)
799+
actual = link_collector.fetch_response(location)
792800

793801
assert actual is not None
794802
assert actual.content == fake_response.content
@@ -797,8 +805,8 @@ def test_fetch_page(self, mock_get_html_response: mock.Mock) -> None:
797805
assert actual.cache_link_parsing == location.cache_link_parsing
798806

799807
# Also check that the right session object was passed to
800-
# _get_html_response().
801-
mock_get_html_response.assert_called_once_with(
808+
# _get_simple_response().
809+
mock_get_simple_response.assert_called_once_with(
802810
url,
803811
session=link_collector.session,
804812
)

0 commit comments

Comments
 (0)