mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #22328 -- Added --exclude option to compilemessages and makemessages.
This commit is contained in:
parent
d1f93e9c1e
commit
0707b824fe
12 changed files with 299 additions and 41 deletions
|
@ -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))
|
||||
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'))
|
||||
|
||||
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue