| 1 |
"""Test inventory util functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
import sphinx.locale |
| 10 |
from sphinx.testing.util import SphinxTestApp |
| 11 |
from sphinx.util.inventory import InventoryFile, _InventoryItem |
| 12 |
|
| 13 |
from tests.test_util.intersphinx_data import ( |
| 14 |
INVENTORY_V1, |
| 15 |
INVENTORY_V2, |
| 16 |
INVENTORY_V2_AMBIGUOUS_TERMS, |
| 17 |
INVENTORY_V2_NO_VERSION, |
| 18 |
) |
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from pathlib import Path |
| 22 |
|
| 23 |
|
| 24 |
def test_read_inventory_v1() -> None: |
| 25 |
inv = InventoryFile.loads(INVENTORY_V1, uri='/util') |
| 26 |
assert inv['py:module', 'module'] == _InventoryItem( |
| 27 |
project_name='foo', |
| 28 |
project_version='1.0', |
| 29 |
uri='/util/foo.html#module-module', |
| 30 |
display_name='-', |
| 31 |
) |
| 32 |
assert inv['py:class', 'module.cls'] == _InventoryItem( |
| 33 |
project_name='foo', |
| 34 |
project_version='1.0', |
| 35 |
uri='/util/foo.html#module.cls', |
| 36 |
display_name='-', |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def test_read_inventory_v2() -> None: |
| 41 |
inv = InventoryFile.loads(INVENTORY_V2, uri='/util') |
| 42 |
|
| 43 |
assert len(inv.data['py:module']) == 2 |
| 44 |
assert inv['py:module', 'module1'] == _InventoryItem( |
| 45 |
project_name='foo', |
| 46 |
project_version='2.0', |
| 47 |
uri='/util/foo.html#module-module1', |
| 48 |
display_name='Long Module desc', |
| 49 |
) |
| 50 |
assert inv['py:module', 'module2'] == _InventoryItem( |
| 51 |
project_name='foo', |
| 52 |
project_version='2.0', |
| 53 |
uri='/util/foo.html#module-module2', |
| 54 |
display_name='-', |
| 55 |
) |
| 56 |
assert inv['py:function', 'module1.func'].uri == ('/util/sub/foo.html#module1.func') |
| 57 |
assert inv['c:function', 'CFunc'].uri == '/util/cfunc.html#CFunc' |
| 58 |
assert inv['std:term', 'a term'].uri == '/util/glossary.html#term-a-term' |
| 59 |
assert inv['std:term', 'a term including:colon'].uri == ( |
| 60 |
'/util/glossary.html#term-a-term-including-colon' |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
def test_read_inventory_v2_not_having_version() -> None: |
| 65 |
inv = InventoryFile.loads(INVENTORY_V2_NO_VERSION, uri='/util') |
| 66 |
assert inv['py:module', 'module1'] == _InventoryItem( |
| 67 |
project_name='foo', |
| 68 |
project_version='', |
| 69 |
uri='/util/foo.html#module-module1', |
| 70 |
display_name='Long Module desc', |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.sphinx('html', testroot='root') |
| 75 |
def test_ambiguous_definition_warning(app: SphinxTestApp) -> None: |
| 76 |
InventoryFile.loads(INVENTORY_V2_AMBIGUOUS_TERMS, uri='/util') |
| 77 |
|
| 78 |
def _multiple_defs_notice_for(entity: str) -> str: |
| 79 |
return f'contains multiple definitions for {entity}' |
| 80 |
|
| 81 |
# was warning-level; reduced to info-level |
| 82 |
# See: https://github.com/sphinx-doc/sphinx/issues/12613 |
| 83 |
mult_defs_a, mult_defs_b = ( |
| 84 |
_multiple_defs_notice_for('std:term:a'), |
| 85 |
_multiple_defs_notice_for('std:term:b'), |
| 86 |
) |
| 87 |
assert mult_defs_a not in app.warning.getvalue().lower() |
| 88 |
assert mult_defs_a not in app.status.getvalue().lower() |
| 89 |
assert mult_defs_b not in app.warning.getvalue().lower() |
| 90 |
assert mult_defs_b in app.status.getvalue().lower() |
| 91 |
|
| 92 |
|
| 93 |
def _write_appconfig(dir: Path, language: str, prefix: str | None = None) -> Path: |
| 94 |
prefix = prefix or language |
| 95 |
(dir / prefix).mkdir(parents=True, exist_ok=True) |
| 96 |
(dir / prefix / 'conf.py').write_text(f'language = "{language}"', encoding='utf8') |
| 97 |
(dir / prefix / 'index.rst').write_text('index.rst', encoding='utf8') |
| 98 |
assert sorted(p.name for p in (dir / prefix).iterdir()) == ['conf.py', 'index.rst'] |
| 99 |
assert (dir / prefix / 'index.rst').exists() |
| 100 |
return dir / prefix |
| 101 |
|
| 102 |
|
| 103 |
def _build_inventory(srcdir: Path) -> Path: |
| 104 |
app = SphinxTestApp(srcdir=srcdir) |
| 105 |
app.build() |
| 106 |
sphinx.locale.translators.clear() |
| 107 |
return app.outdir / 'objects.inv' |
| 108 |
|
| 109 |
|
| 110 |
def test_inventory_localization(tmp_path: Path) -> None: |
| 111 |
# Build an app using Estonian (EE) locale |
| 112 |
srcdir_et = _write_appconfig(tmp_path, 'et') |
| 113 |
inventory_et = _build_inventory(srcdir_et) |
| 114 |
|
| 115 |
# Build the same app using English (US) locale |
| 116 |
srcdir_en = _write_appconfig(tmp_path, 'en') |
| 117 |
inventory_en = _build_inventory(srcdir_en) |
| 118 |
|
| 119 |
# Ensure that the inventory contents differ |
| 120 |
assert inventory_et.read_bytes() != inventory_en.read_bytes() |