mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Spring-clean `test_build_gettext
` (#11573)
This commit is contained in:
parent
c52d55ebd7
commit
4dd2ed4bde
@ -4,17 +4,26 @@ import gettext
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
from subprocess import CalledProcessError
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from sphinx.builders.gettext import Catalog, MsgOrigin
|
from sphinx.builders.gettext import Catalog, MsgOrigin
|
||||||
|
|
||||||
try:
|
if sys.version_info[:2] >= (3, 11):
|
||||||
from contextlib import chdir
|
from contextlib import chdir
|
||||||
except ImportError:
|
else:
|
||||||
from sphinx.util.osutil import _chdir as chdir
|
from sphinx.util.osutil import _chdir as chdir
|
||||||
|
|
||||||
|
_MSGID_PATTERN = re.compile(r'msgid "(.*)"')
|
||||||
|
|
||||||
|
|
||||||
|
def msgid_getter(msgid):
|
||||||
|
if m := _MSGID_PATTERN.search(msgid):
|
||||||
|
return m[1]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def test_Catalog_duplicated_message():
|
def test_Catalog_duplicated_message():
|
||||||
catalog = Catalog()
|
catalog = Catalog()
|
||||||
@ -54,6 +63,7 @@ def test_build_gettext(app):
|
|||||||
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
|
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
|
||||||
def test_msgfmt(app):
|
def test_msgfmt(app):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
|
|
||||||
(app.outdir / 'en' / 'LC_MESSAGES').mkdir(parents=True, exist_ok=True)
|
(app.outdir / 'en' / 'LC_MESSAGES').mkdir(parents=True, exist_ok=True)
|
||||||
with chdir(app.outdir):
|
with chdir(app.outdir):
|
||||||
try:
|
try:
|
||||||
@ -92,18 +102,10 @@ def test_gettext_index_entries(app):
|
|||||||
# regression test for #976
|
# regression test for #976
|
||||||
app.builder.build(['index_entries'])
|
app.builder.build(['index_entries'])
|
||||||
|
|
||||||
_msgid_getter = re.compile(r'msgid "(.*)"').search
|
|
||||||
|
|
||||||
def msgid_getter(msgid):
|
|
||||||
m = _msgid_getter(msgid)
|
|
||||||
if m:
|
|
||||||
return m.groups()[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
||||||
msgids = [_f for _f in map(msgid_getter, pot.splitlines()) if _f]
|
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
|
||||||
|
|
||||||
expected_msgids = [
|
assert msg_ids == [
|
||||||
"i18n with index entries",
|
"i18n with index entries",
|
||||||
"index target section",
|
"index target section",
|
||||||
"this is :index:`Newsletter` target paragraph.",
|
"this is :index:`Newsletter` target paragraph.",
|
||||||
@ -118,12 +120,6 @@ def test_gettext_index_entries(app):
|
|||||||
"Entry",
|
"Entry",
|
||||||
"See",
|
"See",
|
||||||
]
|
]
|
||||||
for expect in expected_msgids:
|
|
||||||
assert expect in msgids
|
|
||||||
msgids.remove(expect)
|
|
||||||
|
|
||||||
# unexpected msgid existent
|
|
||||||
assert msgids == []
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx(
|
@pytest.mark.sphinx(
|
||||||
@ -135,35 +131,22 @@ def test_gettext_disable_index_entries(app):
|
|||||||
app.env._pickled_doctree_cache.clear() # clear cache
|
app.env._pickled_doctree_cache.clear() # clear cache
|
||||||
app.builder.build(['index_entries'])
|
app.builder.build(['index_entries'])
|
||||||
|
|
||||||
_msgid_getter = re.compile(r'msgid "(.*)"').search
|
|
||||||
|
|
||||||
def msgid_getter(msgid):
|
|
||||||
m = _msgid_getter(msgid)
|
|
||||||
if m:
|
|
||||||
return m.groups()[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
pot = (app.outdir / 'index_entries.pot').read_text(encoding='utf8')
|
||||||
msgids = [_f for _f in map(msgid_getter, pot.splitlines()) if _f]
|
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
|
||||||
|
|
||||||
expected_msgids = [
|
assert msg_ids == [
|
||||||
"i18n with index entries",
|
"i18n with index entries",
|
||||||
"index target section",
|
"index target section",
|
||||||
"this is :index:`Newsletter` target paragraph.",
|
"this is :index:`Newsletter` target paragraph.",
|
||||||
"various index entries",
|
"various index entries",
|
||||||
"That's all.",
|
"That's all.",
|
||||||
]
|
]
|
||||||
for expect in expected_msgids:
|
|
||||||
assert expect in msgids
|
|
||||||
msgids.remove(expect)
|
|
||||||
|
|
||||||
# unexpected msgid existent
|
|
||||||
assert msgids == []
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('gettext', testroot='intl', srcdir='gettext')
|
@pytest.mark.sphinx('gettext', testroot='intl', srcdir='gettext')
|
||||||
def test_gettext_template(app):
|
def test_gettext_template(app):
|
||||||
app.build()
|
app.builder.build_all()
|
||||||
|
|
||||||
assert (app.outdir / 'sphinx.pot').is_file()
|
assert (app.outdir / 'sphinx.pot').is_file()
|
||||||
|
|
||||||
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
|
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
|
||||||
@ -183,7 +166,7 @@ def test_gettext_template_msgid_order_in_sphinxpot(app):
|
|||||||
'msgid "Template 2".*'
|
'msgid "Template 2".*'
|
||||||
'msgid "This is Template 2\\.".*'),
|
'msgid "This is Template 2\\.".*'),
|
||||||
result,
|
result,
|
||||||
flags=re.S)
|
flags=re.DOTALL)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx(
|
@pytest.mark.sphinx(
|
||||||
@ -201,7 +184,7 @@ def test_build_single_pot(app):
|
|||||||
'msgid "The minute.".*'
|
'msgid "The minute.".*'
|
||||||
'msgid "Generated section".*'),
|
'msgid "Generated section".*'),
|
||||||
result,
|
result,
|
||||||
flags=re.S)
|
flags=re.DOTALL)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx(
|
@pytest.mark.sphinx(
|
||||||
@ -213,16 +196,10 @@ def test_build_single_pot(app):
|
|||||||
def test_gettext_prolog_epilog_substitution(app):
|
def test_gettext_prolog_epilog_substitution(app):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
|
|
||||||
_msgid_pattern = re.compile(r'msgid "(.*)"')
|
|
||||||
|
|
||||||
def msgid_getter(msgid):
|
|
||||||
if m := _msgid_pattern.search(msgid):
|
|
||||||
return m.groups()[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
assert (app.outdir / 'prolog_epilog_substitution.pot').is_file()
|
assert (app.outdir / 'prolog_epilog_substitution.pot').is_file()
|
||||||
pot = (app.outdir / 'prolog_epilog_substitution.pot').read_text(encoding='utf8')
|
pot = (app.outdir / 'prolog_epilog_substitution.pot').read_text(encoding='utf8')
|
||||||
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
|
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
|
||||||
|
|
||||||
assert msg_ids == [
|
assert msg_ids == [
|
||||||
"i18n with prologue and epilogue substitutions",
|
"i18n with prologue and epilogue substitutions",
|
||||||
"This is content that contains |subst_prolog_1|.",
|
"This is content that contains |subst_prolog_1|.",
|
||||||
@ -246,25 +223,11 @@ def test_gettext_prolog_epilog_substitution_excluded(app):
|
|||||||
# regression test for #9428
|
# regression test for #9428
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
|
|
||||||
_msgid_getter = re.compile(r'msgid "(.*)"').search
|
|
||||||
|
|
||||||
def msgid_getter(msgid):
|
|
||||||
m = _msgid_getter(msgid)
|
|
||||||
if m:
|
|
||||||
return m.groups()[0]
|
|
||||||
return None
|
|
||||||
|
|
||||||
assert (app.outdir / 'prolog_epilog_substitution_excluded.pot').is_file()
|
assert (app.outdir / 'prolog_epilog_substitution_excluded.pot').is_file()
|
||||||
pot = (app.outdir / 'prolog_epilog_substitution_excluded.pot').read_text(encoding='utf8')
|
pot = (app.outdir / 'prolog_epilog_substitution_excluded.pot').read_text(encoding='utf8')
|
||||||
msgids = [_f for _f in map(msgid_getter, pot.splitlines()) if _f]
|
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))
|
||||||
|
|
||||||
expected_msgids = [
|
assert msg_ids == [
|
||||||
"i18n without prologue and epilogue substitutions",
|
"i18n without prologue and epilogue substitutions",
|
||||||
"This is content that does not include prologue and epilogue substitutions.",
|
"This is content that does not include prologue and epilogue substitutions.",
|
||||||
]
|
]
|
||||||
for expect in expected_msgids:
|
|
||||||
assert expect in msgids
|
|
||||||
msgids.remove(expect)
|
|
||||||
|
|
||||||
# unexpected msgid existent
|
|
||||||
assert msgids == []
|
|
||||||
|
Loading…
Reference in New Issue
Block a user