PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/i18n/commands/extraction.py

https://code.google.com/p/mango-py/
Python | 170 lines | 131 code | 37 blank | 2 comment | 14 complexity | d90c9a19088d16aceef10f4ff13006a7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # -*- encoding: utf-8 -*-
  2. import os
  3. import re
  4. import shutil
  5. from django.test import TestCase
  6. from django.core import management
  7. LOCALE='de'
  8. class ExtractorTests(TestCase):
  9. PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
  10. def setUp(self):
  11. self._cwd = os.getcwd()
  12. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  13. def _rmrf(self, dname):
  14. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  15. return
  16. shutil.rmtree(dname)
  17. def tearDown(self):
  18. os.chdir(self.test_dir)
  19. try:
  20. self._rmrf('locale/%s' % LOCALE)
  21. except OSError:
  22. pass
  23. os.chdir(self._cwd)
  24. def assertMsgId(self, msgid, s, use_quotes=True):
  25. if use_quotes:
  26. msgid = '"%s"' % msgid
  27. return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE))
  28. def assertNotMsgId(self, msgid, s, use_quotes=True):
  29. if use_quotes:
  30. msgid = '"%s"' % msgid
  31. return self.assertTrue(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
  32. class BasicExtractorTests(ExtractorTests):
  33. def test_comments_extractor(self):
  34. os.chdir(self.test_dir)
  35. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  36. self.assertTrue(os.path.exists(self.PO_FILE))
  37. po_contents = open(self.PO_FILE, 'r').read()
  38. self.assertTrue('#. Translators: This comment should be extracted' in po_contents)
  39. self.assertTrue('This comment should not be extracted' not in po_contents)
  40. # Comments in templates
  41. self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
  42. self.assertTrue("#. Translators: Django comment block for translators\n#. string's meaning unveiled" in po_contents)
  43. self.assertTrue('#. Translators: One-line translator comment #1' in po_contents)
  44. self.assertTrue('#. Translators: Two-line translator comment #1\n#. continued here.' in po_contents)
  45. self.assertTrue('#. Translators: One-line translator comment #2' in po_contents)
  46. self.assertTrue('#. Translators: Two-line translator comment #2\n#. continued here.' in po_contents)
  47. self.assertTrue('#. Translators: One-line translator comment #3' in po_contents)
  48. self.assertTrue('#. Translators: Two-line translator comment #3\n#. continued here.' in po_contents)
  49. self.assertTrue('#. Translators: One-line translator comment #4' in po_contents)
  50. self.assertTrue('#. Translators: Two-line translator comment #4\n#. continued here.' in po_contents)
  51. self.assertTrue('#. Translators: One-line translator comment #5 -- with non ASCII characters: αινσϊφ' in po_contents)
  52. self.assertTrue('#. Translators: Two-line translator comment #5 -- with non ASCII characters: αινσϊφ\n#. continued here.' in po_contents)
  53. def test_templatize(self):
  54. os.chdir(self.test_dir)
  55. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  56. self.assertTrue(os.path.exists(self.PO_FILE))
  57. po_contents = open(self.PO_FILE, 'r').read()
  58. self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
  59. self.assertMsgId('I think that 100%% is more that 50%% of %\(obj\)s.', po_contents)
  60. def test_extraction_error(self):
  61. os.chdir(self.test_dir)
  62. shutil.copyfile('./templates/template_with_error.txt', './templates/template_with_error.html')
  63. self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=LOCALE, verbosity=0)
  64. try: # TODO: Simplify this try/try block when we drop support for Python 2.4
  65. try:
  66. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  67. except SyntaxError, e:
  68. self.assertEqual(str(e), 'Translation blocks must not include other block tags: blocktrans (file templates/template_with_error.html, line 3)')
  69. finally:
  70. os.remove('./templates/template_with_error.html')
  71. os.remove('./templates/template_with_error.html.py') # Waiting for #8536 to be fixed
  72. class JavascriptExtractorTests(ExtractorTests):
  73. PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
  74. def test_javascript_literals(self):
  75. os.chdir(self.test_dir)
  76. management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
  77. self.assertTrue(os.path.exists(self.PO_FILE))
  78. po_contents = open(self.PO_FILE, 'r').read()
  79. self.assertMsgId('This literal should be included.', po_contents)
  80. self.assertMsgId('This one as well.', po_contents)
  81. class IgnoredExtractorTests(ExtractorTests):
  82. def test_ignore_option(self):
  83. os.chdir(self.test_dir)
  84. management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=['ignore_dir/*'])
  85. self.assertTrue(os.path.exists(self.PO_FILE))
  86. po_contents = open(self.PO_FILE, 'r').read()
  87. self.assertMsgId('This literal should be included.', po_contents)
  88. self.assertNotMsgId('This should be ignored.', po_contents)
  89. class SymlinkExtractorTests(ExtractorTests):
  90. def setUp(self):
  91. self._cwd = os.getcwd()
  92. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  93. self.symlinked_dir = os.path.join(self.test_dir, 'templates_symlinked')
  94. def tearDown(self):
  95. super(SymlinkExtractorTests, self).tearDown()
  96. os.chdir(self.test_dir)
  97. try:
  98. os.remove(self.symlinked_dir)
  99. except OSError:
  100. pass
  101. os.chdir(self._cwd)
  102. def test_symlink(self):
  103. if hasattr(os, 'symlink'):
  104. if os.path.exists(self.symlinked_dir):
  105. self.assertTrue(os.path.islink(self.symlinked_dir))
  106. else:
  107. os.symlink(os.path.join(self.test_dir, 'templates'), self.symlinked_dir)
  108. os.chdir(self.test_dir)
  109. management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
  110. self.assertTrue(os.path.exists(self.PO_FILE))
  111. po_contents = open(self.PO_FILE, 'r').read()
  112. self.assertMsgId('This literal should be included.', po_contents)
  113. self.assertTrue('templates_symlinked/test.html' in po_contents)
  114. class CopyPluralFormsExtractorTests(ExtractorTests):
  115. def test_copy_plural_forms(self):
  116. os.chdir(self.test_dir)
  117. management.call_command('makemessages', locale=LOCALE, verbosity=0)
  118. self.assertTrue(os.path.exists(self.PO_FILE))
  119. po_contents = open(self.PO_FILE, 'r').read()
  120. self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
  121. class NoWrapExtractorTests(ExtractorTests):
  122. def test_no_wrap_enabled(self):
  123. os.chdir(self.test_dir)
  124. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
  125. self.assertTrue(os.path.exists(self.PO_FILE))
  126. po_contents = open(self.PO_FILE, 'r').read()
  127. self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
  128. def test_no_wrap_disabled(self):
  129. os.chdir(self.test_dir)
  130. management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
  131. self.assertTrue(os.path.exists(self.PO_FILE))
  132. po_contents = open(self.PO_FILE, 'r').read()
  133. self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)