[test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)

When running "pytest -t", many tests modules will be imported and run within a single python process. unittest_i18n fails when run after tests such as unittest_webtest.py or unittest_httptest.py.

The main reason is that unittest_i18n does various tricks to add a test cube to the search path, which badly fails when a previous test module has already loaded "stuff" (for lack of a better word).

Instead, let's just call the 'i18ncube' command with subprocess.

The change from logilab.common.testlib to unittest is not directly needed, but comes as a free cleanup with the above changes.

authorRémi Cardona <remi.cardona@logilab.fr>
changeset5ad491852062
branchdefault
phasepublic
hiddenno
parent revision#9add9b7f9df7 [server/test] fix random error in unittest_security
child revision#5b18e2e4dd71 [js] removed unused and broken SuggestForm Widget (closes #5213487)
files modified by this revision
devtools/test/unittest_i18n.py
# HG changeset patch
# User Rémi Cardona <remi.cardona@logilab.fr>
# Date 1435669596 -7200
# Tue Jun 30 15:06:36 2015 +0200
# Node ID 5ad491852062f863d37ed0df3e95229c99bf18e4
# Parent 9add9b7f9df7f8fb956d9db6c1e360d0a9e58655
[test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)

When running "pytest -t", many tests modules will be imported and run
within a single python process. unittest_i18n fails when run after tests
such as unittest_webtest.py or unittest_httptest.py.

The main reason is that unittest_i18n does various tricks to add a test
cube to the search path, which badly fails when a previous test module
has already loaded "stuff" (for lack of a better word).

Instead, let's just call the 'i18ncube' command with subprocess.

The change from logilab.common.testlib to unittest is not directly
needed, but comes as a free cleanup with the above changes.

diff --git a/devtools/test/unittest_i18n.py b/devtools/test/unittest_i18n.py
@@ -18,12 +18,13 @@
1  # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2  """unit tests for i18n messages generator"""
3 
4  import os, os.path as osp
5  import sys
6 +import subprocess
7 
8 -from logilab.common.testlib import TestCase, unittest_main
9 +from unittest import TestCase, main
10 
11  from cubicweb.cwconfig import CubicWebNoAppConfiguration
12 
13  DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
14 
@@ -50,30 +51,25 @@
15 
16 
17  class cubePotGeneratorTC(TestCase):
18      """test case for i18n pot file generator"""
19 
20 -    def setUp(self):
21 -        self._CUBES_PATH = CubicWebNoAppConfiguration.CUBES_PATH[:]
22 -        CubicWebNoAppConfiguration.CUBES_PATH.append(osp.join(DATADIR, 'cubes'))
23 -        CubicWebNoAppConfiguration.cls_adjust_sys_path()
24 -
25 -    def tearDown(self):
26 -        CubicWebNoAppConfiguration.CUBES_PATH[:] = self._CUBES_PATH
27 -
28      def test_i18ncube(self):
29 -        # MUST import here to make, since the import statement fire
30 -        # the cube paths setup (and then must occur after the setUp)
31 -        from cubicweb.devtools.devctl import update_cube_catalogs
32 +        env = os.environ.copy()
33 +        env['CW_CUBES_PATH'] = osp.join(DATADIR, 'cubes')
34 +        if 'PYTHONPATH' in env:
35 +            env['PYTHONPATH'] += os.pathsep
36 +        else:
37 +            env['PYTHONPATH'] = ''
38 +        env['PYTHONPATH'] += DATADIR
39 +        cwctl = osp.abspath(osp.join(osp.dirname(__file__), '../../bin/cubicweb-ctl'))
40 +        with open(os.devnull, 'w') as devnull:
41 +            subprocess.check_call([sys.executable, cwctl, 'i18ncube', 'i18ntestcube'],
42 +                                  env=env, stdout=devnull)
43          cube = osp.join(DATADIR, 'cubes', 'i18ntestcube')
44          msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref'))
45 -        update_cube_catalogs(cube)
46          newmsgs = load_po(osp.join(cube, 'i18n', 'en.po'))
47          self.assertEqual(msgs, newmsgs)
48 
49 +
50  if __name__ == '__main__':
51 -    # XXX dirty hack to make this test runnable using python (works
52 -    # fine with pytest, but not with python directly if this hack is
53 -    # not present)
54 -    # XXX to remove ASA logilab.common is fixed
55 -    sys.path.append('')
56 -    unittest_main()
57 +    main()