| 1 |
"""Test the base build process.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import shutil |
| 6 |
from pathlib import Path |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
if TYPE_CHECKING: |
| 12 |
from collections.abc import Iterator |
| 13 |
|
| 14 |
from sphinx.testing.fixtures import _app_params |
| 15 |
from sphinx.testing.util import SphinxTestApp |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def _setup_test(app_params: _app_params) -> Iterator[None]: |
| 20 |
assert isinstance(app_params.kwargs['srcdir'], Path) |
| 21 |
srcdir = app_params.kwargs['srcdir'] |
| 22 |
src_locale_dir = srcdir / 'xx' / 'LC_MESSAGES' |
| 23 |
dest_locale_dir = srcdir / 'locale' |
| 24 |
# copy all catalogs into locale layout directory |
| 25 |
for po in src_locale_dir.rglob('*.po'): |
| 26 |
copy_po = ( |
| 27 |
dest_locale_dir / 'en' / 'LC_MESSAGES' / po.relative_to(src_locale_dir) |
| 28 |
) |
| 29 |
if not copy_po.parent.exists(): |
| 30 |
copy_po.parent.mkdir(parents=True, exist_ok=True) |
| 31 |
shutil.copy(po, copy_po) |
| 32 |
|
| 33 |
yield |
| 34 |
|
| 35 |
# delete remnants left over after failed build |
| 36 |
shutil.rmtree(dest_locale_dir, ignore_errors=True) |
| 37 |
shutil.rmtree(srcdir / '_build', ignore_errors=True) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.usefixtures('_setup_test') |
| 41 |
@pytest.mark.test_params(shared_result='test-catalogs') |
| 42 |
@pytest.mark.sphinx( |
| 43 |
'html', |
| 44 |
testroot='intl', |
| 45 |
confoverrides={'language': 'en', 'locale_dirs': ['./locale']}, |
| 46 |
) |
| 47 |
def test_compile_all_catalogs(app: SphinxTestApp) -> None: |
| 48 |
app.builder.compile_all_catalogs() |
| 49 |
|
| 50 |
locale_dir = app.srcdir / 'locale' |
| 51 |
catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES' |
| 52 |
expect = {x.with_suffix('.mo') for x in catalog_dir.rglob('*.po')} |
| 53 |
actual = set(catalog_dir.rglob('*.mo')) |
| 54 |
assert actual # not empty |
| 55 |
assert actual == expect |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.usefixtures('_setup_test') |
| 59 |
@pytest.mark.test_params(shared_result='test-catalogs') |
| 60 |
@pytest.mark.sphinx( |
| 61 |
'html', |
| 62 |
testroot='intl', |
| 63 |
confoverrides={'language': 'en', 'locale_dirs': ['./locale']}, |
| 64 |
) |
| 65 |
def test_compile_specific_catalogs(app: SphinxTestApp) -> None: |
| 66 |
locale_dir = app.srcdir / 'locale' |
| 67 |
catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES' |
| 68 |
|
| 69 |
actual_on_boot = set(catalog_dir.rglob('*.mo')) # sphinx.mo might be included |
| 70 |
app.builder.compile_specific_catalogs([app.srcdir / 'admonitions.txt']) |
| 71 |
actual = { |
| 72 |
str(x.relative_to(catalog_dir)) |
| 73 |
for x in catalog_dir.rglob('*.mo') |
| 74 |
if x not in actual_on_boot |
| 75 |
} |
| 76 |
assert actual == {'admonitions.mo'} |
| 77 |
|
| 78 |
|
| 79 |
@pytest.mark.usefixtures('_setup_test') |
| 80 |
@pytest.mark.test_params(shared_result='test-catalogs') |
| 81 |
@pytest.mark.sphinx( |
| 82 |
'html', |
| 83 |
testroot='intl', |
| 84 |
confoverrides={'language': 'en', 'locale_dirs': ['./locale']}, |
| 85 |
) |
| 86 |
def test_compile_update_catalogs(app: SphinxTestApp) -> None: |
| 87 |
app.builder.compile_update_catalogs() |
| 88 |
|
| 89 |
locale_dir = app.srcdir / 'locale' |
| 90 |
catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES' |
| 91 |
expect = {x.with_suffix('.mo') for x in set(catalog_dir.rglob('*.po'))} |
| 92 |
actual = set(catalog_dir.rglob('*.mo')) |
| 93 |
assert actual # not empty |
| 94 |
assert actual == expect |