test_compilation.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import shutil
  4. import stat
  5. import sys
  6. import unittest
  7. import gettext as gettext_module
  8. from django.core.management import call_command, CommandError, execute_from_command_line
  9. from django.core.management.utils import find_command
  10. from django.test import SimpleTestCase
  11. from django.test import override_settings
  12. from django.utils import translation
  13. from django.utils.translation import ugettext
  14. from django.utils.encoding import force_text
  15. from django.utils._os import upath
  16. from django.utils.six import StringIO
  17. has_msgfmt = find_command('msgfmt')
  18. @unittest.skipUnless(has_msgfmt, 'msgfmt is mandatory for compilation tests')
  19. class MessageCompilationTests(SimpleTestCase):
  20. test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'commands'))
  21. def setUp(self):
  22. self._cwd = os.getcwd()
  23. self.addCleanup(os.chdir, self._cwd)
  24. os.chdir(self.test_dir)
  25. def _rmrf(self, dname):
  26. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  27. return
  28. shutil.rmtree(dname)
  29. def rmfile(self, filepath):
  30. if os.path.exists(filepath):
  31. os.remove(filepath)
  32. class PoFileTests(MessageCompilationTests):
  33. LOCALE = 'es_AR'
  34. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
  35. def test_bom_rejection(self):
  36. with self.assertRaises(CommandError) as cm:
  37. call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
  38. self.assertIn("file has a BOM (Byte Order Mark)", cm.exception.args[0])
  39. self.assertFalse(os.path.exists(self.MO_FILE))
  40. def test_no_write_access(self):
  41. mo_file_en = 'locale/en/LC_MESSAGES/django.mo'
  42. err_buffer = StringIO()
  43. # put file in read-only mode
  44. old_mode = os.stat(mo_file_en).st_mode
  45. os.chmod(mo_file_en, stat.S_IREAD)
  46. try:
  47. call_command('compilemessages', locale=['en'], stderr=err_buffer, verbosity=0)
  48. err = err_buffer.getvalue()
  49. self.assertIn("not writable location", err)
  50. finally:
  51. os.chmod(mo_file_en, old_mode)
  52. class PoFileContentsTests(MessageCompilationTests):
  53. # Ticket #11240
  54. LOCALE = 'fr'
  55. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
  56. def setUp(self):
  57. super(PoFileContentsTests, self).setUp()
  58. self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
  59. def test_percent_symbol_in_po_file(self):
  60. call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
  61. self.assertTrue(os.path.exists(self.MO_FILE))
  62. class PercentRenderingTests(MessageCompilationTests):
  63. # Ticket #11240 -- Testing rendering doesn't belong here but we are trying
  64. # to keep tests for all the stack together
  65. LOCALE = 'it'
  66. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
  67. def setUp(self):
  68. super(PercentRenderingTests, self).setUp()
  69. self.addCleanup(os.unlink, os.path.join(self.test_dir, self.MO_FILE))
  70. def test_percent_symbol_escaping(self):
  71. with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
  72. from django.template import Template, Context
  73. call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
  74. with translation.override(self.LOCALE):
  75. t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')
  76. rendered = t.render(Context({}))
  77. self.assertEqual(rendered, 'IT translation contains %% for the above string')
  78. t = Template('{% load i18n %}{% trans "Completed 50%% of all the tasks" %}')
  79. rendered = t.render(Context({}))
  80. self.assertEqual(rendered, 'IT translation of Completed 50%% of all the tasks')
  81. class MultipleLocaleCompilationTests(MessageCompilationTests):
  82. MO_FILE_HR = None
  83. MO_FILE_FR = None
  84. def setUp(self):
  85. super(MultipleLocaleCompilationTests, self).setUp()
  86. localedir = os.path.join(self.test_dir, 'locale')
  87. self.MO_FILE_HR = os.path.join(localedir, 'hr/LC_MESSAGES/django.mo')
  88. self.MO_FILE_FR = os.path.join(localedir, 'fr/LC_MESSAGES/django.mo')
  89. self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_HR))
  90. self.addCleanup(self.rmfile, os.path.join(localedir, self.MO_FILE_FR))
  91. def test_one_locale(self):
  92. with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
  93. call_command('compilemessages', locale=['hr'], stdout=StringIO())
  94. self.assertTrue(os.path.exists(self.MO_FILE_HR))
  95. def test_multiple_locales(self):
  96. with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
  97. call_command('compilemessages', locale=['hr', 'fr'], stdout=StringIO())
  98. self.assertTrue(os.path.exists(self.MO_FILE_HR))
  99. self.assertTrue(os.path.exists(self.MO_FILE_FR))
  100. class ExcludedLocaleCompilationTests(MessageCompilationTests):
  101. test_dir = os.path.abspath(os.path.join(os.path.dirname(upath(__file__)), 'exclude'))
  102. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo'
  103. def setUp(self):
  104. super(ExcludedLocaleCompilationTests, self).setUp()
  105. shutil.copytree('canned_locale', 'locale')
  106. self.addCleanup(self._rmrf, os.path.join(self.test_dir, 'locale'))
  107. def test_command_help(self):
  108. old_stdout, old_stderr = sys.stdout, sys.stderr
  109. sys.stdout, sys.stderr = StringIO(), StringIO()
  110. try:
  111. # `call_command` bypasses the parser; by calling
  112. # `execute_from_command_line` with the help subcommand we
  113. # ensure that there are no issues with the parser itself.
  114. execute_from_command_line(['django-admin', 'help', 'compilemessages'])
  115. finally:
  116. sys.stdout, sys.stderr = old_stdout, old_stderr
  117. def test_one_locale_excluded(self):
  118. call_command('compilemessages', exclude=['it'], stdout=StringIO())
  119. self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
  120. self.assertTrue(os.path.exists(self.MO_FILE % 'fr'))
  121. self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
  122. def test_multiple_locales_excluded(self):
  123. call_command('compilemessages', exclude=['it', 'fr'], stdout=StringIO())
  124. self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
  125. self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
  126. self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
  127. def test_one_locale_excluded_with_locale(self):
  128. call_command('compilemessages', locale=['en', 'fr'], exclude=['fr'], stdout=StringIO())
  129. self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
  130. self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
  131. self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
  132. def test_multiple_locales_excluded_with_locale(self):
  133. call_command('compilemessages', locale=['en', 'fr', 'it'], exclude=['fr', 'it'],
  134. stdout=StringIO())
  135. self.assertTrue(os.path.exists(self.MO_FILE % 'en'))
  136. self.assertFalse(os.path.exists(self.MO_FILE % 'fr'))
  137. self.assertFalse(os.path.exists(self.MO_FILE % 'it'))
  138. class CompilationErrorHandling(MessageCompilationTests):
  139. LOCALE = 'ja'
  140. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
  141. def setUp(self):
  142. super(CompilationErrorHandling, self).setUp()
  143. self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
  144. def test_error_reported_by_msgfmt(self):
  145. with self.assertRaises(CommandError):
  146. call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
  147. class FuzzyTranslationTest(MessageCompilationTests):
  148. LOCALE = 'ru'
  149. MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
  150. def setUp(self):
  151. super(FuzzyTranslationTest, self).setUp()
  152. self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
  153. gettext_module._translations = {} # flush cache or test will be useless
  154. def test_nofuzzy_compiling(self):
  155. with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
  156. call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
  157. with translation.override(self.LOCALE):
  158. self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
  159. self.assertEqual(ugettext('Vodka'), force_text('Vodka'))
  160. def test_fuzzy_compiling(self):
  161. with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
  162. call_command('compilemessages', locale=[self.LOCALE], fuzzy=True, stdout=StringIO())
  163. with translation.override(self.LOCALE):
  164. self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
  165. self.assertEqual(ugettext('Vodka'), force_text('Водка'))