tests.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import re
  3. import shutil
  4. from django.test import TestCase
  5. from django.core import management
  6. LOCALE='de'
  7. class ExtractorTests(TestCase):
  8. def setUp(self):
  9. self._cwd = os.getcwd()
  10. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  11. def _rmrf(self, dname):
  12. if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
  13. return
  14. shutil.rmtree(dname)
  15. def tearDown(self):
  16. os.chdir(self.test_dir)
  17. try:
  18. self._rmrf('locale/%s' % LOCALE)
  19. except OSError:
  20. pass
  21. os.chdir(self._cwd)
  22. def assertMsgId(self, msgid, s):
  23. return self.assert_(re.search('^msgid "%s"' % msgid, s, re.MULTILINE))
  24. class JavascriptExtractorTests(ExtractorTests):
  25. PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
  26. def test_javascript_literals(self):
  27. os.chdir(self.test_dir)
  28. management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
  29. self.assert_(os.path.exists(self.PO_FILE))
  30. po_contents = open(self.PO_FILE, 'r').read()
  31. self.assertMsgId('This literal should be included.', po_contents)
  32. self.assertMsgId('This one as well.', po_contents)
  33. class SymlinkExtractorTests(ExtractorTests):
  34. PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
  35. def setUp(self):
  36. self._cwd = os.getcwd()
  37. self.test_dir = os.path.abspath(os.path.dirname(__file__))
  38. self.symlinked_dir = os.path.join(self.test_dir, 'templates_symlinked')
  39. def tearDown(self):
  40. super(SymlinkExtractorTests, self).tearDown()
  41. os.chdir(self.test_dir)
  42. try:
  43. os.remove(self.symlinked_dir)
  44. except OSError:
  45. pass
  46. os.chdir(self._cwd)
  47. def test_symlink(self):
  48. if hasattr(os, 'symlink'):
  49. if os.path.exists(self.symlinked_dir):
  50. self.assert_(os.path.islink(self.symlinked_dir))
  51. else:
  52. os.symlink(os.path.join(self.test_dir, 'templates'), self.symlinked_dir)
  53. os.chdir(self.test_dir)
  54. management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
  55. self.assert_(os.path.exists(self.PO_FILE))
  56. po_contents = open(self.PO_FILE, 'r').read()
  57. self.assertMsgId('This literal should be included.', po_contents)
  58. self.assert_('templates_symlinked/test.html' in po_contents)