11import logging
22import os .path
3+ import uuid
34from textwrap import dedent
45
56import mock
1011from pip ._vendor .six .moves .urllib import request as urllib_request
1112
1213from pip ._internal .index .collector import (
13- CacheablePageContent ,
1414 HTMLPage ,
1515 _clean_link ,
1616 _determine_base_url ,
@@ -270,15 +270,17 @@ def test_parse_links__yanked_reason(anchor_html, expected):
270270 page = HTMLPage (
271271 html_bytes ,
272272 encoding = None ,
273- url = 'https://example.com/simple/' ,
273+ # parse_links() is cached by url, so we inject a random uuid to ensure
274+ # the page content isn't cached.
275+ url = 'https://example.com/simple-{}/' .format (uuid .uuid4 ()),
274276 )
275277 links = list (parse_links (page ))
276278 link , = links
277279 actual = link .yanked_reason
278280 assert actual == expected
279281
280282
281- def test_parse_links_caches_same_page ():
283+ def test_parse_links_caches_same_page_by_url ():
282284 html = (
283285 # Mark this as a unicode string for Python 2 since anchor_html
284286 # can contain non-ascii.
@@ -292,8 +294,10 @@ def test_parse_links_caches_same_page():
292294 encoding = None ,
293295 url = 'https://example.com/simple/' ,
294296 )
297+ # Make a second page with zero content, to ensure that it's not accessed,
298+ # because the page was cached by url.
295299 page_2 = HTMLPage (
296- html_bytes ,
300+ b'' ,
297301 encoding = None ,
298302 url = 'https://example.com/simple/' ,
299303 )
@@ -378,25 +382,6 @@ def test_get_html_page_invalid_scheme(caplog, url, vcs_scheme):
378382 ]
379383
380384
381- def test_get_html_page_caches_same_link ():
382- link = Link ('https://example.com/link-1/' )
383- session = mock .Mock (PipSession )
384-
385- fake_response = make_fake_html_response (link .url )
386- mock_func = mock .patch ("pip._internal.index.collector._get_html_response" )
387- with mock_func as mock_func :
388- mock_func .return_value = fake_response
389- page_1 = _get_html_page (link , session = session )
390- mock_func .assert_called_once ()
391-
392- with mock_func as mock_func :
393- page_2 = _get_html_page (link , session = session )
394- # Assert that the result of the cached html page fetch will also then
395- # be cached by parse_links() and @with_cached_html_pages.
396- assert CacheablePageContent (page_1 ) == CacheablePageContent (page_2 )
397- mock_func .assert_not_called ()
398-
399-
400385def make_fake_html_response (url ):
401386 """
402387 Create a fake requests.Response object.
0 commit comments