cubicweb/devtools/test/unittest_i18n.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 05 Jul 2016 13:27:19 +0200
branch3.23
changeset 11422 63ac20ef558e
parent 11208 fe57dc4adfea
child 11460 5be729810695
permissions -rw-r--r--
[pkg] Properly export data files in setup.py and adjust "newcube" test With the new package layout (everything under "cubicweb" package), the custom install_lib rule which makes use of include_dirs defined in __pkginfo__.py did not prepend the package name to source directories to be copied. Fixing this. Also, in setup.py's export() function, the destination directories' path to be created during source tree walk was wrong. All this makes cubicweb/skeleton directory (which is not a package) properly installed by setup.py. The test in cubicweb/devtools/test/unittest_devctl.py wasn't properly implemented because it used an installation of cubicweb in "develop" mode which shadows such packaging issues. Also it used "python -m cubicweb" instead of directly "cubicweb-ctl" and the former appears to fall back to using the cubicweb package *from sources* instead of the installed one. Now that this test runs against the installed version of cubicweb, fix MANIFEST.in to include tox.ini files (cubicweb's and skeleton's) as this is expected from the test. Closes #14127941.
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()
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
        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
    61
        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
    62
            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
    63
        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
    64
            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
    65
        env['PYTHONPATH'] += DATADIR
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    66
        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
    67
        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
    68
        stdout, _ = proc.communicate()
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    69
        self.assertEqual(proc.returncode, 0, msg=stdout)
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()