|
4 | 4 | import zlib
|
5 | 5 | from io import BytesIO
|
6 | 6 |
|
| 7 | +from sphinx.testing.util import SphinxTestApp |
7 | 8 | from sphinx.util.inventory import InventoryFile
|
8 | 9 |
|
9 | 10 | inventory_v1 = b'''\
|
@@ -83,3 +84,58 @@ def test_read_inventory_v2_not_having_version():
|
83 | 84 | invdata = InventoryFile.load(f, '/util', posixpath.join)
|
84 | 85 | assert invdata['py:module']['module1'] == \
|
85 | 86 | ('foo', '', '/util/foo.html#module-module1', 'Long Module desc')
|
| 87 | + |
| 88 | + |
| 89 | +def _write_appconfig(dir, language, prefix=None): |
| 90 | + prefix = prefix or language |
| 91 | + (dir / prefix).makedirs() |
| 92 | + (dir / prefix / 'conf.py').write_text(f'language = "{language}"', encoding='utf8') |
| 93 | + (dir / prefix / 'index.rst').write_text('index.rst', encoding='utf8') |
| 94 | + assert sorted((dir / prefix).listdir()) == ['conf.py', 'index.rst'] |
| 95 | + assert (dir / prefix / 'index.rst').exists() |
| 96 | + return (dir / prefix) |
| 97 | + |
| 98 | + |
| 99 | +def _build_inventory(srcdir): |
| 100 | + app = SphinxTestApp(srcdir=srcdir) |
| 101 | + app.build() |
| 102 | + app.cleanup() |
| 103 | + return (app.outdir / 'objects.inv') |
| 104 | + |
| 105 | + |
| 106 | +def test_inventory_localization(tempdir): |
| 107 | + # Build an app using Estonian (EE) locale |
| 108 | + srcdir_et = _write_appconfig(tempdir, "et") |
| 109 | + inventory_et = _build_inventory(srcdir_et) |
| 110 | + |
| 111 | + # Build the same app using English (US) locale |
| 112 | + srcdir_en = _write_appconfig(tempdir, "en") |
| 113 | + inventory_en = _build_inventory(srcdir_en) |
| 114 | + |
| 115 | + # Ensure that the inventory contents differ |
| 116 | + assert inventory_et.read_bytes() != inventory_en.read_bytes() |
| 117 | + |
| 118 | + |
| 119 | +def test_inventory_reproducible(tempdir, monkeypatch): |
| 120 | + with monkeypatch.context() as m: |
| 121 | + # Configure reproducible builds |
| 122 | + # See: https://reproducible-builds.org/docs/source-date-epoch/ |
| 123 | + m.setenv("SOURCE_DATE_EPOCH", "0") |
| 124 | + |
| 125 | + # Build an app using Estonian (EE) locale |
| 126 | + srcdir_et = _write_appconfig(tempdir, "et") |
| 127 | + reproducible_inventory_et = _build_inventory(srcdir_et) |
| 128 | + |
| 129 | + # Build the same app using English (US) locale |
| 130 | + srcdir_en = _write_appconfig(tempdir, "en") |
| 131 | + reproducible_inventory_en = _build_inventory(srcdir_en) |
| 132 | + |
| 133 | + # Also build the app using Estonian (EE) locale without build reproducibility enabled |
| 134 | + srcdir_et = _write_appconfig(tempdir, "et", prefix="localized") |
| 135 | + localized_inventory_et = _build_inventory(srcdir_et) |
| 136 | + |
| 137 | + # Ensure that the reproducible inventory contents are identical |
| 138 | + assert reproducible_inventory_et.read_bytes() == reproducible_inventory_en.read_bytes() |
| 139 | + |
| 140 | + # Ensure that inventory contents are different between a localized and non-localized build |
| 141 | + assert reproducible_inventory_et.read_bytes() != localized_inventory_et.read_bytes() |
0 commit comments