2010-06-07 14:04:16 +02:00
|
|
|
"""Test the build process with gettext builder with the test root."""
|
|
|
|
|
|
2018-02-19 22:39:14 +09:00
|
|
|
import gettext
|
2010-06-09 06:46:30 +02:00
|
|
|
import os
|
2013-01-05 23:38:21 +09:00
|
|
|
import re
|
2019-01-12 20:14:26 +09:00
|
|
|
import subprocess
|
2023-08-10 10:20:48 +01:00
|
|
|
import sys
|
2022-10-17 15:54:59 +01:00
|
|
|
from subprocess import CalledProcessError
|
2010-06-09 06:46:30 +02:00
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
import pytest
|
|
|
|
|
|
2022-05-23 00:37:45 +09:00
|
|
|
from sphinx.builders.gettext import Catalog, MsgOrigin
|
2023-03-05 15:25:47 +00:00
|
|
|
|
2023-08-10 10:20:48 +01:00
|
|
|
if sys.version_info[:2] >= (3, 11):
|
2023-03-05 15:25:47 +00:00
|
|
|
from contextlib import chdir
|
2023-08-10 10:20:48 +01:00
|
|
|
else:
|
2023-03-05 15:25:47 +00:00
|
|
|
from sphinx.util.osutil import _chdir as chdir
|
2010-06-07 14:04:16 +02:00
|
|
|
|
2024-04-23 06:22:43 +02:00
|
|
|
_MSGID_PATTERN = re.compile(r'msgid "((?:\n|.)*?)"\nmsgstr', re.MULTILINE)
|
2023-08-10 10:20:48 +01:00
|
|
|
|
|
|
|
|
|
2024-04-23 06:22:43 +02:00
|
|
|
def get_msgids(pot):
|
|
|
|
|
matches = _MSGID_PATTERN.findall(pot)
|
|
|
|
|
return [m.replace('"\n"', '') for m in matches[1:]]
|
2023-08-10 10:20:48 +01:00
|
|
|
|
2014-09-21 18:36:27 +02:00
|
|
|
|
2022-05-23 00:37:45 +09:00
|
|
|
def test_Catalog_duplicated_message():
|
|
|
|
|
catalog = Catalog()
|
|
|
|
|
catalog.add('hello', MsgOrigin('/path/to/filename', 1))
|
|
|
|
|
catalog.add('hello', MsgOrigin('/path/to/filename', 1))
|
|
|
|
|
catalog.add('hello', MsgOrigin('/path/to/filename', 2))
|
|
|
|
|
catalog.add('hello', MsgOrigin('/path/to/yetanother', 1))
|
|
|
|
|
catalog.add('world', MsgOrigin('/path/to/filename', 1))
|
|
|
|
|
|
|
|
|
|
assert len(list(catalog)) == 2
|
|
|
|
|
|
|
|
|
|
msg1, msg2 = list(catalog)
|
|
|
|
|
assert msg1.text == 'hello'
|
|
|
|
|
assert msg1.locations == [('/path/to/filename', 1),
|
|
|
|
|
('/path/to/filename', 2),
|
|
|
|
|
('/path/to/yetanother', 1)]
|
|
|
|
|
assert msg2.text == 'world'
|
|
|
|
|
assert msg2.locations == [('/path/to/filename', 1)]
|
|
|
|
|
|
|
|
|
|
|
2017-01-07 00:46:26 +09:00
|
|
|
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
|
|
|
|
|
def test_build_gettext(app):
|
2010-11-14 19:42:50 +01:00
|
|
|
# Generic build; should fail only when the builder is horribly broken.
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2010-06-07 14:04:16 +02:00
|
|
|
|
2010-11-14 19:42:50 +01:00
|
|
|
# Do messages end up in the correct location?
|
|
|
|
|
# top-level documents end up in a message catalog
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'extapi.pot').is_file()
|
2010-11-14 19:42:50 +01:00
|
|
|
# directory items are grouped into sections
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'subdir.pot').is_file()
|
2010-06-09 06:46:30 +02:00
|
|
|
|
2012-12-16 21:21:37 +01:00
|
|
|
# regression test for issue #960
|
2022-04-27 03:04:19 +01:00
|
|
|
catalog = (app.outdir / 'markup.pot').read_text(encoding='utf8')
|
2017-01-07 00:46:26 +09:00
|
|
|
assert 'msgid "something, something else, something more"' in catalog
|
2010-06-17 11:46:49 +02:00
|
|
|
|
2017-01-07 00:46:26 +09:00
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
|
|
|
|
|
def test_msgfmt(app):
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2023-08-10 10:20:48 +01:00
|
|
|
|
2023-07-28 00:39:12 +01:00
|
|
|
(app.outdir / 'en' / 'LC_MESSAGES').mkdir(parents=True, exist_ok=True)
|
2023-03-05 15:25:47 +00:00
|
|
|
with chdir(app.outdir):
|
2010-06-09 06:46:30 +02:00
|
|
|
try:
|
2019-01-12 20:14:26 +09:00
|
|
|
args = ['msginit', '--no-translator', '-i', 'markup.pot', '--locale', 'en_US']
|
2022-10-17 15:54:59 +01:00
|
|
|
subprocess.run(args, capture_output=True, check=True)
|
2010-06-09 06:46:30 +02:00
|
|
|
except OSError:
|
2017-01-07 00:46:26 +09:00
|
|
|
pytest.skip() # most likely msginit was not found
|
2019-01-12 20:14:26 +09:00
|
|
|
except CalledProcessError as exc:
|
|
|
|
|
print(exc.stdout)
|
|
|
|
|
print(exc.stderr)
|
2023-08-13 20:07:28 +01:00
|
|
|
msg = f'msginit exited with return code {exc.returncode}'
|
|
|
|
|
raise AssertionError(msg) from exc
|
2019-01-12 20:14:26 +09:00
|
|
|
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'en_US.po').is_file(), 'msginit failed'
|
2010-06-09 06:54:59 +02:00
|
|
|
try:
|
2019-06-30 14:55:22 +09:00
|
|
|
args = ['msgfmt', 'en_US.po',
|
|
|
|
|
'-o', os.path.join('en', 'LC_MESSAGES', 'test_root.mo')]
|
2022-10-17 15:54:59 +01:00
|
|
|
subprocess.run(args, capture_output=True, check=True)
|
2010-06-09 06:54:59 +02:00
|
|
|
except OSError:
|
2017-01-07 00:46:26 +09:00
|
|
|
pytest.skip() # most likely msgfmt was not found
|
2019-01-12 20:14:26 +09:00
|
|
|
except CalledProcessError as exc:
|
|
|
|
|
print(exc.stdout)
|
|
|
|
|
print(exc.stderr)
|
2023-08-13 20:07:28 +01:00
|
|
|
msg = f'msgfmt exited with return code {exc.returncode}'
|
|
|
|
|
raise AssertionError(msg) from exc
|
2019-01-12 20:14:26 +09:00
|
|
|
|
2017-01-07 00:46:26 +09:00
|
|
|
mo = app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo'
|
2023-07-28 00:39:12 +01:00
|
|
|
assert mo.is_file(), 'msgfmt failed'
|
2010-06-23 06:18:47 +02:00
|
|
|
|
2010-08-15 12:30:13 +02:00
|
|
|
_ = gettext.translation('test_root', app.outdir, languages=['en']).gettext
|
2018-12-15 23:02:28 +09:00
|
|
|
assert _("Testing various markup") == "Testing various markup"
|
2013-01-05 23:38:21 +09:00
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext', testroot='intl', srcdir='gettext',
|
|
|
|
|
confoverrides={'gettext_compact': False})
|
2017-01-07 00:46:26 +09:00
|
|
|
def test_gettext_index_entries(app):
|
2013-01-05 23:38:21 +09:00
|
|
|
# regression test for #976
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(filenames=[app.srcdir / 'index_entries.txt'])
|
2013-01-05 23:38:21 +09:00
|
|
|
|
2022-04-27 03:04:19 +01:00
|
|
|
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
2024-04-23 06:22:43 +02:00
|
|
|
msg_ids = get_msgids(pot)
|
2013-01-05 23:38:21 +09:00
|
|
|
|
2023-08-10 10:20:48 +01:00
|
|
|
assert msg_ids == [
|
2013-01-05 23:38:21 +09:00
|
|
|
"i18n with index entries",
|
|
|
|
|
"index target section",
|
|
|
|
|
"this is :index:`Newsletter` target paragraph.",
|
|
|
|
|
"various index entries",
|
|
|
|
|
"That's all.",
|
|
|
|
|
"Mailing List",
|
|
|
|
|
"Newsletter",
|
|
|
|
|
"Recipients List",
|
|
|
|
|
"First",
|
|
|
|
|
"Second",
|
|
|
|
|
"Third",
|
|
|
|
|
"Entry",
|
|
|
|
|
"See",
|
2016-06-12 00:00:52 +09:00
|
|
|
]
|
2014-09-28 21:19:54 +09:00
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext', testroot='intl', srcdir='gettext',
|
2017-01-07 00:46:26 +09:00
|
|
|
confoverrides={'gettext_compact': False,
|
|
|
|
|
'gettext_additional_targets': []})
|
|
|
|
|
def test_gettext_disable_index_entries(app):
|
2014-09-28 21:19:54 +09:00
|
|
|
# regression test for #976
|
2023-01-04 04:22:20 +00:00
|
|
|
app.env._pickled_doctree_cache.clear() # clear cache
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(filenames=[app.srcdir / 'index_entries.txt'])
|
2014-09-28 21:19:54 +09:00
|
|
|
|
2022-04-27 03:04:19 +01:00
|
|
|
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
2024-04-23 06:22:43 +02:00
|
|
|
msg_ids = get_msgids(pot)
|
2014-09-28 21:19:54 +09:00
|
|
|
|
2023-08-10 10:20:48 +01:00
|
|
|
assert msg_ids == [
|
2014-09-28 21:19:54 +09:00
|
|
|
"i18n with index entries",
|
|
|
|
|
"index target section",
|
|
|
|
|
"this is :index:`Newsletter` target paragraph.",
|
|
|
|
|
"various index entries",
|
|
|
|
|
"That's all.",
|
2013-01-05 23:38:21 +09:00
|
|
|
]
|
2013-03-10 22:07:31 +09:00
|
|
|
|
|
|
|
|
|
2017-01-06 01:14:47 +09:00
|
|
|
@pytest.mark.sphinx('gettext', testroot='intl', srcdir='gettext')
|
2017-01-07 00:46:26 +09:00
|
|
|
def test_gettext_template(app):
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2023-08-10 10:20:48 +01:00
|
|
|
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'sphinx.pot').is_file()
|
2013-03-10 22:07:31 +09:00
|
|
|
|
2022-04-27 03:04:19 +01:00
|
|
|
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
|
2013-03-10 22:07:31 +09:00
|
|
|
assert "Welcome" in result
|
|
|
|
|
assert "Sphinx %(version)s" in result
|
2018-02-18 09:05:32 +09:00
|
|
|
|
|
|
|
|
|
2018-02-19 09:51:30 +09:00
|
|
|
@pytest.mark.sphinx('gettext', testroot='gettext-template')
|
2018-02-18 09:05:32 +09:00
|
|
|
def test_gettext_template_msgid_order_in_sphinxpot(app):
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'sphinx.pot').is_file()
|
2018-02-18 09:05:32 +09:00
|
|
|
|
2022-04-27 03:04:19 +01:00
|
|
|
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
|
2018-02-18 09:05:32 +09:00
|
|
|
assert re.search(
|
|
|
|
|
('msgid "Template 1".*'
|
2018-07-28 20:19:30 +09:00
|
|
|
'msgid "This is Template 1\\.".*'
|
2018-02-18 09:05:32 +09:00
|
|
|
'msgid "Template 2".*'
|
2018-07-28 20:19:30 +09:00
|
|
|
'msgid "This is Template 2\\.".*'),
|
2018-02-18 09:05:32 +09:00
|
|
|
result,
|
2023-08-10 10:20:48 +01:00
|
|
|
flags=re.DOTALL)
|
2020-08-06 09:18:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext', srcdir='root-gettext',
|
|
|
|
|
confoverrides={'gettext_compact': 'documentation'})
|
|
|
|
|
def test_build_single_pot(app):
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2020-08-06 09:18:36 +02:00
|
|
|
|
2023-07-28 00:39:12 +01:00
|
|
|
assert (app.outdir / 'documentation.pot').is_file()
|
2020-08-06 09:18:36 +02:00
|
|
|
|
2022-04-27 03:04:19 +01:00
|
|
|
result = (app.outdir / 'documentation.pot').read_text(encoding='utf8')
|
2020-08-06 09:18:36 +02:00
|
|
|
assert re.search(
|
|
|
|
|
('msgid "Todo".*'
|
|
|
|
|
'msgid "Like footnotes.".*'
|
|
|
|
|
'msgid "The minute.".*'
|
|
|
|
|
'msgid "Generated section".*'),
|
|
|
|
|
result,
|
2023-08-10 10:20:48 +01:00
|
|
|
flags=re.DOTALL)
|
2023-08-10 17:01:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext',
|
|
|
|
|
testroot='intl_substitution_definitions',
|
|
|
|
|
srcdir='gettext-subst',
|
|
|
|
|
confoverrides={'gettext_compact': False,
|
|
|
|
|
'gettext_additional_targets': ['image']})
|
|
|
|
|
def test_gettext_prolog_epilog_substitution(app):
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2023-08-10 17:01:25 +08:00
|
|
|
|
|
|
|
|
assert (app.outdir / 'prolog_epilog_substitution.pot').is_file()
|
|
|
|
|
pot = (app.outdir / 'prolog_epilog_substitution.pot').read_text(encoding='utf8')
|
2024-04-23 06:22:43 +02:00
|
|
|
msg_ids = get_msgids(pot)
|
2023-08-10 10:20:48 +01:00
|
|
|
|
2023-08-10 17:01:25 +08:00
|
|
|
assert msg_ids == [
|
|
|
|
|
"i18n with prologue and epilogue substitutions",
|
|
|
|
|
"This is content that contains |subst_prolog_1|.",
|
|
|
|
|
"Substituted image |subst_prolog_2| here.",
|
|
|
|
|
"subst_prolog_2",
|
|
|
|
|
".. image:: /img.png",
|
|
|
|
|
"This is content that contains |subst_epilog_1|.",
|
|
|
|
|
"Substituted image |subst_epilog_2| here.",
|
|
|
|
|
"subst_epilog_2",
|
|
|
|
|
".. image:: /i18n.png",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext',
|
|
|
|
|
testroot='intl_substitution_definitions',
|
|
|
|
|
srcdir='gettext-subst',
|
|
|
|
|
confoverrides={'gettext_compact': False,
|
|
|
|
|
'gettext_additional_targets': ['image']})
|
|
|
|
|
def test_gettext_prolog_epilog_substitution_excluded(app):
|
|
|
|
|
# regression test for #9428
|
2024-01-17 02:38:46 +00:00
|
|
|
app.build(force_all=True)
|
2023-08-10 17:01:25 +08:00
|
|
|
|
|
|
|
|
assert (app.outdir / 'prolog_epilog_substitution_excluded.pot').is_file()
|
|
|
|
|
pot = (app.outdir / 'prolog_epilog_substitution_excluded.pot').read_text(encoding='utf8')
|
2024-04-23 06:22:43 +02:00
|
|
|
msg_ids = get_msgids(pot)
|
2023-08-10 17:01:25 +08:00
|
|
|
|
2023-08-10 10:20:48 +01:00
|
|
|
assert msg_ids == [
|
2023-08-10 17:01:25 +08:00
|
|
|
"i18n without prologue and epilogue substitutions",
|
|
|
|
|
"This is content that does not include prologue and epilogue substitutions.",
|
|
|
|
|
]
|
2024-04-23 06:22:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(
|
|
|
|
|
'gettext', srcdir='gettext',
|
|
|
|
|
confoverrides={'gettext_compact': False,
|
|
|
|
|
'gettext_additional_targets': ['literal-block', 'doctest-block']})
|
|
|
|
|
def test_gettext_literalblock_additional(app):
|
|
|
|
|
app.build(force_all=True)
|
|
|
|
|
|
|
|
|
|
assert (app.outdir / 'literalblock.pot').is_file()
|
|
|
|
|
pot = (app.outdir / 'literalblock.pot').read_text(encoding='utf8')
|
|
|
|
|
msg_ids = get_msgids(pot)
|
|
|
|
|
|
|
|
|
|
assert msg_ids == [
|
|
|
|
|
'i18n with literal block',
|
|
|
|
|
'Correct literal block::',
|
|
|
|
|
'this is\\nliteral block',
|
|
|
|
|
'Missing literal block::',
|
|
|
|
|
"That's all.",
|
|
|
|
|
'included raw.txt',
|
|
|
|
|
'===\\nRaw\\n===\\n\\n.. raw:: html\\n\\n <iframe src=\\"https://sphinx-doc.org\\"></iframe>\\n\\n',
|
|
|
|
|
'code blocks',
|
|
|
|
|
"def main\\n 'result'\\nend",
|
|
|
|
|
'#include <stdlib.h>\\nint main(int argc, char** argv)\\n{\\n return 0;\\n}',
|
|
|
|
|
'example of C language',
|
|
|
|
|
'#include <stdio.h>\\nint main(int argc, char** argv)\\n{\\n return 0;\\n}',
|
|
|
|
|
'literal-block\\nin list',
|
|
|
|
|
'test_code_for_noqa()\\ncontinued()',
|
|
|
|
|
'doctest blocks',
|
|
|
|
|
'>>> import sys # sys importing\\n>>> def main(): # define main '
|
|
|
|
|
"function\\n... sys.stdout.write('hello') # call write method of "
|
|
|
|
|
"stdout object\\n>>>\\n>>> if __name__ == '__main__': # if run this py "
|
|
|
|
|
'file as python script\\n... main() # call main',
|
|
|
|
|
]
|