2017-03-02 07:51:42 -06:00
|
|
|
"""Test inventory util functions."""
|
2023-07-27 18:39:12 -05:00
|
|
|
import os
|
2017-03-02 07:51:42 -06:00
|
|
|
import posixpath
|
2018-09-23 20:36:42 -05:00
|
|
|
from io import BytesIO
|
2017-03-02 07:51:42 -06:00
|
|
|
|
2024-01-16 20:38:46 -06:00
|
|
|
import sphinx.locale
|
2023-04-07 11:49:36 -05:00
|
|
|
from sphinx.testing.util import SphinxTestApp
|
2023-03-17 12:39:44 -05:00
|
|
|
from sphinx.util.inventory import InventoryFile
|
2017-03-02 07:51:42 -06:00
|
|
|
|
2024-03-25 05:03:44 -05:00
|
|
|
from tests.test_util.intersphinx_data import (
|
|
|
|
INVENTORY_V1,
|
|
|
|
INVENTORY_V2,
|
2024-06-17 05:42:22 -05:00
|
|
|
INVENTORY_V2_AMBIGUOUS_TERMS,
|
2024-03-25 05:03:44 -05:00
|
|
|
INVENTORY_V2_NO_VERSION,
|
|
|
|
)
|
2018-01-29 09:14:53 -06:00
|
|
|
|
2017-03-02 07:51:42 -06:00
|
|
|
|
|
|
|
def test_read_inventory_v1():
|
2024-03-25 05:03:44 -05:00
|
|
|
f = BytesIO(INVENTORY_V1)
|
2017-03-02 07:51:42 -06:00
|
|
|
invdata = InventoryFile.load(f, '/util', posixpath.join)
|
|
|
|
assert invdata['py:module']['module'] == \
|
|
|
|
('foo', '1.0', '/util/foo.html#module-module', '-')
|
|
|
|
assert invdata['py:class']['module.cls'] == \
|
|
|
|
('foo', '1.0', '/util/foo.html#module.cls', '-')
|
|
|
|
|
|
|
|
|
|
|
|
def test_read_inventory_v2():
|
2024-03-25 05:03:44 -05:00
|
|
|
f = BytesIO(INVENTORY_V2)
|
2017-03-02 07:51:42 -06:00
|
|
|
invdata = InventoryFile.load(f, '/util', posixpath.join)
|
|
|
|
|
|
|
|
assert len(invdata['py:module']) == 2
|
|
|
|
assert invdata['py:module']['module1'] == \
|
|
|
|
('foo', '2.0', '/util/foo.html#module-module1', 'Long Module desc')
|
|
|
|
assert invdata['py:module']['module2'] == \
|
|
|
|
('foo', '2.0', '/util/foo.html#module-module2', '-')
|
|
|
|
assert invdata['py:function']['module1.func'][2] == \
|
|
|
|
'/util/sub/foo.html#module1.func'
|
|
|
|
assert invdata['c:function']['CFunc'][2] == '/util/cfunc.html#CFunc'
|
|
|
|
assert invdata['std:term']['a term'][2] == \
|
|
|
|
'/util/glossary.html#term-a-term'
|
|
|
|
assert invdata['std:term']['a term including:colon'][2] == \
|
|
|
|
'/util/glossary.html#term-a-term-including-colon'
|
2018-01-29 09:14:53 -06:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_inventory_v2_not_having_version():
|
2024-03-25 05:03:44 -05:00
|
|
|
f = BytesIO(INVENTORY_V2_NO_VERSION)
|
2018-01-29 09:14:53 -06:00
|
|
|
invdata = InventoryFile.load(f, '/util', posixpath.join)
|
|
|
|
assert invdata['py:module']['module1'] == \
|
|
|
|
('foo', '', '/util/foo.html#module-module1', 'Long Module desc')
|
2023-04-07 11:49:36 -05:00
|
|
|
|
|
|
|
|
2024-06-17 05:42:22 -05:00
|
|
|
def test_ambiguous_definition_warning(warning):
|
|
|
|
f = BytesIO(INVENTORY_V2_AMBIGUOUS_TERMS)
|
|
|
|
InventoryFile.load(f, '/util', posixpath.join)
|
|
|
|
|
|
|
|
assert 'contains multiple definitions for std:term:a' in warning.getvalue().lower()
|
|
|
|
|
|
|
|
|
2023-04-07 11:49:36 -05:00
|
|
|
def _write_appconfig(dir, language, prefix=None):
|
|
|
|
prefix = prefix or language
|
2023-07-27 18:39:12 -05:00
|
|
|
os.makedirs(dir / prefix, exist_ok=True)
|
2023-04-07 11:49:36 -05:00
|
|
|
(dir / prefix / 'conf.py').write_text(f'language = "{language}"', encoding='utf8')
|
|
|
|
(dir / prefix / 'index.rst').write_text('index.rst', encoding='utf8')
|
2023-07-27 18:39:12 -05:00
|
|
|
assert sorted(os.listdir(dir / prefix)) == ['conf.py', 'index.rst']
|
2023-04-07 11:49:36 -05:00
|
|
|
assert (dir / prefix / 'index.rst').exists()
|
2023-07-27 18:39:12 -05:00
|
|
|
return dir / prefix
|
2023-04-07 11:49:36 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _build_inventory(srcdir):
|
|
|
|
app = SphinxTestApp(srcdir=srcdir)
|
|
|
|
app.build()
|
2024-01-16 20:38:46 -06:00
|
|
|
sphinx.locale.translators.clear()
|
2024-03-25 05:03:44 -05:00
|
|
|
return app.outdir / 'objects.inv'
|
2023-04-07 11:49:36 -05:00
|
|
|
|
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
def test_inventory_localization(tmp_path):
|
2023-04-07 11:49:36 -05:00
|
|
|
# Build an app using Estonian (EE) locale
|
2023-07-27 18:39:12 -05:00
|
|
|
srcdir_et = _write_appconfig(tmp_path, "et")
|
2023-04-07 11:49:36 -05:00
|
|
|
inventory_et = _build_inventory(srcdir_et)
|
|
|
|
|
|
|
|
# Build the same app using English (US) locale
|
2023-07-27 18:39:12 -05:00
|
|
|
srcdir_en = _write_appconfig(tmp_path, "en")
|
2023-04-07 11:49:36 -05:00
|
|
|
inventory_en = _build_inventory(srcdir_en)
|
|
|
|
|
|
|
|
# Ensure that the inventory contents differ
|
|
|
|
assert inventory_et.read_bytes() != inventory_en.read_bytes()
|