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 sys |
|
24 |
|
25 from six import PY2 |
|
26 |
|
27 from cubicweb import _CubesImporter |
|
28 from cubicweb.cwconfig import CubicWebConfiguration |
|
29 from cubicweb.devtools.testlib import TemporaryDirectory, TestCase |
|
30 |
|
31 |
|
32 @contextmanager |
|
33 def temp_cube(): |
|
34 with TemporaryDirectory() as tempdir: |
|
35 try: |
|
36 libdir = path.join(tempdir, 'libpython') |
|
37 cubedir = path.join(libdir, 'cubicweb_foo') |
|
38 os.makedirs(cubedir) |
|
39 check_code = ("import logging\n" |
|
40 "logging.getLogger('cubicweb_foo')" |
|
41 ".warn('imported %s', __name__)\n") |
|
42 with open(path.join(cubedir, '__init__.py'), 'w') as f: |
|
43 f.write("'cubicweb_foo application package'\n" + check_code) |
|
44 with open(path.join(cubedir, 'bar.py'), 'w') as f: |
|
45 f.write(check_code + 'baz = 1\n') |
|
46 sys.path.append(libdir) |
|
47 yield cubedir |
|
48 finally: |
|
49 sys.path.remove(libdir) |
|
50 |
|
51 |
|
52 class CubesImporterTC(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_reload_cube(self): |
|
99 """reloading cubes twice should return the same module""" |
|
100 CubicWebConfiguration.cls_adjust_sys_path() |
|
101 import cubes |
|
102 if PY2: |
|
103 new = reload(cubes) |
|
104 else: |
|
105 import importlib |
|
106 new = importlib.reload(cubes) |
|
107 self.assertIs(new, cubes) |
|
108 |
|
109 def test_no_double_import(self): |
|
110 """Check new and legacy import the same module once""" |
|
111 with temp_cube(): |
|
112 CubicWebConfiguration.cls_adjust_sys_path() |
|
113 with self.assertLogs('cubicweb_foo', 'WARNING') as cm: |
|
114 from cubes.foo import bar |
|
115 from cubicweb_foo import bar as bar2 |
|
116 self.assertIs(bar, bar2) |
|
117 self.assertIs(sys.modules['cubes.foo'], |
|
118 sys.modules['cubicweb_foo']) |
|
119 self.assertEqual(cm.output, [ |
|
120 'WARNING:cubicweb_foo:imported cubicweb_foo', |
|
121 # module __name__ for subpackage differ along python version |
|
122 # for PY2 it's based on how the module was imported "from |
|
123 # cubes.foo import bar" and for PY3 based on __name__ of parent |
|
124 # module "cubicweb_foo". Not sure if it's an issue, but PY3 |
|
125 # behavior looks better. |
|
126 'WARNING:cubicweb_foo:imported ' + ( |
|
127 'cubes.foo.bar' if PY2 else 'cubicweb_foo.bar') |
|
128 ]) |
|
129 |
|
130 def test_import_legacy_cube(self): |
|
131 """Check that importing a legacy cube works when sys.path got adjusted. |
|
132 """ |
|
133 CubicWebConfiguration.cls_adjust_sys_path() |
|
134 import cubes.card # noqa |
|
135 |
|
136 def test_import_cube_as_package_after_legacy_cube(self): |
|
137 """Check import of a "cube as package" after a legacy cube.""" |
|
138 CubicWebConfiguration.cls_adjust_sys_path() |
|
139 with temp_cube() as cubedir: |
|
140 import cubes.card |
|
141 import cubes.foo |
|
142 self.assertEqual(cubes.foo.__path__, [cubedir]) |
|
143 |
|
144 def test_cube_inexistant(self): |
|
145 """Check for import of an inexistant cube""" |
|
146 CubicWebConfiguration.cls_adjust_sys_path() |
|
147 with self.assertRaises(ImportError) as cm: |
|
148 import cubes.doesnotexists # noqa |
|
149 msg = "No module named " + ("doesnotexists" if PY2 else "'cubes.doesnotexists'") |
|
150 self.assertEqual(str(cm.exception), msg) |
|
151 |
|
152 |
|
153 if __name__ == '__main__': |
|
154 import unittest |
|
155 unittest.main() |
|