cubicweb/devtools/test/unittest_i18n.py
changeset 11057 0b59724cb3f2
parent 10964 bf381a894cd3
child 11099 5fdbf6f2db88
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # -*- coding: iso-8859-1 -*-
       
     2 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    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/>.
       
    19 """unit tests for i18n messages generator"""
       
    20 
       
    21 import os, os.path as osp
       
    22 import sys
       
    23 import subprocess
       
    24 
       
    25 from unittest import TestCase, main
       
    26 
       
    27 from cubicweb.cwconfig import CubicWebNoAppConfiguration
       
    28 
       
    29 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
       
    30 
       
    31 def load_po(fname):
       
    32     """load a po file and  return a set of encountered (msgid, msgctx)"""
       
    33     msgs = set()
       
    34     msgid = msgctxt = None
       
    35     with open(fname) as fobj:
       
    36         for line in fobj:
       
    37             if line.strip() in ('', '#'):
       
    38                 continue
       
    39             if line.startswith('msgstr'):
       
    40                 assert not (msgid, msgctxt) in msgs
       
    41                 msgs.add( (msgid, msgctxt) )
       
    42                 msgid = msgctxt = None
       
    43             elif line.startswith('msgid'):
       
    44                 msgid = line.split(' ', 1)[1][1:-1]
       
    45             elif line.startswith('msgctx'):
       
    46                 msgctxt = line.split(' ', 1)[1][1: -1]
       
    47             elif msgid is not None:
       
    48                 msgid += line[1:-1]
       
    49             elif msgctxt is not None:
       
    50                 msgctxt += line[1:-1]
       
    51     return msgs
       
    52 
       
    53 
       
    54 class cubePotGeneratorTC(TestCase):
       
    55     """test case for i18n pot file generator"""
       
    56 
       
    57     def test_i18ncube(self):
       
    58         env = os.environ.copy()
       
    59         env['CW_CUBES_PATH'] = osp.join(DATADIR, 'cubes')
       
    60         if 'PYTHONPATH' in env:
       
    61             env['PYTHONPATH'] += os.pathsep
       
    62         else:
       
    63             env['PYTHONPATH'] = ''
       
    64         env['PYTHONPATH'] += DATADIR
       
    65         cwctl = osp.abspath(osp.join(osp.dirname(__file__),
       
    66                                      '../../../bin/cubicweb-ctl'))
       
    67         with open(os.devnull, 'w') as devnull:
       
    68             subprocess.check_call([sys.executable, cwctl, 'i18ncube', 'i18ntestcube'],
       
    69                                   env=env, stdout=devnull)
       
    70         cube = osp.join(DATADIR, 'cubes', 'i18ntestcube')
       
    71         msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref'))
       
    72         newmsgs = load_po(osp.join(cube, 'i18n', 'en.po'))
       
    73         self.assertEqual(msgs, newmsgs)
       
    74 
       
    75 
       
    76 if __name__ == '__main__':
       
    77     main()