cubicweb/devtools/test/unittest_i18n.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 21 Feb 2019 18:56:42 +0100
changeset 12399 c0ad7aa36e4a
parent 11822 adf0212d1dcc
child 12546 465ee7fb16e3
permissions -rw-r--r--
[doc] misc, put every reference to something in the code between backquotes
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
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    21
from contextlib import contextmanager
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    22
from io import StringIO, BytesIO
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    23
import os
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    24
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
    25
import sys
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    26
from subprocess import PIPE, Popen, STDOUT
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
    27
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
    28
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    29
from six import PY2
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    30
from mock import patch
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    31
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    32
from cubicweb.devtools import devctl
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    33
from cubicweb.devtools.testlib import BaseTestCase
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    34
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    35
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
    36
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    37
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    38
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
    39
    """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
    40
    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
    41
    msgid = msgctxt = None
10964
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    42
    with open(fname) as fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    43
        for line in fobj:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    44
            if line.strip() in ('', '#'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    45
                continue
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    46
            if line.startswith('msgstr'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    47
                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
    48
                msgs.add((msgid, msgctxt))
10964
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    49
                msgid = msgctxt = None
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    50
            elif line.startswith('msgid'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    51
                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
    52
            elif line.startswith('msgctx'):
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    53
                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
    54
            elif msgid is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    55
                msgid += line[1:-1]
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    56
            elif msgctxt is not None:
bf381a894cd3 [devtools] Fix fd leak in tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10464
diff changeset
    57
                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
    58
    return msgs
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    59
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    60
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    61
TESTCUBE_DIR = osp.join(DATADIR, 'cubes', 'i18ntestcube')
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    62
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    63
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    64
class cubePotGeneratorTC(TestCase):
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    65
    """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
    66
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    67
    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
    68
        env = os.environ.copy()
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    69
        if 'PYTHONPATH' in env:
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    70
            env['PYTHONPATH'] += os.pathsep
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    71
        else:
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    72
            env['PYTHONPATH'] = ''
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    73
        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
    74
        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
    75
        self._check(cubedir, env)
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    76
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    77
    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
    78
        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
    79
        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
    80
        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
    81
            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
    82
        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
    83
            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
    84
        env['PYTHONPATH'] += DATADIR
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    85
        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
    86
        self._check(cubedir, env)
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    87
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    88
    def _check(self, cubedir, env):
11208
fe57dc4adfea [test] Use a Popen in i18n tests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11099
diff changeset
    89
        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
    90
        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
    91
        stdout, _ = proc.communicate()
11460
5be729810695 [devtools] Handle i18ncube command for "cubes as packages"
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11208
diff changeset
    92
        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
    93
        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
    94
        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
    95
        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
    96
        self.assertEqual(msgs, newmsgs)
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
    97
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
    98
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
    99
class CustomMessageExtractor(devctl.I18nCubeMessageExtractor):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   100
    blacklist = devctl.I18nCubeMessageExtractor.blacklist | set(['excludeme'])
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   101
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   102
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   103
@contextmanager
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   104
def capture_stdout():
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   105
    stream = BytesIO() if PY2 else StringIO()
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   106
    sys.stdout = stream
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   107
    yield stream
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   108
    stream.seek(0)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   109
    sys.stdout = sys.__stdout__
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   110
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   111
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   112
class I18nCollectorTest(BaseTestCase):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   113
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   114
    def test_i18ncube_py_collection(self):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   115
        extractor = CustomMessageExtractor(DATADIR, TESTCUBE_DIR)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   116
        collected = extractor.collect_py()
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   117
        expected = [osp.join(TESTCUBE_DIR, path)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   118
                    for path in ('__init__.py', '__pkginfo__.py',
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   119
                                 'views.py', 'schema.py')]
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   120
        self.assertCountEqual(expected, collected)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   121
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   122
    def test_i18ncube_js_collection(self):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   123
        extractor = CustomMessageExtractor(DATADIR, TESTCUBE_DIR)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   124
        collected = extractor.collect_js()
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   125
        self.assertCountEqual([], collected, [])
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   126
        extractor.blacklist = ()  # don't ignore anything
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   127
        collected = extractor.collect_js()
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   128
        expected = [osp.join(TESTCUBE_DIR, 'node_modules/cubes.somefile.js')]
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   129
        self.assertCountEqual(expected, collected)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   130
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   131
    class FakeMessageExtractor(devctl.I18nCubeMessageExtractor):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   132
        """Fake message extractor that generates no pot file."""
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   133
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   134
        def generate_pot_file(self):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   135
            return None
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   136
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   137
    @patch('pkg_resources.load_entry_point', return_value=FakeMessageExtractor)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   138
    def test_cube_custom_extractor(self, mock_load_entry_point):
11822
adf0212d1dcc [devctl] make i18n custom message extractors work with legacy layouts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11735
diff changeset
   139
        distname = 'cubicweb_i18ntestcube'  # same for new and legacy layout
adf0212d1dcc [devctl] make i18n custom message extractors work with legacy layouts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11735
diff changeset
   140
        for cubedir in [
adf0212d1dcc [devctl] make i18n custom message extractors work with legacy layouts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11735
diff changeset
   141
            osp.join(DATADIR, 'libpython', 'cubicweb_i18ntestcube'),
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   142
            # Legacy cubes.
11822
adf0212d1dcc [devctl] make i18n custom message extractors work with legacy layouts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11735
diff changeset
   143
            osp.join(DATADIR, 'cubes', 'i18ntestcube'),
11735
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   144
        ]:
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   145
            with self.subTest(cubedir=cubedir):
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   146
                with capture_stdout() as stream:
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   147
                    devctl.update_cube_catalogs(cubedir)
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   148
                self.assertIn(u'no message catalog for cube i18ntestcube',
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   149
                              stream.read())
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   150
                mock_load_entry_point.assert_called_once_with(
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   151
                    distname, 'cubicweb.i18ncube', 'i18ntestcube')
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   152
                mock_load_entry_point.reset_mock()
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   153
7a170207acbf [devtools] make i18ncube customizable in a cube
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 11460
diff changeset
   154
9153
bc1b8e77d6ce [test/devctl] add a test case for i18ncube command
David Douard <david.douard@logilab.fr>
parents:
diff changeset
   155
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
   156
    main()