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