Debian Codegraph @

tests/i18n/test_compilation.py

raw ◀ ▶
1 Debian patch touch this file
1 import gettext as gettext_module
2 import os
•3 import re
4 import stat
5 import unittest
6 from io import StringIO
7 from pathlib import Path
8 from subprocess import run
9 from unittest import mock
10
11 from django.core.management import CommandError, call_command, execute_from_command_line
•12 from django.core.management.utils import find_command, popen_wrapper
13 from django.test import SimpleTestCase, override_settings
14 from django.test.utils import captured_stderr, captured_stdout
15 from django.utils import translation
•16 from django.utils.encoding import DEFAULT_LOCALE_ENCODING
17 from django.utils.functional import cached_property
18 from django.utils.translation import gettext
19
20 from .utils import RunInTmpDirMixin, copytree
21
22 has_msgfmt = find_command("msgfmt")
23
24
25 @unittest.skipUnless(has_msgfmt, "msgfmt is mandatory for compilation tests")
26 class MessageCompilationTests(RunInTmpDirMixin, SimpleTestCase):
27 work_subdir = "commands"
28
29
30 class PoFileTests(MessageCompilationTests):
31 LOCALE = "es_AR"
32 MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
33 MO_FILE_EN = "locale/en/LC_MESSAGES/django.mo"
34
35 def test_bom_rejection(self):
36 stderr = StringIO()
37 with self.assertRaisesMessage(
38 CommandError, "compilemessages generated one or more errors."
39 ):
40 call_command(
41 "compilemessages", locale=[self.LOCALE], verbosity=0, stderr=stderr
42 )
43 self.assertIn("file has a BOM (Byte Order Mark)", stderr.getvalue())
44 self.assertFalse(os.path.exists(self.MO_FILE))
45
46 def test_no_write_access(self):
47 mo_file_en = Path(self.MO_FILE_EN)
48 err_buffer = StringIO()
49 # Put parent directory in read-only mode.
50 old_mode = mo_file_en.parent.stat().st_mode
51 mo_file_en.parent.chmod(stat.S_IRUSR | stat.S_IXUSR)
52 # Ensure .po file is more recent than .mo file.
53 mo_file_en.with_suffix(".po").touch()
54 try:
55 with self.assertRaisesMessage(
56 CommandError, "compilemessages generated one or more errors."
57 ):
58 call_command(
59 "compilemessages", locale=["en"], stderr=err_buffer, verbosity=0
60 )
61 self.assertIn("not writable location", err_buffer.getvalue())
62 finally:
63 mo_file_en.parent.chmod(old_mode)
64
65 def test_no_compile_when_unneeded(self):
66 mo_file_en = Path(self.MO_FILE_EN)
67 mo_file_en.touch()
68 stdout = StringIO()
69 call_command("compilemessages", locale=["en"], stdout=stdout, verbosity=1)
70 msg = "%s” is already compiled and up to date." % mo_file_en.with_suffix(".po")
71 self.assertIn(msg, stdout.getvalue())
72
73
74 class PoFileContentsTests(MessageCompilationTests):
75 # Ticket #11240
76
77 LOCALE = "fr"
78 MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
79
80 def test_percent_symbol_in_po_file(self):
81 call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
82 self.assertTrue(os.path.exists(self.MO_FILE))
83
84
85 class MultipleLocaleCompilationTests(MessageCompilationTests):
86 MO_FILE_HR = None
87 MO_FILE_FR = None
88
89 def setUp(self):
90 super().setUp()
91 localedir = os.path.join(self.test_dir, "locale")
92 self.MO_FILE_HR = os.path.join(localedir, "hr/LC_MESSAGES/django.mo")
93 self.MO_FILE_FR = os.path.join(localedir, "fr/LC_MESSAGES/django.mo")
94
95 def test_one_locale(self):
96 with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
97 call_command("compilemessages", locale=["hr"], verbosity=0)
98
99 self.assertTrue(os.path.exists(self.MO_FILE_HR))
100
101 def test_multiple_locales(self):
102 with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
103 call_command("compilemessages", locale=["hr", "fr"], verbosity=0)
104
105 self.assertTrue(os.path.exists(self.MO_FILE_HR))
106 self.assertTrue(os.path.exists(self.MO_FILE_FR))
107
108
109 class ExcludedLocaleCompilationTests(MessageCompilationTests):
110 work_subdir = "exclude"
111
112 MO_FILE = "locale/%s/LC_MESSAGES/django.mo"
113
114 def setUp(self):
115 super().setUp()
116 copytree("canned_locale", "locale")
117
118 def test_command_help(self):
119 with captured_stdout(), captured_stderr():
120 # `call_command` bypasses the parser; by calling
121 # `execute_from_command_line` with the help subcommand we
122 # ensure that there are no issues with the parser itself.
123 execute_from_command_line(["django-admin", "help", "compilemessages"])
124
125 def test_one_locale_excluded(self):
126 call_command("compilemessages", exclude=["it"], verbosity=0)
127 self.assertTrue(os.path.exists(self.MO_FILE % "en"))
128 self.assertTrue(os.path.exists(self.MO_FILE % "fr"))
129 self.assertFalse(os.path.exists(self.MO_FILE % "it"))
130
131 def test_multiple_locales_excluded(self):
132 call_command("compilemessages", exclude=["it", "fr"], verbosity=0)
133 self.assertTrue(os.path.exists(self.MO_FILE % "en"))
134 self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
135 self.assertFalse(os.path.exists(self.MO_FILE % "it"))
136
137 def test_one_locale_excluded_with_locale(self):
138 call_command(
139 "compilemessages", locale=["en", "fr"], exclude=["fr"], verbosity=0
140 )
141 self.assertTrue(os.path.exists(self.MO_FILE % "en"))
142 self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
143 self.assertFalse(os.path.exists(self.MO_FILE % "it"))
144
145 def test_multiple_locales_excluded_with_locale(self):
146 call_command(
147 "compilemessages",
148 locale=["en", "fr", "it"],
149 exclude=["fr", "it"],
150 verbosity=0,
151 )
152 self.assertTrue(os.path.exists(self.MO_FILE % "en"))
153 self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
154 self.assertFalse(os.path.exists(self.MO_FILE % "it"))
155
156
157 class IgnoreDirectoryCompilationTests(MessageCompilationTests):
158 # Reuse the exclude directory since it contains some locale fixtures.
159 work_subdir = "exclude"
160 MO_FILE = "%s/%s/LC_MESSAGES/django.mo"
161 CACHE_DIR = Path("cache") / "locale"
162 NESTED_DIR = Path("outdated") / "v1" / "locale"
163
164 def setUp(self):
165 super().setUp()
166 copytree("canned_locale", "locale")
167 copytree("canned_locale", self.CACHE_DIR)
168 copytree("canned_locale", self.NESTED_DIR)
169
170 def assertAllExist(self, dir, langs):
171 self.assertTrue(
172 all(Path(self.MO_FILE % (dir, lang)).exists() for lang in langs)
173 )
174
175 def assertNoneExist(self, dir, langs):
176 self.assertTrue(
177 all(Path(self.MO_FILE % (dir, lang)).exists() is False for lang in langs)
178 )
179
180 def test_one_locale_dir_ignored(self):
181 call_command("compilemessages", ignore=["cache"], verbosity=0)
182 self.assertAllExist("locale", ["en", "fr", "it"])
183 self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
184 self.assertAllExist(self.NESTED_DIR, ["en", "fr", "it"])
185
186 def test_multiple_locale_dirs_ignored(self):
187 call_command(
188 "compilemessages", ignore=["cache/locale", "outdated"], verbosity=0
189 )
190 self.assertAllExist("locale", ["en", "fr", "it"])
191 self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
192 self.assertNoneExist(self.NESTED_DIR, ["en", "fr", "it"])
193
194 def test_ignores_based_on_pattern(self):
195 call_command("compilemessages", ignore=["*/locale"], verbosity=0)
196 self.assertAllExist("locale", ["en", "fr", "it"])
197 self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
198 self.assertNoneExist(self.NESTED_DIR, ["en", "fr", "it"])
199
200 def test_no_dirs_accidentally_skipped(self):
201 os_walk_results = [
202 # To discover .po filepaths, compilemessages uses with a starting list of
203 # basedirs to inspect, which in this scenario are:
204 # ["conf/locale", "locale"]
205 # Then os.walk is used to discover other locale dirs, ignoring dirs matching
206 # `ignore_patterns`. Mock the results to place an ignored directory directly
207 # before and after a directory named "locale".
208 [("somedir", ["ignore", "locale", "ignore"], [])],
209 # This will result in three basedirs discovered:
210 # ["conf/locale", "locale", "somedir/locale"]
211 # os.walk is called for each locale in each basedir looking for .po files.
212 # In this scenario, we need to mock os.walk results for "en", "fr", and "it"
213 # locales for each basedir:
214 [("exclude/locale/LC_MESSAGES", [], ["en.po"])],
215 [("exclude/locale/LC_MESSAGES", [], ["fr.po"])],
216 [("exclude/locale/LC_MESSAGES", [], ["it.po"])],
217 [("exclude/conf/locale/LC_MESSAGES", [], ["en.po"])],
218 [("exclude/conf/locale/LC_MESSAGES", [], ["fr.po"])],
219 [("exclude/conf/locale/LC_MESSAGES", [], ["it.po"])],
220 [("exclude/somedir/locale/LC_MESSAGES", [], ["en.po"])],
221 [("exclude/somedir/locale/LC_MESSAGES", [], ["fr.po"])],
222 [("exclude/somedir/locale/LC_MESSAGES", [], ["it.po"])],
223 ]
224
225 module_path = "django.core.management.commands.compilemessages"
226 with mock.patch(f"{module_path}.os.walk", side_effect=os_walk_results):
227 with mock.patch(f"{module_path}.os.path.isdir", return_value=True):
228 with mock.patch(
229 f"{module_path}.Command.compile_messages"
230 ) as mock_compile_messages:
231 call_command("compilemessages", ignore=["ignore"], verbosity=4)
232
233 expected = [
234 (
235 [
236 ("exclude/locale/LC_MESSAGES", "en.po"),
237 ("exclude/locale/LC_MESSAGES", "fr.po"),
238 ("exclude/locale/LC_MESSAGES", "it.po"),
239 ],
240 ),
241 (
242 [
243 ("exclude/conf/locale/LC_MESSAGES", "en.po"),
244 ("exclude/conf/locale/LC_MESSAGES", "fr.po"),
245 ("exclude/conf/locale/LC_MESSAGES", "it.po"),
246 ],
247 ),
248 (
249 [
250 ("exclude/somedir/locale/LC_MESSAGES", "en.po"),
251 ("exclude/somedir/locale/LC_MESSAGES", "fr.po"),
252 ("exclude/somedir/locale/LC_MESSAGES", "it.po"),
253 ],
254 ),
255 ]
256 self.assertEqual([c.args for c in mock_compile_messages.mock_calls], expected)
257
258
259 class CompilationErrorHandling(MessageCompilationTests):
•260 @cached_property
261 def msgfmt_version(self):
262 # Note that msgfmt is installed via GNU gettext tools, hence the msgfmt
263 # version should align to gettext.
264 out, err, status = popen_wrapper(
265 ["msgfmt", "--version"],
266 stdout_encoding=DEFAULT_LOCALE_ENCODING,
267 )
268 m = re.search(r"(\d+)\.(\d+)\.?(\d+)?", out)
269 return tuple(int(d) for d in m.groups() if d is not None)
270
271 def test_error_reported_by_msgfmt(self):
272 # po file contains wrong po formatting.
273 with self.assertRaises(CommandError):
274 call_command("compilemessages", locale=["ja"], verbosity=0)
275 # It should still fail a second time.
276 with self.assertRaises(CommandError):
277 call_command("compilemessages", locale=["ja"], verbosity=0)
278
279 def test_msgfmt_error_including_non_ascii(self):
280 # po file contains invalid msgstr content (triggers non-ascii error content).
281 # Make sure the output of msgfmt is unaffected by the current locale.
282 env = os.environ.copy()
283 env.update({"LC_ALL": "C"})
284 with mock.patch(
285 "django.core.management.utils.run",
286 lambda *args, **kwargs: run(*args, env=env, **kwargs),
287 ):
288 stderr = StringIO()
289 with self.assertRaisesMessage(
290 CommandError, "compilemessages generated one or more errors"
291 ):
292 call_command(
293 "compilemessages", locale=["ko"], stdout=StringIO(), stderr=stderr
294 )
•295 if self.msgfmt_version < (0, 25):
296 error_msg = "' cannot start a field name"
297 else:
298 error_msg = (
299 "a field name starts with a character that is not alphanumerical "
300 "or underscore"
301 )
302 self.assertIn(error_msg, stderr.getvalue())
303
304
305 class ProjectAndAppTests(MessageCompilationTests):
306 LOCALE = "ru"
307 PROJECT_MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
308 APP_MO_FILE = "app_with_locale/locale/%s/LC_MESSAGES/django.mo" % LOCALE
309
310
311 class FuzzyTranslationTest(ProjectAndAppTests):
312 def setUp(self):
313 super().setUp()
314 gettext_module._translations = {} # flush cache or test will be useless
315
316 def test_nofuzzy_compiling(self):
317 with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
318 call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
319 with translation.override(self.LOCALE):
320 self.assertEqual(gettext("Lenin"), "Ленин")
321 self.assertEqual(gettext("Vodka"), "Vodka")
322
323 def test_fuzzy_compiling(self):
324 with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
325 call_command(
326 "compilemessages", locale=[self.LOCALE], fuzzy=True, verbosity=0
327 )
328 with translation.override(self.LOCALE):
329 self.assertEqual(gettext("Lenin"), "Ленин")
330 self.assertEqual(gettext("Vodka"), "Водка")
331
332
333 class AppCompilationTest(ProjectAndAppTests):
334 def test_app_locale_compiled(self):
335 call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
336 self.assertTrue(os.path.exists(self.PROJECT_MO_FILE))
337 self.assertTrue(os.path.exists(self.APP_MO_FILE))
338
339
340 class PathLibLocaleCompilationTests(MessageCompilationTests):
341 work_subdir = "exclude"
342
343 def test_locale_paths_pathlib(self):
344 with override_settings(LOCALE_PATHS=[Path(self.test_dir) / "canned_locale"]):
345 call_command("compilemessages", locale=["fr"], verbosity=0)
346 self.assertTrue(os.path.exists("canned_locale/fr/LC_MESSAGES/django.mo"))