[test] Fix unittest_i18n to run properly with "pytest -t" (closes #5576169)
authorRémi Cardona <remi.cardona@logilab.fr>
Tue, 30 Jun 2015 15:06:36 +0200
changeset 10464 5ad491852062
parent 10463 9add9b7f9df7
child 10465 5b18e2e4dd71
[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.
devtools/test/unittest_i18n.py
--- a/devtools/test/unittest_i18n.py	Mon Jun 29 16:58:43 2015 +0200
+++ b/devtools/test/unittest_i18n.py	Tue Jun 30 15:06:36 2015 +0200
@@ -20,8 +20,9 @@
 
 import os, os.path as osp
 import sys
+import subprocess
 
-from logilab.common.testlib import TestCase, unittest_main
+from unittest import TestCase, main
 
 from cubicweb.cwconfig import CubicWebNoAppConfiguration
 
@@ -52,28 +53,23 @@
 class cubePotGeneratorTC(TestCase):
     """test case for i18n pot file generator"""
 
-    def setUp(self):
-        self._CUBES_PATH = CubicWebNoAppConfiguration.CUBES_PATH[:]
-        CubicWebNoAppConfiguration.CUBES_PATH.append(osp.join(DATADIR, 'cubes'))
-        CubicWebNoAppConfiguration.cls_adjust_sys_path()
-
-    def tearDown(self):
-        CubicWebNoAppConfiguration.CUBES_PATH[:] = self._CUBES_PATH
-
     def test_i18ncube(self):
-        # MUST import here to make, since the import statement fire
-        # the cube paths setup (and then must occur after the setUp)
-        from cubicweb.devtools.devctl import update_cube_catalogs
+        env = os.environ.copy()
+        env['CW_CUBES_PATH'] = osp.join(DATADIR, 'cubes')
+        if 'PYTHONPATH' in env:
+            env['PYTHONPATH'] += os.pathsep
+        else:
+            env['PYTHONPATH'] = ''
+        env['PYTHONPATH'] += DATADIR
+        cwctl = osp.abspath(osp.join(osp.dirname(__file__), '../../bin/cubicweb-ctl'))
+        with open(os.devnull, 'w') as devnull:
+            subprocess.check_call([sys.executable, cwctl, 'i18ncube', 'i18ntestcube'],
+                                  env=env, stdout=devnull)
         cube = osp.join(DATADIR, 'cubes', 'i18ntestcube')
         msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref'))
-        update_cube_catalogs(cube)
         newmsgs = load_po(osp.join(cube, 'i18n', 'en.po'))
         self.assertEqual(msgs, newmsgs)
 
+
 if __name__ == '__main__':
-    # XXX dirty hack to make this test runnable using python (works
-    # fine with pytest, but not with python directly if this hack is
-    # not present)
-    # XXX to remove ASA logilab.common is fixed
-    sys.path.append('')
-    unittest_main()
+    main()