/
test_i18n_tool.py
379 lines (340 loc) · 13 KB
/
test_i18n_tool.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# -*- coding: utf-8 -*-
import io
import os
from os.path import abspath, dirname, exists, getmtime, join, realpath
os.environ['SECUREDROP_ENV'] = 'test' # noqa
import i18n_tool
from mock import patch
import pytest
import shutil
import signal
import time
from sh import sed, msginit, pybabel, git, touch
def dummy_translate(po):
buf = None
with io.open(po, "r") as in_file:
buf = in_file.readlines()
with io.open(po, "w") as out_file:
for line in buf:
if line.startswith("msgid "):
out_file.write(line)
idstr = line.split("msgid ", 1)
out_file.write("msgstr {}".format(idstr[1]))
elif line.startswith("msgstr "):
pass
else:
out_file.write(line)
class TestI18NTool(object):
def setup(self):
self.dir = abspath(dirname(realpath(__file__)))
def test_main(self, tmpdir, caplog):
with pytest.raises(SystemExit):
i18n_tool.I18NTool().main(['--help'])
tool = i18n_tool.I18NTool()
with patch.object(tool,
'setup_verbosity',
side_effect=KeyboardInterrupt):
assert tool.main([
'translate-messages',
'--translations-dir', str(tmpdir)
]) == signal.SIGINT
def test_translate_desktop_l10n(self, tmpdir):
in_files = {}
for what in ('source', 'journalist'):
in_files[what] = join(str(tmpdir), what + '.desktop.in')
shutil.copy(join(self.dir, 'i18n/' + what + '.desktop.in'),
in_files[what])
i18n_tool.I18NTool().main([
'--verbose',
'translate-desktop',
'--translations-dir', str(tmpdir),
'--sources', in_files['source'],
'--extract-update',
])
messages_file = join(str(tmpdir), 'desktop.pot')
assert exists(messages_file)
with io.open(messages_file) as fobj:
pot = fobj.read()
assert 'SecureDrop Source Interfaces' in pot
# pretend this happened a few seconds ago
few_seconds_ago = time.time() - 60
os.utime(messages_file, (few_seconds_ago, few_seconds_ago))
i18n_file = join(str(tmpdir), 'source.desktop')
#
# Extract+update but do not compile
#
old_messages_mtime = getmtime(messages_file)
assert not exists(i18n_file)
i18n_tool.I18NTool().main([
'--verbose',
'translate-desktop',
'--translations-dir', str(tmpdir),
'--sources', ",".join(list(in_files.values())),
'--extract-update',
])
assert not exists(i18n_file)
current_messages_mtime = getmtime(messages_file)
assert old_messages_mtime < current_messages_mtime
locale = 'fr_FR'
po_file = join(str(tmpdir), locale + ".po")
msginit(
'--no-translator',
'--locale', locale,
'--output', po_file,
'--input', messages_file)
source = 'SecureDrop Source Interfaces'
sed('-i', '-e',
'/{}/,+1s/msgstr ""/msgstr "SOURCE FR"/'.format(source),
po_file)
assert exists(po_file)
# Regression test to trigger bug introduced when adding
# Romanian as an accepted language.
locale = 'ro'
po_file = join(str(tmpdir), locale + ".po")
msginit(
'--no-translator',
'--locale', locale,
'--output', po_file,
'--input', messages_file)
source = 'SecureDrop Source Interfaces'
sed('-i', '-e',
'/{}/,+1s/msgstr ""/msgstr "SOURCE RO"/'.format(source),
po_file)
assert exists(po_file)
#
# Compile but do not extract+update
#
old_messages_mtime = current_messages_mtime
i18n_tool.I18NTool().main([
'--verbose',
'translate-desktop',
'--translations-dir', str(tmpdir),
'--sources', ",".join(list(in_files.values()) + ['BOOM']),
'--compile',
])
assert old_messages_mtime == getmtime(messages_file)
with io.open(po_file) as fobj:
po = fobj.read()
assert 'SecureDrop Source Interfaces' in po
assert 'SecureDrop Journalist Interfaces' not in po
with io.open(i18n_file) as fobj:
i18n = fobj.read()
assert 'SOURCE FR' in i18n
def test_translate_messages_l10n(self, tmpdir):
source = [
join(self.dir, 'i18n/code.py'),
join(self.dir, 'i18n/template.html'),
]
args = [
'--verbose',
'translate-messages',
'--translations-dir', str(tmpdir),
'--mapping', join(self.dir, 'i18n/babel.cfg'),
'--sources', ",".join(source),
'--extract-update',
'--compile',
]
i18n_tool.I18NTool().main(args)
messages_file = join(str(tmpdir), 'messages.pot')
assert exists(messages_file)
with io.open(messages_file, 'rb') as fobj:
pot = fobj.read()
assert b'code hello i18n' in pot
assert b'template hello i18n' in pot
locale = 'en_US'
locale_dir = join(str(tmpdir), locale)
pybabel('init', '-i', messages_file, '-d', str(tmpdir), '-l', locale)
po_file = join(locale_dir, 'LC_MESSAGES/messages.po')
dummy_translate(po_file)
mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo')
assert not exists(mo_file)
i18n_tool.I18NTool().main(args)
assert exists(mo_file)
with io.open(mo_file, mode='rb') as fobj:
mo = fobj.read()
assert b'code hello i18n' in mo
assert b'template hello i18n' in mo
def test_translate_messages_compile_arg(self, tmpdir):
args = [
'--verbose',
'translate-messages',
'--translations-dir', str(tmpdir),
'--mapping', join(self.dir, 'i18n/babel.cfg'),
]
i18n_tool.I18NTool().main(args + [
'--sources', join(self.dir, 'i18n/code.py'),
'--extract-update',
])
messages_file = join(str(tmpdir), 'messages.pot')
assert exists(messages_file)
with io.open(messages_file) as fobj:
pot = fobj.read()
assert 'code hello i18n' in pot
locale = 'en_US'
locale_dir = join(str(tmpdir), locale)
po_file = join(locale_dir, 'LC_MESSAGES/messages.po')
pybabel(['init', '-i', messages_file, '-d', str(tmpdir), '-l', locale])
assert exists(po_file)
# pretend this happened a few seconds ago
few_seconds_ago = time.time() - 60
os.utime(po_file, (few_seconds_ago, few_seconds_ago))
mo_file = join(locale_dir, 'LC_MESSAGES/messages.mo')
#
# Extract+update but do not compile
#
old_po_mtime = getmtime(po_file)
assert not exists(mo_file)
i18n_tool.I18NTool().main(args + [
'--sources', join(self.dir, 'i18n/code.py'),
'--extract-update',
])
assert not exists(mo_file)
current_po_mtime = getmtime(po_file)
assert old_po_mtime < current_po_mtime
#
# Translation would occur here - let's fake it
dummy_translate(po_file)
#
# Compile but do not extract+update
#
source = [
join(self.dir, 'i18n/code.py'),
join(self.dir, 'i18n/template.html'),
]
current_po_mtime = getmtime(po_file)
i18n_tool.I18NTool().main(args + [
'--sources', ",".join(source),
'--compile',
])
assert current_po_mtime == getmtime(po_file)
with io.open(mo_file, mode='rb') as fobj:
mo = fobj.read()
assert b'code hello i18n' in mo
assert b'template hello i18n' not in mo
def test_require_git_email_name(self, tmpdir):
k = {'_cwd': str(tmpdir)}
git('init', **k)
with pytest.raises(Exception) as excinfo:
i18n_tool.I18NTool.require_git_email_name(str(tmpdir))
assert 'please set name' in str(excinfo.value)
git.config('user.email', "you@example.com", **k)
git.config('user.name', "Your Name", **k)
assert i18n_tool.I18NTool.require_git_email_name(str(tmpdir))
def test_update_docs(self, tmpdir, caplog):
k = {'_cwd': str(tmpdir)}
git.init(**k)
git.config('user.email', "you@example.com", **k)
git.config('user.name', "Your Name", **k)
os.makedirs(join(str(tmpdir), 'docs/includes'))
touch('docs/includes/l10n.txt', **k)
git.add('docs/includes/l10n.txt', **k)
git.commit('-m', 'init', **k)
i18n_tool.I18NTool().main([
'--verbose',
'update-docs',
'--docs-repo-dir', str(tmpdir)])
assert 'l10n.txt updated' in caplog.text
caplog.clear()
i18n_tool.I18NTool().main([
'--verbose',
'update-docs',
'--docs-repo-dir', str(tmpdir)])
assert 'l10n.txt already up to date' in caplog.text
def test_update_from_weblate(self, tmpdir, caplog):
d = str(tmpdir)
for repo in ('i18n', 'securedrop'):
os.mkdir(join(d, repo))
k = {'_cwd': join(d, repo)}
git.init(**k)
git.config('user.email', 'you@example.com', **k)
git.config('user.name', 'Loïc Nordhøy', **k)
touch('README.md', **k)
git.add('README.md', **k)
git.commit('-m', 'README', 'README.md', **k)
for o in os.listdir(join(self.dir, 'i18n')):
f = join(self.dir, 'i18n', o)
if os.path.isfile(f):
shutil.copyfile(f, join(d, 'i18n', o))
else:
shutil.copytree(f, join(d, 'i18n', o))
k = {'_cwd': join(d, 'i18n')}
git.add('securedrop', 'install_files', **k)
git.commit('-m', 'init', '-a', **k)
git.checkout('-b', 'i18n', 'master', **k)
def r():
return "".join([str(l) for l in caplog.records])
#
# de_DE is not amount the supported languages, it is not taken
# into account despite the fact that it exists in weblate.
#
caplog.clear()
i18n_tool.I18NTool().main([
'--verbose',
'update-from-weblate',
'--root', join(str(tmpdir), 'securedrop'),
'--url', join(str(tmpdir), 'i18n'),
'--supported-languages', 'nl',
])
assert 'l10n: updated Dutch (nl)' in r()
assert 'l10n: updated German (de_DE)' not in r()
#
# de_DE is added but there is no change in the nl translation
# therefore nothing is done for nl
#
caplog.clear()
i18n_tool.I18NTool().main([
'--verbose',
'update-from-weblate',
'--root', join(str(tmpdir), 'securedrop'),
'--url', join(str(tmpdir), 'i18n'),
'--supported-languages', 'nl,de_DE',
])
assert 'l10n: updated Dutch (nl)' not in r()
assert 'l10n: updated German (de_DE)' in r()
#
# nothing new for nl or de_DE: nothing is done
#
caplog.clear()
i18n_tool.I18NTool().main([
'--verbose',
'update-from-weblate',
'--root', join(str(tmpdir), 'securedrop'),
'--url', join(str(tmpdir), 'i18n'),
'--supported-languages', 'nl,de_DE',
])
assert 'l10n: updated Dutch (nl)' not in r()
assert 'l10n: updated German (de_DE)' not in r()
message = str(git('--no-pager', '-C', 'securedrop', 'show',
_cwd=d, _encoding='utf-8'))
assert "Loïc" in message
#
# an update is done to nl in weblate
#
k = {'_cwd': join(d, 'i18n')}
f = 'securedrop/translations/nl/LC_MESSAGES/messages.po'
sed('-i', '-e', 's/inactiviteit/INACTIVITEIT/', f, **k)
git.add(f, **k)
git.config('user.email', 'somone@else.com', **k)
git.config('user.name', 'Someone Else', **k)
git.commit('-m', 'translation change', f, **k)
k = {'_cwd': join(d, 'securedrop')}
git.config('user.email', 'somone@else.com', **k)
git.config('user.name', 'Someone Else', **k)
#
# the nl translation update from weblate is copied
# over.
#
caplog.clear()
i18n_tool.I18NTool().main([
'--verbose',
'update-from-weblate',
'--root', join(str(tmpdir), 'securedrop'),
'--url', join(str(tmpdir), 'i18n'),
'--supported-languages', 'nl,de_DE',
])
assert 'l10n: updated Dutch (nl)' in r()
assert 'l10n: updated German (de_DE)' not in r()
message = str(git('--no-pager', '-C', 'securedrop', 'show',
_cwd=d))
assert "Someone Else" in message
assert "Loïc" not in message