forked from sphinx-doc/sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_build_gettext.py
204 lines (163 loc) · 6.4 KB
/
test_build_gettext.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""Test the build process with gettext builder with the test root."""
import gettext
import os
import re
import subprocess
from subprocess import CalledProcessError
import pytest
from sphinx.builders.gettext import Catalog, MsgOrigin
try:
from contextlib import chdir
except ImportError:
from sphinx.util.osutil import _chdir as chdir
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)]
@pytest.mark.sphinx('gettext', srcdir='root-gettext')
def test_build_gettext(app):
# Generic build; should fail only when the builder is horribly broken.
app.builder.build_all()
# Do messages end up in the correct location?
# top-level documents end up in a message catalog
assert (app.outdir / 'extapi.pot').is_file()
# directory items are grouped into sections
assert (app.outdir / 'subdir.pot').is_file()
# regression test for issue #960
catalog = (app.outdir / 'markup.pot').read_text(encoding='utf8')
assert 'msgid "something, something else, something more"' in catalog
@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:
args = ['msginit', '--no-translator', '-i', 'markup.pot', '--locale', 'en_US']
subprocess.run(args, capture_output=True, check=True)
except OSError:
pytest.skip() # most likely msginit was not found
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
raise AssertionError('msginit exited with return code %s' % exc.returncode)
assert (app.outdir / 'en_US.po').is_file(), 'msginit failed'
try:
args = ['msgfmt', 'en_US.po',
'-o', os.path.join('en', 'LC_MESSAGES', 'test_root.mo')]
subprocess.run(args, capture_output=True, check=True)
except OSError:
pytest.skip() # most likely msgfmt was not found
except CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
raise AssertionError('msgfmt exited with return code %s' % exc.returncode)
mo = app.outdir / 'en' / 'LC_MESSAGES' / 'test_root.mo'
assert mo.is_file(), 'msgfmt failed'
_ = gettext.translation('test_root', app.outdir, languages=['en']).gettext
assert _("Testing various markup") == "Testing various markup"
@pytest.mark.sphinx(
'gettext', testroot='intl', srcdir='gettext',
confoverrides={'gettext_compact': False})
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]
expected_msgids = [
"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",
]
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',
confoverrides={'gettext_compact': False,
'gettext_additional_targets': []})
def test_gettext_disable_index_entries(app):
# regression test for #976
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]
expected_msgids = [
"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()
assert (app.outdir / 'sphinx.pot').is_file()
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
assert "Welcome" in result
assert "Sphinx %(version)s" in result
@pytest.mark.sphinx('gettext', testroot='gettext-template')
def test_gettext_template_msgid_order_in_sphinxpot(app):
app.builder.build_all()
assert (app.outdir / 'sphinx.pot').is_file()
result = (app.outdir / 'sphinx.pot').read_text(encoding='utf8')
assert re.search(
('msgid "Template 1".*'
'msgid "This is Template 1\\.".*'
'msgid "Template 2".*'
'msgid "This is Template 2\\.".*'),
result,
flags=re.S)
@pytest.mark.sphinx(
'gettext', srcdir='root-gettext',
confoverrides={'gettext_compact': 'documentation'})
def test_build_single_pot(app):
app.builder.build_all()
assert (app.outdir / 'documentation.pot').is_file()
result = (app.outdir / 'documentation.pot').read_text(encoding='utf8')
assert re.search(
('msgid "Todo".*'
'msgid "Like footnotes.".*'
'msgid "The minute.".*'
'msgid "Generated section".*'),
result,
flags=re.S)