|
14 | 14 | from django.core.management import execute_from_command_line |
15 | 15 | from django.core.management.base import CommandError |
16 | 16 | from django.core.management.commands.makemessages import Command as MakeMessagesCommand |
17 | | -from django.core.management.commands.makemessages import write_pot_file |
| 17 | +from django.core.management.commands.makemessages import ( |
| 18 | + TranslatableFile, |
| 19 | + write_pot_file, |
| 20 | +) |
18 | 21 | from django.core.management.utils import find_command |
19 | 22 | from django.test import SimpleTestCase, override_settings |
20 | 23 | from django.test.utils import captured_stderr, captured_stdout |
@@ -600,6 +603,79 @@ def test_pot_charset_header_is_utf8(self): |
600 | 603 | self.assertIn("Content-Type: text/plain; charset=UTF-8", pot_contents) |
601 | 604 | self.assertIn("mañana; charset=CHARSET", pot_contents) |
602 | 605 |
|
| 606 | + def test_no_duplicate_locale_paths(self): |
| 607 | + for locale_paths in [ |
| 608 | + [], |
| 609 | + [os.path.join(self.test_dir, "locale")], |
| 610 | + [Path(self.test_dir, "locale")], |
| 611 | + ]: |
| 612 | + with self.subTest(locale_paths=locale_paths): |
| 613 | + with override_settings(LOCALE_PATHS=locale_paths): |
| 614 | + cmd = MakeMessagesCommand() |
| 615 | + management.call_command(cmd, locale=["en", "ru"], verbosity=0) |
| 616 | + self.assertTrue( |
| 617 | + all(isinstance(path, str) for path in cmd.locale_paths) |
| 618 | + ) |
| 619 | + self.assertEqual(len(cmd.locale_paths), len(set(cmd.locale_paths))) |
| 620 | + |
| 621 | + def test_no_duplicate_write_po_file_calls(self): |
| 622 | + with mock.patch.object( |
| 623 | + MakeMessagesCommand, "write_po_file" |
| 624 | + ) as mock_write_po_file: |
| 625 | + cmd = MakeMessagesCommand() |
| 626 | + management.call_command(cmd, locale=["en", "ru"], verbosity=0) |
| 627 | + self.assertEqual( |
| 628 | + len(mock_write_po_file.call_args_list), |
| 629 | + len({call.args for call in mock_write_po_file.call_args_list}), |
| 630 | + ) |
| 631 | + |
| 632 | + def test_correct_translatable_file_locale_dir(self): |
| 633 | + |
| 634 | + class ReturnTrackingMock(mock.Mock): |
| 635 | + def __init__(self, *args, **kwargs): |
| 636 | + super().__init__(*args, **kwargs) |
| 637 | + self.call_return_value_list = [] |
| 638 | + |
| 639 | + def __call__(self, *args, **kwargs): |
| 640 | + value = super().__call__(*args, **kwargs) |
| 641 | + self.call_return_value_list.append(value) |
| 642 | + return value |
| 643 | + |
| 644 | + for locale_paths in [ |
| 645 | + [], |
| 646 | + [ |
| 647 | + os.path.join(self.test_dir, "app_with_locale", "locale"), |
| 648 | + ], |
| 649 | + [ |
| 650 | + os.path.join(self.test_dir, "locale"), |
| 651 | + os.path.join(self.test_dir, "app_with_locale", "locale"), |
| 652 | + ], |
| 653 | + ]: |
| 654 | + with self.subTest(locale_paths=locale_paths): |
| 655 | + with override_settings(LOCALE_PATHS=locale_paths): |
| 656 | + cmd = MakeMessagesCommand() |
| 657 | + rtm = ReturnTrackingMock(wraps=cmd.find_files) |
| 658 | + |
| 659 | + with mock.patch.object(cmd, "find_files", new=rtm): |
| 660 | + management.call_command(cmd, locale=["en", "ru"], verbosity=0) |
| 661 | + self.assertEqual(len(rtm.call_args_list), 1) |
| 662 | + self.assertEqual(len(rtm.call_return_value_list), 1) |
| 663 | + |
| 664 | + for tf in rtm.call_return_value_list[0]: |
| 665 | + self.assertIsInstance(tf, TranslatableFile) |
| 666 | + abs_file_path = os.path.abspath( |
| 667 | + os.path.join(self.test_dir, tf.dirpath, tf.file) |
| 668 | + ) |
| 669 | + max_common_path = max( |
| 670 | + [ |
| 671 | + os.path.commonpath([abs_file_path, locale_path]) |
| 672 | + for locale_path in cmd.locale_paths |
| 673 | + ], |
| 674 | + key=len, |
| 675 | + ) |
| 676 | + correct_locale_dir = os.path.join(max_common_path, "locale") |
| 677 | + self.assertEqual(tf.locale_dir, correct_locale_dir) |
| 678 | + |
603 | 679 |
|
604 | 680 | class JavaScriptExtractorTests(ExtractorTests): |
605 | 681 | PO_FILE = "locale/%s/LC_MESSAGES/djangojs.po" % LOCALE |
|
0 commit comments