diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index ba8de527bf9..932bc0897ee 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -310,9 +310,9 @@ class Command(BaseCommand): @cached_property def gettext_version(self): out, err, status = popen_wrapper(['xgettext', '--version']) - m = re.search(r'(\d)\.(\d+)\.(\d+)', out) + m = re.search(r'(\d)\.(\d+)\.?(\d+)?', out) if m: - return tuple(int(d) for d in m.groups()) + return tuple(int(d) for d in m.groups() if d is not None) else: raise CommandError("Unable to get gettext version. Is it installed?") diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index 705eca431ed..6f8fea1bfe5 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -12,9 +12,10 @@ import warnings from django.conf import settings 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.utils import find_command -from django.test import SimpleTestCase -from django.test import override_settings +from django.test import mock, override_settings, SimpleTestCase from django.test.utils import captured_stderr, captured_stdout from django.utils.encoding import force_text from django.utils._os import upath @@ -205,8 +206,7 @@ class BasicExtractorTests(ExtractorTests): def test_force_en_us_locale(self): """Value of locale-munging option used by the command is the right one""" - from django.core.management.commands.makemessages import Command - self.assertTrue(Command.leave_locale_alone) + self.assertTrue(MakeMessagesCommand.leave_locale_alone) def test_extraction_error(self): os.chdir(self.test_dir) @@ -347,8 +347,7 @@ class BasicExtractorTests(ExtractorTests): """ Test that find_files only discover files having the proper extensions. """ - from django.core.management.commands.makemessages import Command - cmd = Command() + cmd = MakeMessagesCommand() cmd.ignore_patterns = ['CVS', '.*', '*~', '*.pyc'] cmd.symlinks = False cmd.domain = 'django' @@ -366,6 +365,32 @@ class BasicExtractorTests(ExtractorTests): found_exts = set([os.path.splitext(tfile.file)[1] for tfile in found_files]) self.assertEqual(found_exts.difference({'.js'}), set()) + @mock.patch('django.core.management.commands.makemessages.popen_wrapper') + 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 \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 six.assertRaisesRegex(self, CommandError, "Unable to get gettext version. Is it installed?"): + cmd.gettext_version + class JavascriptExtractorTests(ExtractorTests):