]> git.evergreen-ils.org Git - Evergreen.git/blob - build/i18n/tests/testpo.py
baa0fac80f91300f289069f9bdda59d7c8549195
[Evergreen.git] / build / i18n / tests / testpo.py
1 #!/usr/bin/env python
2
3 import filecmp
4 import glob
5 import os
6 import re
7 import subprocess
8 import sys
9 import unittest
10
11 class TestPOFramework(unittest.TestCase):
12
13     po_sources = ('../../Open-ILS/web/opac/locale/en-US/*.dtd', \
14         '../../Open-ILS/xul/staff_client/chrome/locale/en-US/*.properties')
15
16     def setUp(self):
17         devnull = open('/dev/null', 'w')
18         proc = subprocess.Popen(('make', 'LOCALE=ll-LL', 'newpo'), 0, None, None, devnull, devnull).wait()
19         proc = subprocess.Popen(('make', 'LOCALE=ll-LL', 'newproject'), 0, None, None, devnull, devnull).wait()
20
21     def tearDown(self):
22         for file in os.listdir('po/ll-LL/'):
23             os.remove(os.path.join('po/ll-LL', file))
24         os.rmdir('po/ll-LL/')
25         for file in os.listdir('locale/ll-LL/'):
26             os.remove(os.path.join('locale/ll-LL/', file))
27         os.rmdir('locale/ll-LL/')
28
29     def testnewpofiles(self):
30         # Create a brand new set of PO files from our en-US project files.
31         # Compare the files generated in the po/ll-LL directory with
32         # the number expected by a manual count of our known sources.
33         po_files = []
34         for po_dir in self.po_sources:
35             for path in glob.glob(po_dir):
36                 po_files.append(os.path.basename(path) + '.po')
37         po_files.sort()
38         new_pofiles = os.listdir('po/ll-LL/')
39         new_pofiles.sort()
40         self.assertEqual(po_files, new_pofiles)
41
42     def testnewprojectfiles(self):
43         # Create a brand new set of project files from PO files.
44         # Compare the files created with a manual count of our known sources.
45         moz_files = []
46         for po_dir in self.po_sources:
47             for path in glob.glob(po_dir):
48                 moz_files.append(os.path.basename(path))
49         moz_files.sort()
50         new_mozfiles = os.listdir('locale/ll-LL/')
51         new_mozfiles.sort()
52         self.assertEqual(moz_files, new_mozfiles)
53
54     def testtranslatedfile(self):
55         # "Translate" strings in a PO file, then generate the project
56         # files to ensure that the translated string appears in the output.
57
58         # Create the "translated" PO file
59         commonpo = 'po/ll-LL/common.properties.po'
60         testpo = 'po/ll-LL/test.properties.po'
61         commonfile = open(commonpo)
62         testfile = open(testpo, 'w')
63         for line in commonfile:
64             line = re.sub(r'^msgstr ""', r'msgstr "abcdefg"', line)
65             testfile.write(line)
66         commonfile.close()
67         testfile.close()
68         os.remove(commonpo)
69         os.rename(testpo, commonpo)
70
71         # Create the "translated" properties file
72         commonprops = 'locale/ll-LL/common.properties'
73         testprops = 'locale/ll-LL/test.properties'
74         commonfile = open(commonprops)
75         testfile = open(testprops, 'w')
76         for line in commonfile:
77             line = re.sub(r'^(.*?)=.*?$', r'\1=abcdefg', line)
78             testfile.write(line)
79         commonfile.close()
80         testfile.close()
81
82         # Regenerate the project files to get the translated strings in place
83         devnull = open('/dev/null', 'w')
84         proc = subprocess.Popen(('make', 'LOCALE=ll-LL', 'newproject'), 0, None, None, devnull, devnull).wait()
85
86         self.assertEqual(filecmp.cmp(commonprops, testprops), 1)
87
88 if __name__ == '__main__':
89     os.chdir('..')
90     unittest.main()