Skip to content

Commit

Permalink
Spring-clean test_build_gettext (#11573)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Aug 10, 2023
1 parent c52d55e commit 4dd2ed4
Showing 1 changed file with 23 additions and 60 deletions.
83 changes: 23 additions & 60 deletions tests/test_build_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
import os
import re
import subprocess
import sys
from subprocess import CalledProcessError

import pytest

from sphinx.builders.gettext import Catalog, MsgOrigin

try:
if sys.version_info[:2] >= (3, 11):
from contextlib import chdir
except ImportError:
else:
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():
catalog = Catalog()
Expand Down Expand Up @@ -54,6 +63,7 @@ def test_build_gettext(app):
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
def test_msgfmt(app):
app.builder.build_all()

(app.outdir / 'en' / 'LC_MESSAGES').mkdir(parents=True, exist_ok=True)
with chdir(app.outdir):
try:
Expand Down Expand Up @@ -92,18 +102,10 @@ def test_gettext_index_entries(app):
# regression test for #976
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')
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",
"index target section",
"this is :index:`Newsletter` target paragraph.",
Expand All @@ -118,12 +120,6 @@ def msgid_getter(msgid):
"Entry",
"See",
]
for expect in expected_msgids:
assert expect in msgids
msgids.remove(expect)

# unexpected msgid existent
assert msgids == []


@pytest.mark.sphinx(
Expand All @@ -135,35 +131,22 @@ def test_gettext_disable_index_entries(app):
app.env._pickled_doctree_cache.clear() # clear cache
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')
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",
"index target section",
"this is :index:`Newsletter` target paragraph.",
"various index entries",
"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')
def test_gettext_template(app):
app.build()
app.builder.build_all()

assert (app.outdir / 'sphinx.pot').is_file()

result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
Expand All @@ -183,7 +166,7 @@ def test_gettext_template_msgid_order_in_sphinxpot(app):
'msgid "Template 2".*'
'msgid "This is Template 2\\.".*'),
result,
flags=re.S)
flags=re.DOTALL)


@pytest.mark.sphinx(
Expand All @@ -201,7 +184,7 @@ def test_build_single_pot(app):
'msgid "The minute.".*'
'msgid "Generated section".*'),
result,
flags=re.S)
flags=re.DOTALL)


@pytest.mark.sphinx(
Expand All @@ -213,16 +196,10 @@ def test_build_single_pot(app):
def test_gettext_prolog_epilog_substitution(app):
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()
pot = (app.outdir / 'prolog_epilog_substitution.pot').read_text(encoding='utf8')
msg_ids = list(filter(None, map(msgid_getter, pot.splitlines())))

assert msg_ids == [
"i18n with prologue and epilogue substitutions",
"This is content that contains |subst_prolog_1|.",
Expand All @@ -246,25 +223,11 @@ def test_gettext_prolog_epilog_substitution_excluded(app):
# regression test for #9428
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()
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",
"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 == []

0 comments on commit 4dd2ed4

Please sign in to comment.