Beispiel #1
0
    def test_makemessages_gettext_version(self, mocked_popen_wrapper):
        # "Normal" output:
        mocked_popen_wrapper.return_value = (
            "xgettext (GNU gettext-tools) 0.18.1\n"
            "Copyright (C) 1995-1998, 2000-2010 Free Software Foundation, Inc.\n"
            "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
            "This is free software: you are free to change and redistribute it.\n"
            "There is NO WARRANTY, to the extent permitted by law.\n"
            "Written by Ulrich Drepper.\n", '', 0)
        cmd = MakeMessagesCommand()
        self.assertEqual(cmd.gettext_version, (0, 18, 1))

        # Version number with only 2 parts (#23788)
        mocked_popen_wrapper.return_value = (
            "xgettext (GNU gettext-tools) 0.17\n", '', 0)
        cmd = MakeMessagesCommand()
        self.assertEqual(cmd.gettext_version, (0, 17))

        # Bad version output
        mocked_popen_wrapper.return_value = ("any other return value\n", '', 0)
        cmd = MakeMessagesCommand()
        with self.assertRaisesMessage(
                CommandError,
                "Unable to get gettext version. Is it installed?"):
            cmd.gettext_version
Beispiel #2
0
 def test_msgfmt_error_including_non_ascii(self):
     # po file contains invalid msgstr content (triggers non-ascii error content).
     mo_file = 'locale/ko/LC_MESSAGES/django.mo'
     self.addCleanup(self.rmfile, os.path.join(self.test_dir, mo_file))
     # Make sure the output of msgfmt is unaffected by the current locale.
     env = os.environ.copy()
     env.update({str('LANG'): str('C')})
     with mock.patch(
             'django.core.management.utils.Popen',
             lambda *args, **kwargs: Popen(*args, env=env, **kwargs)):
         if six.PY2:
             # Various assertRaises on PY2 don't support unicode error messages.
             try:
                 call_command('compilemessages', locale=['ko'], verbosity=0)
             except CommandError as err:
                 self.assertIn("' cannot start a field name",
                               six.text_type(err))
         else:
             cmd = MakeMessagesCommand()
             if cmd.gettext_version < (0, 18, 3):
                 raise unittest.SkipTest(
                     "python-brace-format is a recent gettext addition.")
             with self.assertRaisesMessage(CommandError,
                                           "' cannot start a field name"):
                 call_command('compilemessages', locale=['ko'], verbosity=0)
Beispiel #3
0
 def test_msgfmt_error_including_non_ascii(self):
     # po file contains invalid msgstr content (triggers non-ascii error content).
     # Make sure the output of msgfmt is unaffected by the current locale.
     env = os.environ.copy()
     env.update({'LANG': 'C'})
     with mock.patch('django.core.management.utils.Popen', lambda *args, **kwargs: Popen(*args, env=env, **kwargs)):
         cmd = MakeMessagesCommand()
         if cmd.gettext_version < (0, 18, 3):
             self.skipTest("python-brace-format is a recent gettext addition.")
         with self.assertRaisesMessage(CommandError, "' cannot start a field name"):
             call_command('compilemessages', locale=['ko'], verbosity=0)
Beispiel #4
0
 def test_msgfmt_error_including_non_ascii(self):
     # po file contains invalid msgstr content (triggers non-ascii error content).
     mo_file = 'locale/ko/LC_MESSAGES/django.mo'
     self.addCleanup(self.rmfile, os.path.join(self.test_dir, mo_file))
     if six.PY2:
         # Various assertRaises on PY2 don't support unicode error messages.
         try:
             call_command('compilemessages', locale=['ko'], verbosity=0)
         except CommandError as err:
             self.assertIn("'�' cannot start a field name",
                           six.text_type(err))
     else:
         cmd = MakeMessagesCommand()
         if cmd.gettext_version < (0, 18, 3):
             raise unittest.SkipTest(
                 "python-brace-format is a recent gettext addition.")
         with self.assertRaisesMessage(CommandError,
                                       "'�' cannot start a field name"):
             call_command('compilemessages', locale=['ko'], verbosity=0)
