summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authormichalpokusa <72110769+michalpokusa@users.noreply.github.com>2025-05-08 05:05:24 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-08-12 09:47:16 +0200
commit2c99fbcf95f07eaff0bf9673af7c82d07353df8f (patch)
treefb48119c4cba5b4cb3539fb68d5a64f6432e3819 /tests
parent80cc9994d178d4afff0c85f9be8393bcedfd2687 (diff)
Fixed #36368 -- Prevented duplicate locale paths and write_po_file calls in makemessages.
Diffstat (limited to 'tests')
-rw-r--r--tests/i18n/commands/app_with_locale/some_file.py0
-rw-r--r--tests/i18n/test_extraction.py78
2 files changed, 77 insertions, 1 deletions
diff --git a/tests/i18n/commands/app_with_locale/some_file.py b/tests/i18n/commands/app_with_locale/some_file.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/i18n/commands/app_with_locale/some_file.py
diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py
index b02b24ab5b..deac15ef8f 100644
--- a/tests/i18n/test_extraction.py
+++ b/tests/i18n/test_extraction.py
@@ -14,7 +14,10 @@ from django.core import management
from django.core.management import execute_from_command_line
from django.core.management.base import CommandError
from django.core.management.commands.makemessages import Command as MakeMessagesCommand
-from django.core.management.commands.makemessages import write_pot_file
+from django.core.management.commands.makemessages import (
+ TranslatableFile,
+ write_pot_file,
+)
from django.core.management.utils import find_command
from django.test import SimpleTestCase, override_settings
from django.test.utils import captured_stderr, captured_stdout
@@ -600,6 +603,79 @@ class BasicExtractorTests(ExtractorTests):
self.assertIn("Content-Type: text/plain; charset=UTF-8", pot_contents)
self.assertIn("maƱana; charset=CHARSET", pot_contents)
+ def test_no_duplicate_locale_paths(self):
+ for locale_paths in [
+ [],
+ [os.path.join(self.test_dir, "locale")],
+ [Path(self.test_dir, "locale")],
+ ]:
+ with self.subTest(locale_paths=locale_paths):
+ with override_settings(LOCALE_PATHS=locale_paths):
+ cmd = MakeMessagesCommand()
+ management.call_command(cmd, locale=["en", "ru"], verbosity=0)
+ self.assertTrue(
+ all(isinstance(path, str) for path in cmd.locale_paths)
+ )
+ self.assertEqual(len(cmd.locale_paths), len(set(cmd.locale_paths)))
+
+ def test_no_duplicate_write_po_file_calls(self):
+ with mock.patch.object(
+ MakeMessagesCommand, "write_po_file"
+ ) as mock_write_po_file:
+ cmd = MakeMessagesCommand()
+ management.call_command(cmd, locale=["en", "ru"], verbosity=0)
+ self.assertEqual(
+ len(mock_write_po_file.call_args_list),
+ len({call.args for call in mock_write_po_file.call_args_list}),
+ )
+
+ def test_correct_translatable_file_locale_dir(self):
+
+ class ReturnTrackingMock(mock.Mock):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.call_return_value_list = []
+
+ def __call__(self, *args, **kwargs):
+ value = super().__call__(*args, **kwargs)
+ self.call_return_value_list.append(value)
+ return value
+
+ for locale_paths in [
+ [],
+ [
+ os.path.join(self.test_dir, "app_with_locale", "locale"),
+ ],
+ [
+ os.path.join(self.test_dir, "locale"),
+ os.path.join(self.test_dir, "app_with_locale", "locale"),
+ ],
+ ]:
+ with self.subTest(locale_paths=locale_paths):
+ with override_settings(LOCALE_PATHS=locale_paths):
+ cmd = MakeMessagesCommand()
+ rtm = ReturnTrackingMock(wraps=cmd.find_files)
+
+ with mock.patch.object(cmd, "find_files", new=rtm):
+ management.call_command(cmd, locale=["en", "ru"], verbosity=0)
+ self.assertEqual(len(rtm.call_args_list), 1)
+ self.assertEqual(len(rtm.call_return_value_list), 1)
+
+ for tf in rtm.call_return_value_list[0]:
+ self.assertIsInstance(tf, TranslatableFile)
+ abs_file_path = os.path.abspath(
+ os.path.join(self.test_dir, tf.dirpath, tf.file)
+ )
+ max_common_path = max(
+ [
+ os.path.commonpath([abs_file_path, locale_path])
+ for locale_path in cmd.locale_paths
+ ],
+ key=len,
+ )
+ correct_locale_dir = os.path.join(max_common_path, "locale")
+ self.assertEqual(tf.locale_dir, correct_locale_dir)
+
class JavaScriptExtractorTests(ExtractorTests):
PO_FILE = "locale/%s/LC_MESSAGES/djangojs.po" % LOCALE