1
1
import logging
2
2
import os .path
3
+ import uuid
3
4
from textwrap import dedent
4
5
5
6
import mock
10
11
from pip ._vendor .six .moves .urllib import request as urllib_request
11
12
12
13
from pip ._internal .index .collector import (
13
- CacheablePageContent ,
14
14
HTMLPage ,
15
15
_clean_link ,
16
16
_determine_base_url ,
@@ -270,15 +270,17 @@ def test_parse_links__yanked_reason(anchor_html, expected):
270
270
page = HTMLPage (
271
271
html_bytes ,
272
272
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 ()),
274
276
)
275
277
links = list (parse_links (page ))
276
278
link , = links
277
279
actual = link .yanked_reason
278
280
assert actual == expected
279
281
280
282
281
- def test_parse_links_caches_same_page ():
283
+ def test_parse_links_caches_same_page_by_url ():
282
284
html = (
283
285
# Mark this as a unicode string for Python 2 since anchor_html
284
286
# can contain non-ascii.
@@ -292,8 +294,10 @@ def test_parse_links_caches_same_page():
292
294
encoding = None ,
293
295
url = 'https://example.com/simple/' ,
294
296
)
297
+ # Make a second page with zero content, to ensure that it's not accessed,
298
+ # because the page was cached by url.
295
299
page_2 = HTMLPage (
296
- html_bytes ,
300
+ b'' ,
297
301
encoding = None ,
298
302
url = 'https://example.com/simple/' ,
299
303
)
@@ -378,25 +382,6 @@ def test_get_html_page_invalid_scheme(caplog, url, vcs_scheme):
378
382
]
379
383
380
384
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
-
400
385
def make_fake_html_response (url ):
401
386
"""
402
387
Create a fake requests.Response object.
0 commit comments