cubicweb/devtools/test/unittest_i18n.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Wed, 08 Jun 2016 21:59:34 +0200
changeset 11460 5be729810695
parent 11208 fe57dc4adfea
child 11735 7a170207acbf
permissions -rw-r--r--
[devtools] Handle i18ncube command for "cubes as packages" And add a test so that both layouts are tested. Move the test cube into a package. Related to #13001466.
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
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    21
import os
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    22
import os.path as osp
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    23
import sys
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    24
from subprocess import PIPE, Popen, STDOUT
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    25
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
    26
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
    27
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
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    31
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    32
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
    33
    """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
    34
    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
    35
    msgid = msgctxt = None
10964
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    36
    with open(fname) as fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    37
        for line in fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    38
            if line.strip() in ('', '#'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    39
                continue
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    40
            if line.startswith('msgstr'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    41
                assert not (msgid, msgctxt) in msgs
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    42
                msgs.add((msgid, msgctxt))
10964
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    43
                msgid = msgctxt = None
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    44
            elif line.startswith('msgid'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    45
                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
    46
            elif line.startswith('msgctx'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    47
                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
    48
            elif msgid is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    49
                msgid += line[1:-1]
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    50
            elif msgctxt is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    51
                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
    52
    return msgs
9153
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
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    55
class cubePotGeneratorTC(TestCase):
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    56
    """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
    57
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    58
    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
    59
        env = os.environ.copy()
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    60
        if 'PYTHONPATH' in env:
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    61
            env['PYTHONPATH'] += os.pathsep
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    62
        else:
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    63
            env['PYTHONPATH'] = ''
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    64
        env['PYTHONPATH'] += osp.join(DATADIR, 'libpython')
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    65
        cubedir = osp.join(DATADIR, 'libpython', 'cubicweb_i18ntestcube')
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    66
        self._check(cubedir, env)
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    67
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    68
    def test_i18ncube_legacy_layout(self):
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    69
        env = os.environ.copy()
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
    70
        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
    71
        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
    72
            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
    73
        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
    74
            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
    75
        env['PYTHONPATH'] += DATADIR
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    76
        cubedir = osp.join(DATADIR, 'cubes', 'i18ntestcube')
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    77
        self._check(cubedir, env)
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    78
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    79
    def _check(self, cubedir, env):
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    80
        cmd = [sys.executable, '-m', 'cubicweb', 'i18ncube', 'i18ntestcube']
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    81
        proc = Popen(cmd, env=env, stdout=PIPE, stderr=STDOUT)
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    82
        stdout, _ = proc.communicate()
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    83
        msg = stdout.decode(sys.getdefaultencoding(), errors='replace')
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    84
        self.assertEqual(proc.returncode, 0, msg=msg)
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    85
        msgs = load_po(osp.join(cubedir, 'i18n', 'en.po.ref'))
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    86
        newmsgs = load_po(osp.join(cubedir, 'i18n', 'en.po'))
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    87
        self.assertEqual(msgs, newmsgs)
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    88
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
    89
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    90
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
    91
    main()