16 # |
16 # |
17 # You should have received a copy of the GNU Lesser General Public License along |
17 # You should have received a copy of the GNU Lesser General Public License along |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """unit tests for i18n messages generator""" |
19 """unit tests for i18n messages generator""" |
20 |
20 |
21 import os, os.path as osp |
21 import os |
|
22 import os.path as osp |
22 import sys |
23 import sys |
23 import subprocess |
24 from subprocess import PIPE, Popen, STDOUT |
24 |
25 |
25 from unittest import TestCase, main |
26 from unittest import TestCase, main |
26 |
27 |
27 from cubicweb.cwconfig import CubicWebNoAppConfiguration |
|
28 |
28 |
29 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
29 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
30 |
30 |
31 |
31 def load_po(fname): |
32 def load_po(fname): |
32 """load a po file and return a set of encountered (msgid, msgctx)""" |
33 """load a po file and return a set of encountered (msgid, msgctx)""" |
33 msgs = set() |
34 msgs = set() |
34 msgid = msgctxt = None |
35 msgid = msgctxt = None |
36 for line in fobj: |
37 for line in fobj: |
37 if line.strip() in ('', '#'): |
38 if line.strip() in ('', '#'): |
38 continue |
39 continue |
39 if line.startswith('msgstr'): |
40 if line.startswith('msgstr'): |
40 assert not (msgid, msgctxt) in msgs |
41 assert not (msgid, msgctxt) in msgs |
41 msgs.add( (msgid, msgctxt) ) |
42 msgs.add((msgid, msgctxt)) |
42 msgid = msgctxt = None |
43 msgid = msgctxt = None |
43 elif line.startswith('msgid'): |
44 elif line.startswith('msgid'): |
44 msgid = line.split(' ', 1)[1][1:-1] |
45 msgid = line.split(' ', 1)[1][1:-1] |
45 elif line.startswith('msgctx'): |
46 elif line.startswith('msgctx'): |
46 msgctxt = line.split(' ', 1)[1][1: -1] |
47 msgctxt = line.split(' ', 1)[1][1: -1] |
60 if 'PYTHONPATH' in env: |
61 if 'PYTHONPATH' in env: |
61 env['PYTHONPATH'] += os.pathsep |
62 env['PYTHONPATH'] += os.pathsep |
62 else: |
63 else: |
63 env['PYTHONPATH'] = '' |
64 env['PYTHONPATH'] = '' |
64 env['PYTHONPATH'] += DATADIR |
65 env['PYTHONPATH'] += DATADIR |
65 with open(os.devnull, 'w') as devnull: |
66 cmd = [sys.executable, '-m', 'cubicweb', 'i18ncube', 'i18ntestcube'] |
66 subprocess.check_call( |
67 proc = Popen(cmd, env=env, stdout=PIPE, stderr=STDOUT) |
67 [sys.executable, '-m', 'cubicweb', 'i18ncube', 'i18ntestcube'], |
68 stdout, _ = proc.communicate() |
68 env=env, stdout=devnull) |
69 self.assertEqual(proc.returncode, 0, msg=stdout) |
69 cube = osp.join(DATADIR, 'cubes', 'i18ntestcube') |
70 cube = osp.join(DATADIR, 'cubes', 'i18ntestcube') |
70 msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref')) |
71 msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref')) |
71 newmsgs = load_po(osp.join(cube, 'i18n', 'en.po')) |
72 newmsgs = load_po(osp.join(cube, 'i18n', 'en.po')) |
72 self.assertEqual(msgs, newmsgs) |
73 self.assertEqual(msgs, newmsgs) |
73 |
74 |