summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAna Krivokapic <akrivokapic1@gmail.com>2014-03-24 14:03:06 +0100
committerLoic Bistuer <loic.bistuer@gmail.com>2014-04-26 17:07:44 +0700
commit0707b824fe77e08ca8b75fcc3738ada694f2a3a6 (patch)
tree31dab52c85ab79a5de469ac258f450c76fe76c3a /tests
parentd1f93e9c1e2c61bb06a6f99c80bd9437c5081f2d (diff)
Fixed #22328 -- Added --exclude option to compilemessages and makemessages.
Diffstat (limited to 'tests')
-rw-r--r--tests/i18n/exclude/__init__.py12
-rw-r--r--tests/i18n/exclude/canned_locale/en/LC_MESSAGES/django.po27
-rw-r--r--tests/i18n/exclude/canned_locale/fr/LC_MESSAGES/django.po28
-rw-r--r--tests/i18n/exclude/canned_locale/it/LC_MESSAGES/django.po28
-rw-r--r--tests/i18n/test_compilation.py91
-rw-r--r--tests/i18n/test_extraction.py69
6 files changed, 231 insertions, 24 deletions
diff --git a/tests/i18n/exclude/__init__.py b/tests/i18n/exclude/__init__.py
new file mode 100644
index 0000000000..ff4b2bb2ad
--- /dev/null
+++ b/tests/i18n/exclude/__init__.py
@@ -0,0 +1,12 @@
+# This package is used to test the --exclude option of
+# the makemessages and compilemessages management commands.
+# The locale directory for this app is generated automatically
+# by the test cases.
+
+from django.utils.translation import ugettext as _
+
+# Translators: This comment should be extracted
+dummy1 = _("This is a translatable string.")
+
+# This comment should not be extracted
+dummy2 = _("This is another translatable string.")
diff --git a/tests/i18n/exclude/canned_locale/en/LC_MESSAGES/django.po b/tests/i18n/exclude/canned_locale/en/LC_MESSAGES/django.po
new file mode 100644
index 0000000000..7ca97afcc1
--- /dev/null
+++ b/tests/i18n/exclude/canned_locale/en/LC_MESSAGES/django.po
@@ -0,0 +1,27 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2014-04-25 15:39-0500\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+#. Translators: This comment should be extracted
+#: __init__.py:8
+msgid "This is a translatable string."
+msgstr ""
+
+#: __init__.py:11
+msgid "This is another translatable string."
+msgstr ""
diff --git a/tests/i18n/exclude/canned_locale/fr/LC_MESSAGES/django.po b/tests/i18n/exclude/canned_locale/fr/LC_MESSAGES/django.po
new file mode 100644
index 0000000000..94d1f5d2dd
--- /dev/null
+++ b/tests/i18n/exclude/canned_locale/fr/LC_MESSAGES/django.po
@@ -0,0 +1,28 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2014-04-25 15:39-0500\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n > 1);\n"
+
+#. Translators: This comment should be extracted
+#: __init__.py:8
+msgid "This is a translatable string."
+msgstr ""
+
+#: __init__.py:11
+msgid "This is another translatable string."
+msgstr ""
diff --git a/tests/i18n/exclude/canned_locale/it/LC_MESSAGES/django.po b/tests/i18n/exclude/canned_locale/it/LC_MESSAGES/django.po
new file mode 100644
index 0000000000..53f492bb8a
--- /dev/null
+++ b/tests/i18n/exclude/canned_locale/it/LC_MESSAGES/django.po
@@ -0,0 +1,28 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2014-04-25 15:39-0500\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
+
+#. Translators: This comment should be extracted
+#: __init__.py:8
+msgid "This is a translatable string."
+msgstr ""
+
+#: __init__.py:11
+msgid "This is another translatable string."
+msgstr ""
diff --git a/tests/i18n/test_compilation.py b/tests/i18n/test_compilation.py
index 463ff21d23..325c597aa9 100644
--- a/tests/i18n/test_compilation.py
+++ b/tests/i18n/test_compilation.py
@@ -1,4 +1,5 @@
import os
+import shutil
import stat
import unittest
@@ -10,17 +11,23 @@ from django.utils import translation
from django.utils._os import upath
from django.utils.six import StringIO
-test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
has_msgfmt = find_command('msgfmt')
@unittest.skipUnless(has_msgfmt, 'msgfmt is mandatory for compilation tests')
class MessageCompilationTests(SimpleTestCase):
+ test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
+
def setUp(self):
self._cwd = os.getcwd()
self.addCleanup(os.chdir, self._cwd)
- os.chdir(test_dir)
+ os.chdir(self.test_dir)
+
+ def _rmrf(self, dname):
+ if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
+ return
+ shutil.rmtree(dname)
def rmfile(self, filepath):
if os.path.exists(filepath):
@@ -60,7 +67,7 @@ class PoFileContentsTests(MessageCompilationTests):
def setUp(self):
super(PoFileContentsTests, self).setUp()
- self.addCleanup(os.unlink, os.path.join(test_dir, self.MO_FILE))
+ self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
def test_percent_symbol_in_po_file(self):
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
@@ -76,45 +83,85 @@ class PercentRenderingTests(MessageCompilationTests):
def setUp(self):
super(PercentRenderingTests, self).setUp()
- self.addCleanup(os.unlink, os.path.join(test_dir, self.MO_FILE))
+ self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
- @override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
def test_percent_symbol_escaping(self):
- from django.template import Template, Context
- call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
- with translation.override(self.LOCALE):
- t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')
- rendered = t.render(Context({}))
- self.assertEqual(rendered, 'IT translation contains %% for the above string')
+ with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
+ from django.template import Template, Context
+ call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
+ with translation.override(self.LOCALE):
+ t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')
+ rendered = t.render(Context({}))
+ self.assertEqual(rendered, 'IT translation contains %% for the above string')
- t = Template('{% load i18n %}{% trans "Completed 50%% of all the tasks" %}')
- rendered = t.render(Context({}))
- self.assertEqual(rendered, 'IT translation of Completed 50%% of all the tasks')
+ t = Template('{% load i18n %}{% trans "Completed 50%% of all the tasks" %}')
+ rendered = t.render(Context({}))
+ self.assertEqual(rendered, 'IT translation of Completed 50%% of all the tasks')
-@override_settings(LOCALE_PATHS=(os.path.join(test_dir, 'locale'),))
class MultipleLocaleCompilationTests(MessageCompilationTests):
+
MO_FILE_HR = None
MO_FILE_FR = None
def setUp(self):
super(MultipleLocaleCompilationTests, self).setUp()
- localedir = os.path.join(test_dir, 'locale')
+ localedir = os.path.join(self.test_dir, 'locale')
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')
self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_HR))
self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_FR))
def test_one_locale(self):
- call_command('compilemessages', locale=['hr'], stdout=StringIO())
+ with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
+ call_command('compilemessages', locale=['hr'], stdout=StringIO())
- self.assertTrue(os.path.exists(self.MO_FILE_HR))
+ self.assertTrue(os.path.exists(self.MO_FILE_HR))
def test_multiple_locales(self):
- call_command('compilemessages', locale=['hr', 'fr'], stdout=StringIO())
+ with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
+ call_command('compilemessages', locale=['hr', 'fr'], stdout=StringIO())
+
+ self.assertTrue(os.path.exists(self.MO_FILE_HR))
+ self.assertTrue(os.path.exists(self.MO_FILE_FR))
+
+
+class ExcludedLocaleCompilationTests(MessageCompilationTests):
+
+ test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'exclude'))
+
+ MO_FILE = 'locale/%s/LC_MESSAGES/django.mo'
+
+ def setUp(self):
+ super(ExcludedLocaleCompilationTests, self).setUp()
+
+ shutil.copytree('canned_locale', 'locale')
+ self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale'))
+
+ def test_one_locale_excluded(self):
+ call_command('compilemessages', exclude=['it'], stdout=StringIO())
+ 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):
+ call_command('compilemessages', exclude=['it', 'fr'], stdout=StringIO())
+ 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):
+ call_command('compilemessages', locale=['en', 'fr'], exclude=['fr'], stdout=StringIO())
+ 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'))
- self.assertTrue(os.path.exists(self.MO_FILE_HR))
- self.assertTrue(os.path.exists(self.MO_FILE_FR))
+ def test_multiple_locales_excluded_with_locale(self):
+ call_command('compilemessages', locale=['en', 'fr', 'it'], exclude=['fr', 'it'],
+ stdout=StringIO())
+ 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'))
class CompilationErrorHandling(MessageCompilationTests):
@@ -124,7 +171,7 @@ class CompilationErrorHandling(MessageCompilationTests):
def setUp(self):
super(CompilationErrorHandling, self).setUp()
- self.addCleanup(self.rmfile, os.path.join(test_dir, self.MO_FILE))
+ self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
def test_error_reported_by_msgfmt(self):
with self.assertRaises(CommandError):
diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py
index f0a7b35fae..717ece6c9d 100644
--- a/tests/i18n/test_extraction.py
+++ b/tests/i18n/test_extraction.py
@@ -5,6 +5,7 @@ import io
import os
import re
import shutil
+import time
from unittest import SkipTest, skipUnless
import warnings
@@ -27,12 +28,12 @@ has_xgettext = find_command('xgettext')
@skipUnless(has_xgettext, 'xgettext is mandatory for extraction tests')
class ExtractorTests(SimpleTestCase):
+ test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
+
PO_FILE = 'locale/%s/LC_MESSAGES/django.po' % LOCALE
def setUp(self):
self._cwd = os.getcwd()
- self.test_dir = os.path.abspath(
- os.path.join(os.path.dirname(upath(__file__)), 'commands'))
def _rmrf(self, dname):
if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
@@ -103,6 +104,20 @@ class ExtractorTests(SimpleTestCase):
"""Check the opposite of assertLocationComment()"""
return self._assertPoLocComment(False, po_filename, line_number, *comment_parts)
+ def assertRecentlyModified(self, path):
+ """
+ Assert that file was recently modified (modification time was less than 10 seconds ago).
+ """
+ delta = time.time() - os.stat(path).st_mtime
+ self.assertLess(delta, 10, "%s was recently modified" % path)
+
+ def assertNotRecentlyModified(self, path):
+ """
+ Assert that file was not recently modified (modification time was more than 10 seconds ago).
+ """
+ delta = time.time() - os.stat(path).st_mtime
+ self.assertGreater(delta, 10, "%s wasn't recently modified" % path)
+
class BasicExtractorTests(ExtractorTests):
@@ -402,6 +417,7 @@ class SymlinkExtractorTests(ExtractorTests):
class CopyPluralFormsExtractorTests(ExtractorTests):
+
PO_FILE_ES = 'locale/es/LC_MESSAGES/django.po'
def tearDown(self):
@@ -527,7 +543,56 @@ class MultipleLocaleExtractionTests(ExtractorTests):
self.assertTrue(os.path.exists(self.PO_FILE_DE))
+class ExcludedLocaleExtractionTests(ExtractorTests):
+
+ LOCALES = ['en', 'fr', 'it']
+ PO_FILE = 'locale/%s/LC_MESSAGES/django.po'
+
+ test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'exclude'))
+
+ def _set_times_for_all_po_files(self):
+ """
+ Set access and modification times to the Unix epoch time for all the .po files.
+ """
+ for locale in self.LOCALES:
+ os.utime(self.PO_FILE % locale, (0, 0))
+
+ def setUp(self):
+ super(ExcludedLocaleExtractionTests, self).setUp()
+
+ os.chdir(self.test_dir) # ExtractorTests.tearDown() takes care of restoring.
+ shutil.copytree('canned_locale', 'locale')
+ self._set_times_for_all_po_files()
+ self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale'))
+
+ def test_one_locale_excluded(self):
+ management.call_command('makemessages', exclude=['it'], stdout=StringIO())
+ self.assertRecentlyModified(self.PO_FILE % 'en')
+ self.assertRecentlyModified(self.PO_FILE % 'fr')
+ self.assertNotRecentlyModified(self.PO_FILE % 'it')
+
+ def test_multiple_locales_excluded(self):
+ management.call_command('makemessages', exclude=['it', 'fr'], stdout=StringIO())
+ self.assertRecentlyModified(self.PO_FILE % 'en')
+ self.assertNotRecentlyModified(self.PO_FILE % 'fr')
+ self.assertNotRecentlyModified(self.PO_FILE % 'it')
+
+ def test_one_locale_excluded_with_locale(self):
+ management.call_command('makemessages', locale=['en', 'fr'], exclude=['fr'], stdout=StringIO())
+ self.assertRecentlyModified(self.PO_FILE % 'en')
+ self.assertNotRecentlyModified(self.PO_FILE % 'fr')
+ self.assertNotRecentlyModified(self.PO_FILE % 'it')
+
+ def test_multiple_locales_excluded_with_locale(self):
+ management.call_command('makemessages', locale=['en', 'fr', 'it'], exclude=['fr', 'it'],
+ stdout=StringIO())
+ self.assertRecentlyModified(self.PO_FILE % 'en')
+ self.assertNotRecentlyModified(self.PO_FILE % 'fr')
+ self.assertNotRecentlyModified(self.PO_FILE % 'it')
+
+
class CustomLayoutExtractionTests(ExtractorTests):
+
def setUp(self):
self._cwd = os.getcwd()
self.test_dir = os.path.join(os.path.dirname(upath(__file__)), 'project_dir')