cubicweb/devtools/test/unittest_i18n.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10964 devtools/test/unittest_i18n.py@bf381a894cd3
child 11099 5fdbf6f2db88
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     1
# -*- coding: iso-8859-1 -*-
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     2
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     4
#
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     5
# This file is part of CubicWeb.
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     6
#
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    10
# any later version.
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    11
#
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    15
# details.
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    16
#
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    19
"""unit tests for i18n messages generator"""
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    20
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    21
import os, os.path as osp
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    22
import sys
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    23
import subprocess
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    24
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    25
from unittest import TestCase, main
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    26
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    27
from cubicweb.cwconfig import CubicWebNoAppConfiguration
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    28
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    29
DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    30
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    31
def load_po(fname):
9200
1ba5961b19dd [i18n test] load_po: fix bug in case of multi-lines msgid and ensure there are no duplicated messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9153
diff changeset
    32
    """load a po file and  return a set of encountered (msgid, msgctx)"""
1ba5961b19dd [i18n test] load_po: fix bug in case of multi-lines msgid and ensure there are no duplicated messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9153
diff changeset
    33
    msgs = set()
1ba5961b19dd [i18n test] load_po: fix bug in case of multi-lines msgid and ensure there are no duplicated messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9153
diff changeset
    34
    msgid = msgctxt = None
10964
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    35
    with open(fname) as fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    36
        for line in fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    37
            if line.strip() in ('', '#'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    38
                continue
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    39
            if line.startswith('msgstr'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    40
                assert not (msgid, msgctxt) in msgs
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    41
                msgs.add( (msgid, msgctxt) )
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    42
                msgid = msgctxt = None
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    43
            elif line.startswith('msgid'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    44
                msgid = line.split(' ', 1)[1][1:-1]
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    45
            elif line.startswith('msgctx'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    46
                msgctxt = line.split(' ', 1)[1][1: -1]
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    47
            elif msgid is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    48
                msgid += line[1:-1]
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    49
            elif msgctxt is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    50
                msgctxt += line[1:-1]
9200
1ba5961b19dd [i18n test] load_po: fix bug in case of multi-lines msgid and ensure there are no duplicated messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9153
diff changeset
    51
    return msgs
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    52
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    53
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    54
class cubePotGeneratorTC(TestCase):
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    55
    """test case for i18n pot file generator"""
9200
1ba5961b19dd [i18n test] load_po: fix bug in case of multi-lines msgid and ensure there are no duplicated messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9153
diff changeset
    56
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    57
    def test_i18ncube(self):
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    58
        env = os.environ.copy()
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    59
        env['CW_CUBES_PATH'] = osp.join(DATADIR, 'cubes')
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    60
        if 'PYTHONPATH' in env:
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    61
            env['PYTHONPATH'] += os.pathsep
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    62
        else:
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    63
            env['PYTHONPATH'] = ''
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    64
        env['PYTHONPATH'] += DATADIR
11057
0b59724cb3f2 Reorganize source tree to have a "cubicweb" top-level package
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10964
diff changeset
    65
        cwctl = osp.abspath(osp.join(osp.dirname(__file__),
0b59724cb3f2 Reorganize source tree to have a "cubicweb" top-level package
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10964
diff changeset
    66
                                     '../../../bin/cubicweb-ctl'))
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    67
        with open(os.devnull, 'w') as devnull:
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    68
            subprocess.check_call([sys.executable, cwctl, 'i18ncube', 'i18ntestcube'],
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    69
                                  env=env, stdout=devnull)
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    70
        cube = osp.join(DATADIR, 'cubes', 'i18ntestcube')
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    71
        msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref'))
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    72
        newmsgs = load_po(osp.join(cube, 'i18n', 'en.po'))
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    73
        self.assertEqual(msgs, newmsgs)
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    74
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    75
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    76
if __name__ == '__main__':
10464
5ad491852062 [test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 9202
diff changeset
    77
    main()