2015-01-28 12:35:27 +00:00
|
|
|
import gettext as gettext_module
|
2010-10-10 16:38:28 +00:00
|
|
|
import os
|
2014-01-04 21:26:24 +00:00
|
|
|
import stat
|
2013-10-28 13:17:48 +00:00
|
|
|
import unittest
|
2017-01-07 11:11:46 +00:00
|
|
|
from io import StringIO
|
2019-01-27 19:35:17 +00:00
|
|
|
from pathlib import Path
|
2019-08-23 08:53:36 +00:00
|
|
|
from subprocess import run
|
2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.core.management import CommandError, call_command, execute_from_command_line
|
2013-10-28 13:17:48 +00:00
|
|
|
from django.core.management.utils import find_command
|
2017-01-19 17:16:04 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2014-11-28 22:47:53 +00:00
|
|
|
from django.test.utils import captured_stderr, captured_stdout
|
2016-12-01 10:38:01 +00:00
|
|
|
from django.utils import translation
|
2017-01-26 19:58:33 +00:00
|
|
|
from django.utils.translation import gettext
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2016-06-09 13:08:31 +00:00
|
|
|
from .utils import RunInTmpDirMixin, copytree
|
|
|
|
|
2013-10-28 13:17:48 +00:00
|
|
|
has_msgfmt = find_command("msgfmt")
|
2010-10-10 16:38:28 +00:00
|
|
|
|
2012-09-07 18:01:46 +00:00
|
|
|
|
2013-10-28 13:17:48 +00:00
|
|
|
@unittest.skipUnless(has_msgfmt, "msgfmt is mandatory for compilation tests")
|
2016-06-09 13:08:31 +00:00
|
|
|
class MessageCompilationTests(RunInTmpDirMixin, SimpleTestCase):
|
2016-05-29 14:25:05 +00:00
|
|
|
work_subdir = "commands"
|
2014-03-24 13:03:06 +00:00
|
|
|
|
2010-10-10 16:38:28 +00:00
|
|
|
|
|
|
|
class PoFileTests(MessageCompilationTests):
|
2012-09-07 18:01:46 +00:00
|
|
|
LOCALE = "es_AR"
|
|
|
|
MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
|
2020-06-19 08:25:42 +00:00
|
|
|
MO_FILE_EN = "locale/en/LC_MESSAGES/django.mo"
|
2011-12-11 00:07:06 +00:00
|
|
|
|
2010-10-10 16:38:28 +00:00
|
|
|
def test_bom_rejection(self):
|
2018-06-06 09:24:25 +00:00
|
|
|
stderr = StringIO()
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "compilemessages generated one or more errors."
|
|
|
|
):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command(
|
|
|
|
"compilemessages", locale=[self.LOCALE], verbosity=0, stderr=stderr
|
|
|
|
)
|
2018-06-06 09:24:25 +00:00
|
|
|
self.assertIn("file has a BOM (Byte Order Mark)", stderr.getvalue())
|
2010-12-04 07:28:12 +00:00
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE))
|
2011-12-11 00:07:06 +00:00
|
|
|
|
2014-01-04 21:26:24 +00:00
|
|
|
def test_no_write_access(self):
|
2020-06-19 08:25:42 +00:00
|
|
|
mo_file_en = Path(self.MO_FILE_EN)
|
2014-01-04 21:26:24 +00:00
|
|
|
err_buffer = StringIO()
|
2020-06-19 08:25:42 +00:00
|
|
|
# Put file in read-only mode.
|
|
|
|
old_mode = mo_file_en.stat().st_mode
|
|
|
|
mo_file_en.chmod(stat.S_IREAD)
|
2020-06-19 08:26:26 +00:00
|
|
|
# Ensure .po file is more recent than .mo file.
|
|
|
|
mo_file_en.with_suffix(".po").touch()
|
2014-01-04 21:26:24 +00:00
|
|
|
try:
|
2018-06-06 09:24:25 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "compilemessages generated one or more errors."
|
|
|
|
):
|
|
|
|
call_command(
|
|
|
|
"compilemessages", locale=["en"], stderr=err_buffer, verbosity=0
|
|
|
|
)
|
|
|
|
self.assertIn("not writable location", err_buffer.getvalue())
|
2014-01-04 21:26:24 +00:00
|
|
|
finally:
|
2020-06-19 08:25:42 +00:00
|
|
|
mo_file_en.chmod(old_mode)
|
2014-01-04 21:26:24 +00:00
|
|
|
|
2020-06-19 08:26:26 +00:00
|
|
|
def test_no_compile_when_unneeded(self):
|
|
|
|
mo_file_en = Path(self.MO_FILE_EN)
|
|
|
|
mo_file_en.touch()
|
|
|
|
stdout = StringIO()
|
|
|
|
call_command("compilemessages", locale=["en"], stdout=stdout, verbosity=1)
|
|
|
|
msg = "%s” is already compiled and up to date." % mo_file_en.with_suffix(".po")
|
|
|
|
self.assertIn(msg, stdout.getvalue())
|
|
|
|
|
2011-12-11 00:07:06 +00:00
|
|
|
|
|
|
|
class PoFileContentsTests(MessageCompilationTests):
|
|
|
|
# Ticket #11240
|
|
|
|
|
2013-10-23 10:09:29 +00:00
|
|
|
LOCALE = "fr"
|
|
|
|
MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
|
2011-12-11 00:07:06 +00:00
|
|
|
|
|
|
|
def test_percent_symbol_in_po_file(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
|
2011-12-11 00:07:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE))
|
|
|
|
|
|
|
|
|
2012-06-07 09:23:25 +00:00
|
|
|
class MultipleLocaleCompilationTests(MessageCompilationTests):
|
|
|
|
MO_FILE_HR = None
|
|
|
|
MO_FILE_FR = None
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2014-03-24 13:03:06 +00:00
|
|
|
localedir = os.path.join(self.test_dir, "locale")
|
2013-02-27 01:27:35 +00:00
|
|
|
self.MO_FILE_HR = os.path.join(localedir, "hr/LC_MESSAGES/django.mo")
|
|
|
|
self.MO_FILE_FR = os.path.join(localedir, "fr/LC_MESSAGES/django.mo")
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
def test_one_locale(self):
|
2015-01-21 16:55:57 +00:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=["hr"], verbosity=0)
|
2012-06-07 09:23:25 +00:00
|
|
|
|
2014-03-24 13:03:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
2012-06-07 09:23:25 +00:00
|
|
|
|
|
|
|
def test_multiple_locales(self):
|
2015-01-21 16:55:57 +00:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=["hr", "fr"], verbosity=0)
|
2014-03-24 13:03:06 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_HR))
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE_FR))
|
|
|
|
|
|
|
|
|
|
|
|
class ExcludedLocaleCompilationTests(MessageCompilationTests):
|
2016-05-29 14:25:05 +00:00
|
|
|
work_subdir = "exclude"
|
2014-03-24 13:03:06 +00:00
|
|
|
|
|
|
|
MO_FILE = "locale/%s/LC_MESSAGES/django.mo"
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2016-06-09 13:08:31 +00:00
|
|
|
copytree("canned_locale", "locale")
|
2014-03-24 13:03:06 +00:00
|
|
|
|
2014-05-01 07:03:24 +00:00
|
|
|
def test_command_help(self):
|
2014-11-28 22:47:53 +00:00
|
|
|
with captured_stdout(), captured_stderr():
|
2014-05-01 07:03:24 +00:00
|
|
|
# `call_command` bypasses the parser; by calling
|
|
|
|
# `execute_from_command_line` with the help subcommand we
|
|
|
|
# ensure that there are no issues with the parser itself.
|
|
|
|
execute_from_command_line(["django-admin", "help", "compilemessages"])
|
|
|
|
|
2014-03-24 13:03:06 +00:00
|
|
|
def test_one_locale_excluded(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", exclude=["it"], verbosity=0)
|
2014-03-24 13:03:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % "en"))
|
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % "fr"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "it"))
|
|
|
|
|
|
|
|
def test_multiple_locales_excluded(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", exclude=["it", "fr"], verbosity=0)
|
2014-03-24 13:03:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % "en"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "it"))
|
|
|
|
|
|
|
|
def test_one_locale_excluded_with_locale(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command(
|
|
|
|
"compilemessages", locale=["en", "fr"], exclude=["fr"], verbosity=0
|
|
|
|
)
|
2014-03-24 13:03:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % "en"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "it"))
|
2012-06-07 09:23:25 +00:00
|
|
|
|
2014-03-24 13:03:06 +00:00
|
|
|
def test_multiple_locales_excluded_with_locale(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command(
|
|
|
|
"compilemessages",
|
|
|
|
locale=["en", "fr", "it"],
|
|
|
|
exclude=["fr", "it"],
|
|
|
|
verbosity=0,
|
|
|
|
)
|
2014-03-24 13:03:06 +00:00
|
|
|
self.assertTrue(os.path.exists(self.MO_FILE % "en"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "fr"))
|
|
|
|
self.assertFalse(os.path.exists(self.MO_FILE % "it"))
|
2013-02-12 16:58:49 +00:00
|
|
|
|
|
|
|
|
2019-01-27 19:35:17 +00:00
|
|
|
class IgnoreDirectoryCompilationTests(MessageCompilationTests):
|
|
|
|
# Reuse the exclude directory since it contains some locale fixtures.
|
|
|
|
work_subdir = "exclude"
|
|
|
|
MO_FILE = "%s/%s/LC_MESSAGES/django.mo"
|
|
|
|
CACHE_DIR = Path("cache") / "locale"
|
|
|
|
NESTED_DIR = Path("outdated") / "v1" / "locale"
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
|
|
|
copytree("canned_locale", "locale")
|
|
|
|
copytree("canned_locale", self.CACHE_DIR)
|
|
|
|
copytree("canned_locale", self.NESTED_DIR)
|
|
|
|
|
|
|
|
def assertAllExist(self, dir, langs):
|
|
|
|
self.assertTrue(
|
|
|
|
all(Path(self.MO_FILE % (dir, lang)).exists() for lang in langs)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-01-27 19:35:17 +00:00
|
|
|
|
|
|
|
def assertNoneExist(self, dir, langs):
|
|
|
|
self.assertTrue(
|
|
|
|
all(Path(self.MO_FILE % (dir, lang)).exists() is False for lang in langs)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-01-27 19:35:17 +00:00
|
|
|
|
|
|
|
def test_one_locale_dir_ignored(self):
|
|
|
|
call_command("compilemessages", ignore=["cache"], verbosity=0)
|
|
|
|
self.assertAllExist("locale", ["en", "fr", "it"])
|
|
|
|
self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
|
|
|
|
self.assertAllExist(self.NESTED_DIR, ["en", "fr", "it"])
|
|
|
|
|
|
|
|
def test_multiple_locale_dirs_ignored(self):
|
|
|
|
call_command(
|
|
|
|
"compilemessages", ignore=["cache/locale", "outdated"], verbosity=0
|
|
|
|
)
|
|
|
|
self.assertAllExist("locale", ["en", "fr", "it"])
|
|
|
|
self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
|
|
|
|
self.assertNoneExist(self.NESTED_DIR, ["en", "fr", "it"])
|
|
|
|
|
|
|
|
def test_ignores_based_on_pattern(self):
|
|
|
|
call_command("compilemessages", ignore=["*/locale"], verbosity=0)
|
|
|
|
self.assertAllExist("locale", ["en", "fr", "it"])
|
|
|
|
self.assertNoneExist(self.CACHE_DIR, ["en", "fr", "it"])
|
|
|
|
self.assertNoneExist(self.NESTED_DIR, ["en", "fr", "it"])
|
|
|
|
|
2023-11-07 11:32:19 +00:00
|
|
|
def test_no_dirs_accidentally_skipped(self):
|
|
|
|
os_walk_results = [
|
|
|
|
# To discover .po filepaths, compilemessages uses with a starting list of
|
|
|
|
# basedirs to inspect, which in this scenario are:
|
|
|
|
# ["conf/locale", "locale"]
|
|
|
|
# Then os.walk is used to discover other locale dirs, ignoring dirs matching
|
|
|
|
# `ignore_patterns`. Mock the results to place an ignored directory directly
|
|
|
|
# before and after a directory named "locale".
|
|
|
|
[("somedir", ["ignore", "locale", "ignore"], [])],
|
|
|
|
# This will result in three basedirs discovered:
|
|
|
|
# ["conf/locale", "locale", "somedir/locale"]
|
|
|
|
# os.walk is called for each locale in each basedir looking for .po files.
|
|
|
|
# In this scenario, we need to mock os.walk results for "en", "fr", and "it"
|
|
|
|
# locales for each basedir:
|
|
|
|
[("exclude/locale/LC_MESSAGES", [], ["en.po"])],
|
|
|
|
[("exclude/locale/LC_MESSAGES", [], ["fr.po"])],
|
|
|
|
[("exclude/locale/LC_MESSAGES", [], ["it.po"])],
|
|
|
|
[("exclude/conf/locale/LC_MESSAGES", [], ["en.po"])],
|
|
|
|
[("exclude/conf/locale/LC_MESSAGES", [], ["fr.po"])],
|
|
|
|
[("exclude/conf/locale/LC_MESSAGES", [], ["it.po"])],
|
|
|
|
[("exclude/somedir/locale/LC_MESSAGES", [], ["en.po"])],
|
|
|
|
[("exclude/somedir/locale/LC_MESSAGES", [], ["fr.po"])],
|
|
|
|
[("exclude/somedir/locale/LC_MESSAGES", [], ["it.po"])],
|
|
|
|
]
|
|
|
|
|
|
|
|
module_path = "django.core.management.commands.compilemessages"
|
|
|
|
with mock.patch(f"{module_path}.os.walk", side_effect=os_walk_results):
|
|
|
|
with mock.patch(f"{module_path}.os.path.isdir", return_value=True):
|
|
|
|
with mock.patch(
|
|
|
|
f"{module_path}.Command.compile_messages"
|
|
|
|
) as mock_compile_messages:
|
|
|
|
call_command("compilemessages", ignore=["ignore"], verbosity=4)
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
(
|
|
|
|
[
|
|
|
|
("exclude/locale/LC_MESSAGES", "en.po"),
|
|
|
|
("exclude/locale/LC_MESSAGES", "fr.po"),
|
|
|
|
("exclude/locale/LC_MESSAGES", "it.po"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
("exclude/conf/locale/LC_MESSAGES", "en.po"),
|
|
|
|
("exclude/conf/locale/LC_MESSAGES", "fr.po"),
|
|
|
|
("exclude/conf/locale/LC_MESSAGES", "it.po"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[
|
|
|
|
("exclude/somedir/locale/LC_MESSAGES", "en.po"),
|
|
|
|
("exclude/somedir/locale/LC_MESSAGES", "fr.po"),
|
|
|
|
("exclude/somedir/locale/LC_MESSAGES", "it.po"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual([c.args for c in mock_compile_messages.mock_calls], expected)
|
|
|
|
|
2019-01-27 19:35:17 +00:00
|
|
|
|
2013-02-12 16:58:49 +00:00
|
|
|
class CompilationErrorHandling(MessageCompilationTests):
|
|
|
|
def test_error_reported_by_msgfmt(self):
|
2015-11-04 20:50:16 +00:00
|
|
|
# po file contains wrong po formatting.
|
2013-02-12 16:58:49 +00:00
|
|
|
with self.assertRaises(CommandError):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=["ja"], verbosity=0)
|
2015-11-04 20:50:16 +00:00
|
|
|
|
|
|
|
def test_msgfmt_error_including_non_ascii(self):
|
|
|
|
# po file contains invalid msgstr content (triggers non-ascii error content).
|
2016-02-20 08:36:01 +00:00
|
|
|
# Make sure the output of msgfmt is unaffected by the current locale.
|
|
|
|
env = os.environ.copy()
|
2021-05-20 18:31:44 +00:00
|
|
|
env.update({"LC_ALL": "C"})
|
2019-08-23 08:53:36 +00:00
|
|
|
with mock.patch(
|
|
|
|
"django.core.management.utils.run",
|
|
|
|
lambda *args, **kwargs: run(*args, env=env, **kwargs),
|
|
|
|
):
|
2018-06-06 09:24:25 +00:00
|
|
|
stderr = StringIO()
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
CommandError, "compilemessages generated one or more errors"
|
|
|
|
):
|
|
|
|
call_command(
|
|
|
|
"compilemessages", locale=["ko"], stdout=StringIO(), stderr=stderr
|
|
|
|
)
|
|
|
|
self.assertIn("' cannot start a field name", stderr.getvalue())
|
2014-11-15 22:56:37 +00:00
|
|
|
|
|
|
|
|
2015-04-16 11:55:37 +00:00
|
|
|
class ProjectAndAppTests(MessageCompilationTests):
|
2014-11-15 22:56:37 +00:00
|
|
|
LOCALE = "ru"
|
2015-04-16 11:55:37 +00:00
|
|
|
PROJECT_MO_FILE = "locale/%s/LC_MESSAGES/django.mo" % LOCALE
|
|
|
|
APP_MO_FILE = "app_with_locale/locale/%s/LC_MESSAGES/django.mo" % LOCALE
|
|
|
|
|
|
|
|
|
|
|
|
class FuzzyTranslationTest(ProjectAndAppTests):
|
2014-11-15 22:56:37 +00:00
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2014-11-15 22:56:37 +00:00
|
|
|
gettext_module._translations = {} # flush cache or test will be useless
|
|
|
|
|
|
|
|
def test_nofuzzy_compiling(self):
|
2015-01-21 16:55:57 +00:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
|
2014-11-15 22:56:37 +00:00
|
|
|
with translation.override(self.LOCALE):
|
2017-01-26 19:58:33 +00:00
|
|
|
self.assertEqual(gettext("Lenin"), "Ленин")
|
|
|
|
self.assertEqual(gettext("Vodka"), "Vodka")
|
2014-11-15 22:56:37 +00:00
|
|
|
|
|
|
|
def test_fuzzy_compiling(self):
|
2015-01-21 16:55:57 +00:00
|
|
|
with override_settings(LOCALE_PATHS=[os.path.join(self.test_dir, "locale")]):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command(
|
|
|
|
"compilemessages", locale=[self.LOCALE], fuzzy=True, verbosity=0
|
|
|
|
)
|
2014-11-15 22:56:37 +00:00
|
|
|
with translation.override(self.LOCALE):
|
2017-01-26 19:58:33 +00:00
|
|
|
self.assertEqual(gettext("Lenin"), "Ленин")
|
|
|
|
self.assertEqual(gettext("Vodka"), "Водка")
|
2015-04-16 11:55:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AppCompilationTest(ProjectAndAppTests):
|
|
|
|
def test_app_locale_compiled(self):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=[self.LOCALE], verbosity=0)
|
2015-04-16 11:55:37 +00:00
|
|
|
self.assertTrue(os.path.exists(self.PROJECT_MO_FILE))
|
|
|
|
self.assertTrue(os.path.exists(self.APP_MO_FILE))
|
2019-11-07 09:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PathLibLocaleCompilationTests(MessageCompilationTests):
|
|
|
|
work_subdir = "exclude"
|
|
|
|
|
|
|
|
def test_locale_paths_pathlib(self):
|
|
|
|
with override_settings(LOCALE_PATHS=[Path(self.test_dir) / "canned_locale"]):
|
2020-04-18 10:27:43 +00:00
|
|
|
call_command("compilemessages", locale=["fr"], verbosity=0)
|
2019-11-07 09:26:22 +00:00
|
|
|
self.assertTrue(os.path.exists("canned_locale/fr/LC_MESSAGES/django.mo"))
|