4 from django
.test
import TestCase
5 from django
.core
import management
9 class ExtractorTests(TestCase
):
11 PO_FILE
='locale/%s/LC_MESSAGES/django.po' % LOCALE
14 self
._cwd
= os
.getcwd()
15 self
.test_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
17 def _rmrf(self
, dname
):
18 if os
.path
.commonprefix([self
.test_dir
, os
.path
.abspath(dname
)]) != self
.test_dir
:
23 os
.chdir(self
.test_dir
)
25 self
._rmrf
('locale/%s' % LOCALE
)
30 def assertMsgId(self
, msgid
, s
):
31 return self
.assert_(re
.search('^msgid "%s"' % msgid
, s
, re
.MULTILINE
))
33 def assertNotMsgId(self
, msgid
, s
):
34 return self
.assert_(not re
.search('^msgid "%s"' % msgid
, s
, re
.MULTILINE
))
37 class JavascriptExtractorTests(ExtractorTests
):
39 PO_FILE
='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
41 def test_javascript_literals(self
):
42 os
.chdir(self
.test_dir
)
43 management
.call_command('makemessages', domain
='djangojs', locale
=LOCALE
, verbosity
=0)
44 self
.assert_(os
.path
.exists(self
.PO_FILE
))
45 po_contents
= open(self
.PO_FILE
, 'r').read()
46 self
.assertMsgId('This literal should be included.', po_contents
)
47 self
.assertMsgId('This one as well.', po_contents
)
50 class IgnoredExtractorTests(ExtractorTests
):
52 def test_ignore_option(self
):
53 os
.chdir(self
.test_dir
)
54 management
.call_command('makemessages', locale
=LOCALE
, verbosity
=0, ignore_patterns
=['ignore_dir/*'])
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
.assertNotMsgId('This should be ignored.', po_contents
)
61 class SymlinkExtractorTests(ExtractorTests
):
64 self
._cwd
= os
.getcwd()
65 self
.test_dir
= os
.path
.abspath(os
.path
.dirname(__file__
))
66 self
.symlinked_dir
= os
.path
.join(self
.test_dir
, 'templates_symlinked')
69 super(SymlinkExtractorTests
, self
).tearDown()
70 os
.chdir(self
.test_dir
)
72 os
.remove(self
.symlinked_dir
)
77 def test_symlink(self
):
78 if hasattr(os
, 'symlink'):
79 if os
.path
.exists(self
.symlinked_dir
):
80 self
.assert_(os
.path
.islink(self
.symlinked_dir
))
82 os
.symlink(os
.path
.join(self
.test_dir
, 'templates'), self
.symlinked_dir
)
83 os
.chdir(self
.test_dir
)
84 management
.call_command('makemessages', locale
=LOCALE
, verbosity
=0, symlinks
=True)
85 self
.assert_(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
.assert_('templates_symlinked/test.html' in po_contents
)
91 class CopyPluralFormsExtractorTests(ExtractorTests
):
93 def test_copy_plural_forms(self
):
94 os
.chdir(self
.test_dir
)
95 management
.call_command('makemessages', locale
=LOCALE
, verbosity
=0)
96 self
.assert_(os
.path
.exists(self
.PO_FILE
))
97 po_contents
= open(self
.PO_FILE
, 'r').read()
98 self
.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents
)