diff -r e0d708fb20e8 -r f85ec84355db cubicweb/test/unittest_cubes.py --- a/cubicweb/test/unittest_cubes.py Wed Feb 08 10:31:26 2017 +0100 +++ b/cubicweb/test/unittest_cubes.py Tue Feb 07 13:47:03 2017 +0100 @@ -21,13 +21,12 @@ import os from os import path import sys -import unittest from six import PY2 from cubicweb import _CubesImporter from cubicweb.cwconfig import CubicWebConfiguration -from cubicweb.devtools.testlib import TemporaryDirectory +from cubicweb.devtools.testlib import TemporaryDirectory, TestCase @contextmanager @@ -37,17 +36,20 @@ libdir = path.join(tempdir, 'libpython') cubedir = path.join(libdir, 'cubicweb_foo') os.makedirs(cubedir) + check_code = ("import logging\n" + "logging.getLogger('cubicweb_foo')" + ".warn('imported %s', __name__)\n") with open(path.join(cubedir, '__init__.py'), 'w') as f: - f.write('"""cubicweb_foo application package"""') + f.write("'cubicweb_foo application package'\n" + check_code) with open(path.join(cubedir, 'bar.py'), 'w') as f: - f.write('baz = 1') + f.write(check_code + 'baz = 1\n') sys.path.append(libdir) yield cubedir finally: sys.path.remove(libdir) -class CubesImporterTC(unittest.TestCase): +class CubesImporterTC(TestCase): def setUp(self): # During discovery, CubicWebConfiguration.cls_adjust_sys_path may be @@ -104,6 +106,27 @@ new = importlib.reload(cubes) self.assertIs(new, cubes) + def test_no_double_import(self): + """Check new and legacy import the same module once""" + with temp_cube(): + CubicWebConfiguration.cls_adjust_sys_path() + with self.assertLogs('cubicweb_foo', 'WARNING') as cm: + from cubes.foo import bar + from cubicweb_foo import bar as bar2 + self.assertIs(bar, bar2) + self.assertIs(sys.modules['cubes.foo'], + sys.modules['cubicweb_foo']) + self.assertEqual(cm.output, [ + 'WARNING:cubicweb_foo:imported cubicweb_foo', + # module __name__ for subpackage differ along python version + # for PY2 it's based on how the module was imported "from + # cubes.foo import bar" and for PY3 based on __name__ of parent + # module "cubicweb_foo". Not sure if it's an issue, but PY3 + # behavior looks better. + 'WARNING:cubicweb_foo:imported ' + ( + 'cubes.foo.bar' if PY2 else 'cubicweb_foo.bar') + ]) + def test_import_legacy_cube(self): """Check that importing a legacy cube works when sys.path got adjusted. """