Beispiel #5
0
 def test_msgfmt_error_including_non_ascii(self):
     # po file contains invalid msgstr content (triggers non-ascii error content).
     # Make sure the output of msgfmt is unaffected by the current locale.
     env = os.environ.copy()
     env.update({"LC_ALL": "C"})
     with mock.patch(
         "django.core.management.utils.run",
         lambda *args, **kwargs: run(*args, env=env, **kwargs),
     ):
         cmd = MakeMessagesCommand()
         if cmd.gettext_version < (0, 18, 3):
             self.skipTest("python-brace-format is a recent gettext addition.")
         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())
Beispiel #6
0
    def test_makemessages_find_files(self):
        """
        Test that find_files only discover files having the proper extensions.
        """
        cmd = MakeMessagesCommand()
        cmd.ignore_patterns = ['CVS', '.*', '*~', '*.pyc']
        cmd.symlinks = False
        cmd.domain = 'django'
        cmd.extensions = ['html', 'txt', 'py']
        cmd.verbosity = 0
        cmd.locale_paths = []
        cmd.default_locale_path = os.path.join(self.test_dir, 'locale')
        found_files = cmd.find_files(self.test_dir)
        found_exts = set([os.path.splitext(tfile.file)[1] for tfile in found_files])
        self.assertEqual(found_exts.difference({'.py', '.html', '.txt'}), set())

        cmd.extensions = ['js']
        cmd.domain = 'djangojs'
        found_files = cmd.find_files(self.test_dir)
        found_exts = set([os.path.splitext(tfile.file)[1] for tfile in found_files])
        self.assertEqual(found_exts.difference({'.js'}), set())
Beispiel #7
0
    def test_makemessages_find_files(self):
        """
        find_files only discover files having the proper extensions.
        """
        cmd = MakeMessagesCommand()
        cmd.ignore_patterns = ["CVS", ".*", "*~", "*.pyc"]
        cmd.symlinks = False
        cmd.domain = "django"
        cmd.extensions = ["html", "txt", "py"]
        cmd.verbosity = 0
        cmd.locale_paths = []
        cmd.default_locale_path = os.path.join(self.test_dir, "locale")
        found_files = cmd.find_files(self.test_dir)
        found_exts = {os.path.splitext(tfile.file)[1] for tfile in found_files}
        self.assertEqual(found_exts.difference({".py", ".html", ".txt"}), set())

        cmd.extensions = ["js"]
        cmd.domain = "djangojs"
        found_files = cmd.find_files(self.test_dir)
        found_exts = {os.path.splitext(tfile.file)[1] for tfile in found_files}
        self.assertEqual(found_exts.difference({".js"}), set())
Beispiel #8
0
from django.core.management.base import CommandError
from django.core.management.commands.makemessages import (
    Command as MakeMessagesCommand,
    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
from django.utils._os import symlinks_supported
from django.utils.translation import TranslatorCommentWarning

from .utils import POFileAssertionMixin, RunInTmpDirMixin, copytree

LOCALE = 'de'
has_xgettext = find_command('xgettext')
gettext_version = MakeMessagesCommand(
).gettext_version if has_xgettext else None
requires_gettext_019 = skipIf(has_xgettext and gettext_version < (0, 19),
                              'gettext 0.19 required')


@skipUnless(has_xgettext, 'xgettext is mandatory for extraction tests')
class ExtractorTests(POFileAssertionMixin, RunInTmpDirMixin, SimpleTestCase):

    work_subdir = 'commands'

    PO_FILE = 'locale/%s/LC_MESSAGES/django.po' % LOCALE

    def _run_makemessages(self, **options):
        out = StringIO()
        management.call_command('makemessages',
                                locale=[LOCALE],