cubicweb/test/unittest_cubes.py
changeset 11457 d404fd8499dd
child 11912 c9e6df20e5a4
equal deleted inserted replaced
11456:077f32a7a4c3 11457:d404fd8499dd
       
     1 # copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License
       
    17 # along with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """Unit tests for "cubes" importer."""
       
    19 
       
    20 from contextlib import contextmanager
       
    21 import os
       
    22 from os import path
       
    23 import shutil
       
    24 import sys
       
    25 import tempfile
       
    26 import unittest
       
    27 
       
    28 from six import PY2
       
    29 
       
    30 from cubicweb import _CubesImporter
       
    31 from cubicweb.cwconfig import CubicWebConfiguration
       
    32 
       
    33 
       
    34 @contextmanager
       
    35 def temp_cube():
       
    36     tempdir = tempfile.mkdtemp()
       
    37     try:
       
    38         libdir = path.join(tempdir, 'libpython')
       
    39         cubedir = path.join(libdir, 'cubicweb_foo')
       
    40         os.makedirs(cubedir)
       
    41         with open(path.join(cubedir, '__init__.py'), 'w') as f:
       
    42             f.write('"""cubicweb_foo application package"""')
       
    43         with open(path.join(cubedir, 'bar.py'), 'w') as f:
       
    44             f.write('baz = 1')
       
    45         sys.path.append(libdir)
       
    46         yield cubedir
       
    47     finally:
       
    48         shutil.rmtree(tempdir)
       
    49         sys.path.remove(libdir)
       
    50 
       
    51 
       
    52 class CubesImporterTC(unittest.TestCase):
       
    53 
       
    54     def setUp(self):
       
    55         # During discovery, CubicWebConfiguration.cls_adjust_sys_path may be
       
    56         # called (probably because of cubicweb.devtools's __init__.py), so
       
    57         # uninstall _CubesImporter.
       
    58         for x in sys.meta_path:
       
    59             if isinstance(x, _CubesImporter):
       
    60                 sys.meta_path.remove(x)
       
    61         # Keep track of initial sys.path and sys.meta_path.
       
    62         self.orig_sys_path = sys.path[:]
       
    63         self.orig_sys_meta_path = sys.meta_path[:]
       
    64 
       
    65     def tearDown(self):
       
    66         # Cleanup any imported "cubes".
       
    67         for name in list(sys.modules):
       
    68             if name.startswith('cubes') or name.startswith('cubicweb_'):
       
    69                 del sys.modules[name]
       
    70         # Restore sys.{meta_,}path
       
    71         sys.path[:] = self.orig_sys_path
       
    72         sys.meta_path[:] = self.orig_sys_meta_path
       
    73 
       
    74     def test_importer_install(self):
       
    75         _CubesImporter.install()
       
    76         self.assertIsInstance(sys.meta_path[-1], _CubesImporter)
       
    77 
       
    78     def test_config_installs_importer(self):
       
    79         CubicWebConfiguration.cls_adjust_sys_path()
       
    80         self.assertIsInstance(sys.meta_path[-1], _CubesImporter)
       
    81 
       
    82     def test_import_cube_as_package_legacy_name(self):
       
    83         """Check for import of an actual package-cube using legacy name"""
       
    84         with temp_cube() as cubedir:
       
    85             import cubicweb_foo  # noqa
       
    86             del sys.modules['cubicweb_foo']
       
    87             with self.assertRaises(ImportError):
       
    88                 import cubes.foo
       
    89             CubicWebConfiguration.cls_adjust_sys_path()
       
    90             import cubes.foo  # noqa
       
    91             self.assertEqual(cubes.foo.__path__, [cubedir])
       
    92             self.assertEqual(cubes.foo.__doc__,
       
    93                              'cubicweb_foo application package')
       
    94             # Import a submodule.
       
    95             from cubes.foo import bar
       
    96             self.assertEqual(bar.baz, 1)
       
    97 
       
    98     def test_import_legacy_cube(self):
       
    99         """Check that importing a legacy cube works when sys.path got adjusted.
       
   100         """
       
   101         CubicWebConfiguration.cls_adjust_sys_path()
       
   102         import cubes.card  # noqa
       
   103 
       
   104     def test_import_cube_as_package_after_legacy_cube(self):
       
   105         """Check import of a "cube as package" after a legacy cube."""
       
   106         CubicWebConfiguration.cls_adjust_sys_path()
       
   107         with temp_cube() as cubedir:
       
   108             import cubes.card
       
   109             import cubes.foo
       
   110         self.assertEqual(cubes.foo.__path__, [cubedir])
       
   111 
       
   112     def test_cube_inexistant(self):
       
   113         """Check for import of an inexistant cube"""
       
   114         CubicWebConfiguration.cls_adjust_sys_path()
       
   115         with self.assertRaises(ImportError) as cm:
       
   116             import cubes.doesnotexists  # noqa
       
   117         msg = "No module named " + ("doesnotexists" if PY2 else "'cubes.doesnotexists'")
       
   118         self.assertEqual(str(cm.exception), msg)
       
   119 
       
   120 
       
   121 if __name__ == '__main__':
       
   122     import unittest
       
   123     unittest.main()