@@ -745,30 +745,57 @@ def test_intersphinx_role(app):
745745
746746
747747@pytest .mark .sphinx ('html' , testroot = 'root' )
748- def test_intersphinx_cache_limit (app ):
748+ @pytest .mark .parametrize (
749+ ('cache_limit' , 'expected_expired' ),
750+ [
751+ (5 , False ),
752+ (1 , True ),
753+ (0 , True ),
754+ (- 1 , False ),
755+ ],
756+ )
757+ def test_intersphinx_cache_limit (app , monkeypatch , cache_limit , expected_expired ):
749758 url = 'https://example.org/'
750- app .config .intersphinx_cache_limit = - 1
759+ app .config .intersphinx_cache_limit = cache_limit
751760 app .config .intersphinx_mapping = {
752761 'inv' : (url , None ),
753762 }
754763 # load the inventory and check if it's done correctly
755764 intersphinx_cache : dict [str , InventoryCacheEntry ] = {
756- url : ('' , 0 , {}), # 0 is a timestamp, make sure the entry is expired
765+ url : ('inv ' , 0 , {}), # Timestamp of last cache write is zero.
757766 }
758767 validate_intersphinx_mapping (app , app .config )
759- load_mappings (app )
760768
761- now = int (time .time ())
769+ # The test's `now` is two days after the cache was created.
770+ now = 2 * 86400
771+ monkeypatch .setattr ('time.time' , lambda : now )
772+
773+ # `_fetch_inventory_group` calls `_fetch_inventory`.
774+ # We replace it with a mock to test whether it has been called.
775+ # If it has been called, it means the cache had expired.
776+ mock_fetch_inventory = mock .Mock (return_value = ('inv' , now , {}))
777+ monkeypatch .setattr (
778+ 'sphinx.ext.intersphinx._load._fetch_inventory' , mock_fetch_inventory
779+ )
780+
762781 for name , (uri , locations ) in app .config .intersphinx_mapping .values ():
763782 project = _IntersphinxProject (name = name , target_uri = uri , locations = locations )
764- # no need to read from remote
765- assert not _fetch_inventory_group (
783+ updated = _fetch_inventory_group (
766784 project = project ,
767785 cache = intersphinx_cache ,
768786 now = now ,
769787 config = app .config ,
770788 srcdir = app .srcdir ,
771789 )
790+ # If we hadn't mocked `_fetch_inventory`, it would've made
791+ # a request to `https://example.org/` and found no inventory
792+ # file. That would've been an error, and `updated` would've been
793+ # False even if the cache had expired. The mock makes it behave
794+ # "correctly".
795+ assert updated is expected_expired
796+ # Double-check: If the cache was expired, `mock_fetch_inventory`
797+ # must've been called.
798+ assert mock_fetch_inventory .called is expected_expired
772799
773800
774801def test_intersphinx_fetch_inventory_group_url ():
0 commit comments