1"""
2    test_build_base
3    ~~~~~~~~~~~~~~~
4
5    Test the base build process.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10import shutil
11
12import pytest
13
14from sphinx.testing.util import find_files
15
16
17@pytest.fixture
18def setup_test(app_params):
19    srcdir = app_params.kwargs['srcdir']
20    src_locale_dir = srcdir / 'xx' / 'LC_MESSAGES'
21    dest_locale_dir = srcdir / 'locale'
22    # copy all catalogs into locale layout directory
23    for po in find_files(src_locale_dir, '.po'):
24        copy_po = (dest_locale_dir / 'en' / 'LC_MESSAGES' / po)
25        if not copy_po.parent.exists():
26            copy_po.parent.makedirs()
27        shutil.copy(src_locale_dir / po, copy_po)
28
29    yield
30
31    # delete remnants left over after failed build
32    dest_locale_dir.rmtree(True)
33    (srcdir / '_build').rmtree(True)
34
35
36@pytest.mark.usefixtures('setup_test')
37@pytest.mark.test_params(shared_result='test-catalogs')
38@pytest.mark.sphinx(
39    'html', testroot='intl',
40    confoverrides={'language': 'en', 'locale_dirs': ['./locale']})
41def test_compile_all_catalogs(app, status, warning):
42    app.builder.compile_all_catalogs()
43
44    locale_dir = app.srcdir / 'locale'
45    catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES'
46    expect = {
47        x.replace('.po', '.mo')
48        for x in find_files(catalog_dir, '.po')
49    }
50    actual = set(find_files(catalog_dir, '.mo'))
51    assert actual  # not empty
52    assert actual == expect
53
54
55@pytest.mark.usefixtures('setup_test')
56@pytest.mark.test_params(shared_result='test-catalogs')
57@pytest.mark.sphinx(
58    'html', testroot='intl',
59    confoverrides={'language': 'en', 'locale_dirs': ['./locale']})
60def test_compile_specific_catalogs(app, status, warning):
61    locale_dir = app.srcdir / 'locale'
62    catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES'
63
64    def get_actual():
65        return set(find_files(catalog_dir, '.mo'))
66
67    actual_on_boot = get_actual()  # sphinx.mo might be included
68    app.builder.compile_specific_catalogs([app.srcdir / 'admonitions.txt'])
69    actual = get_actual() - actual_on_boot
70    assert actual == {'admonitions.mo'}
71
72
73@pytest.mark.usefixtures('setup_test')
74@pytest.mark.test_params(shared_result='test-catalogs')
75@pytest.mark.sphinx(
76    'html', testroot='intl',
77    confoverrides={'language': 'en', 'locale_dirs': ['./locale']})
78def test_compile_update_catalogs(app, status, warning):
79    app.builder.compile_update_catalogs()
80
81    locale_dir = app.srcdir / 'locale'
82    catalog_dir = locale_dir / app.config.language / 'LC_MESSAGES'
83    expect = {
84        x.replace('.po', '.mo')
85        for x in find_files(catalog_dir, '.po')
86    }
87    actual = set(find_files(catalog_dir, '.mo'))
88    assert actual  # not empty
89    assert actual == expect
90