| 1 | n/a | import contextlib |
|---|
| 2 | n/a | import importlib |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import sys |
|---|
| 5 | n/a | import unittest |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from test.test_importlib import util |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | # needed tests: |
|---|
| 10 | n/a | # |
|---|
| 11 | n/a | # need to test when nested, so that the top-level path isn't sys.path |
|---|
| 12 | n/a | # need to test dynamic path detection, both at top-level and nested |
|---|
| 13 | n/a | # with dynamic path, check when a loader is returned on path reload (that is, |
|---|
| 14 | n/a | # trying to switch from a namespace package to a regular package) |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | @contextlib.contextmanager |
|---|
| 18 | n/a | def sys_modules_context(): |
|---|
| 19 | n/a | """ |
|---|
| 20 | n/a | Make sure sys.modules is the same object and has the same content |
|---|
| 21 | n/a | when exiting the context as when entering. |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | Similar to importlib.test.util.uncache, but doesn't require explicit |
|---|
| 24 | n/a | names. |
|---|
| 25 | n/a | """ |
|---|
| 26 | n/a | sys_modules_saved = sys.modules |
|---|
| 27 | n/a | sys_modules_copy = sys.modules.copy() |
|---|
| 28 | n/a | try: |
|---|
| 29 | n/a | yield |
|---|
| 30 | n/a | finally: |
|---|
| 31 | n/a | sys.modules = sys_modules_saved |
|---|
| 32 | n/a | sys.modules.clear() |
|---|
| 33 | n/a | sys.modules.update(sys_modules_copy) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | @contextlib.contextmanager |
|---|
| 37 | n/a | def namespace_tree_context(**kwargs): |
|---|
| 38 | n/a | """ |
|---|
| 39 | n/a | Save import state and sys.modules cache and restore it on exit. |
|---|
| 40 | n/a | Typical usage: |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | >>> with namespace_tree_context(path=['/tmp/xxyy/portion1', |
|---|
| 43 | n/a | ... '/tmp/xxyy/portion2']): |
|---|
| 44 | n/a | ... pass |
|---|
| 45 | n/a | """ |
|---|
| 46 | n/a | # use default meta_path and path_hooks unless specified otherwise |
|---|
| 47 | n/a | kwargs.setdefault('meta_path', sys.meta_path) |
|---|
| 48 | n/a | kwargs.setdefault('path_hooks', sys.path_hooks) |
|---|
| 49 | n/a | import_context = util.import_state(**kwargs) |
|---|
| 50 | n/a | with import_context, sys_modules_context(): |
|---|
| 51 | n/a | yield |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | class NamespacePackageTest(unittest.TestCase): |
|---|
| 54 | n/a | """ |
|---|
| 55 | n/a | Subclasses should define self.root and self.paths (under that root) |
|---|
| 56 | n/a | to be added to sys.path. |
|---|
| 57 | n/a | """ |
|---|
| 58 | n/a | root = os.path.join(os.path.dirname(__file__), 'namespace_pkgs') |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | def setUp(self): |
|---|
| 61 | n/a | self.resolved_paths = [ |
|---|
| 62 | n/a | os.path.join(self.root, path) for path in self.paths |
|---|
| 63 | n/a | ] |
|---|
| 64 | n/a | self.ctx = namespace_tree_context(path=self.resolved_paths) |
|---|
| 65 | n/a | self.ctx.__enter__() |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def tearDown(self): |
|---|
| 68 | n/a | # TODO: will we ever want to pass exc_info to __exit__? |
|---|
| 69 | n/a | self.ctx.__exit__(None, None, None) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | class SingleNamespacePackage(NamespacePackageTest): |
|---|
| 73 | n/a | paths = ['portion1'] |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | def test_simple_package(self): |
|---|
| 76 | n/a | import foo.one |
|---|
| 77 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def test_cant_import_other(self): |
|---|
| 80 | n/a | with self.assertRaises(ImportError): |
|---|
| 81 | n/a | import foo.two |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def test_module_repr(self): |
|---|
| 84 | n/a | import foo.one |
|---|
| 85 | n/a | self.assertEqual(repr(foo), "<module 'foo' (namespace)>") |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | class DynamicPathNamespacePackage(NamespacePackageTest): |
|---|
| 89 | n/a | paths = ['portion1'] |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def test_dynamic_path(self): |
|---|
| 92 | n/a | # Make sure only 'foo.one' can be imported |
|---|
| 93 | n/a | import foo.one |
|---|
| 94 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | with self.assertRaises(ImportError): |
|---|
| 97 | n/a | import foo.two |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | # Now modify sys.path |
|---|
| 100 | n/a | sys.path.append(os.path.join(self.root, 'portion2')) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | # And make sure foo.two is now importable |
|---|
| 103 | n/a | import foo.two |
|---|
| 104 | n/a | self.assertEqual(foo.two.attr, 'portion2 foo two') |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | class CombinedNamespacePackages(NamespacePackageTest): |
|---|
| 108 | n/a | paths = ['both_portions'] |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def test_imports(self): |
|---|
| 111 | n/a | import foo.one |
|---|
| 112 | n/a | import foo.two |
|---|
| 113 | n/a | self.assertEqual(foo.one.attr, 'both_portions foo one') |
|---|
| 114 | n/a | self.assertEqual(foo.two.attr, 'both_portions foo two') |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | class SeparatedNamespacePackages(NamespacePackageTest): |
|---|
| 118 | n/a | paths = ['portion1', 'portion2'] |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def test_imports(self): |
|---|
| 121 | n/a | import foo.one |
|---|
| 122 | n/a | import foo.two |
|---|
| 123 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 124 | n/a | self.assertEqual(foo.two.attr, 'portion2 foo two') |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | class SeparatedOverlappingNamespacePackages(NamespacePackageTest): |
|---|
| 128 | n/a | paths = ['portion1', 'both_portions'] |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | def test_first_path_wins(self): |
|---|
| 131 | n/a | import foo.one |
|---|
| 132 | n/a | import foo.two |
|---|
| 133 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 134 | n/a | self.assertEqual(foo.two.attr, 'both_portions foo two') |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def test_first_path_wins_again(self): |
|---|
| 137 | n/a | sys.path.reverse() |
|---|
| 138 | n/a | import foo.one |
|---|
| 139 | n/a | import foo.two |
|---|
| 140 | n/a | self.assertEqual(foo.one.attr, 'both_portions foo one') |
|---|
| 141 | n/a | self.assertEqual(foo.two.attr, 'both_portions foo two') |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | def test_first_path_wins_importing_second_first(self): |
|---|
| 144 | n/a | import foo.two |
|---|
| 145 | n/a | import foo.one |
|---|
| 146 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 147 | n/a | self.assertEqual(foo.two.attr, 'both_portions foo two') |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | class SingleZipNamespacePackage(NamespacePackageTest): |
|---|
| 151 | n/a | paths = ['top_level_portion1.zip'] |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def test_simple_package(self): |
|---|
| 154 | n/a | import foo.one |
|---|
| 155 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | def test_cant_import_other(self): |
|---|
| 158 | n/a | with self.assertRaises(ImportError): |
|---|
| 159 | n/a | import foo.two |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | class SeparatedZipNamespacePackages(NamespacePackageTest): |
|---|
| 163 | n/a | paths = ['top_level_portion1.zip', 'portion2'] |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | def test_imports(self): |
|---|
| 166 | n/a | import foo.one |
|---|
| 167 | n/a | import foo.two |
|---|
| 168 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 169 | n/a | self.assertEqual(foo.two.attr, 'portion2 foo two') |
|---|
| 170 | n/a | self.assertIn('top_level_portion1.zip', foo.one.__file__) |
|---|
| 171 | n/a | self.assertNotIn('.zip', foo.two.__file__) |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | class SingleNestedZipNamespacePackage(NamespacePackageTest): |
|---|
| 175 | n/a | paths = ['nested_portion1.zip/nested_portion1'] |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | def test_simple_package(self): |
|---|
| 178 | n/a | import foo.one |
|---|
| 179 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | def test_cant_import_other(self): |
|---|
| 182 | n/a | with self.assertRaises(ImportError): |
|---|
| 183 | n/a | import foo.two |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | class SeparatedNestedZipNamespacePackages(NamespacePackageTest): |
|---|
| 187 | n/a | paths = ['nested_portion1.zip/nested_portion1', 'portion2'] |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | def test_imports(self): |
|---|
| 190 | n/a | import foo.one |
|---|
| 191 | n/a | import foo.two |
|---|
| 192 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 193 | n/a | self.assertEqual(foo.two.attr, 'portion2 foo two') |
|---|
| 194 | n/a | fn = os.path.join('nested_portion1.zip', 'nested_portion1') |
|---|
| 195 | n/a | self.assertIn(fn, foo.one.__file__) |
|---|
| 196 | n/a | self.assertNotIn('.zip', foo.two.__file__) |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | class LegacySupport(NamespacePackageTest): |
|---|
| 200 | n/a | paths = ['not_a_namespace_pkg', 'portion1', 'portion2', 'both_portions'] |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | def test_non_namespace_package_takes_precedence(self): |
|---|
| 203 | n/a | import foo.one |
|---|
| 204 | n/a | with self.assertRaises(ImportError): |
|---|
| 205 | n/a | import foo.two |
|---|
| 206 | n/a | self.assertIn('__init__', foo.__file__) |
|---|
| 207 | n/a | self.assertNotIn('namespace', str(foo.__loader__).lower()) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | class DynamicPathCalculation(NamespacePackageTest): |
|---|
| 211 | n/a | paths = ['project1', 'project2'] |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def test_project3_fails(self): |
|---|
| 214 | n/a | import parent.child.one |
|---|
| 215 | n/a | self.assertEqual(len(parent.__path__), 2) |
|---|
| 216 | n/a | self.assertEqual(len(parent.child.__path__), 2) |
|---|
| 217 | n/a | import parent.child.two |
|---|
| 218 | n/a | self.assertEqual(len(parent.__path__), 2) |
|---|
| 219 | n/a | self.assertEqual(len(parent.child.__path__), 2) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | self.assertEqual(parent.child.one.attr, 'parent child one') |
|---|
| 222 | n/a | self.assertEqual(parent.child.two.attr, 'parent child two') |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | with self.assertRaises(ImportError): |
|---|
| 225 | n/a | import parent.child.three |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | self.assertEqual(len(parent.__path__), 2) |
|---|
| 228 | n/a | self.assertEqual(len(parent.child.__path__), 2) |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def test_project3_succeeds(self): |
|---|
| 231 | n/a | import parent.child.one |
|---|
| 232 | n/a | self.assertEqual(len(parent.__path__), 2) |
|---|
| 233 | n/a | self.assertEqual(len(parent.child.__path__), 2) |
|---|
| 234 | n/a | import parent.child.two |
|---|
| 235 | n/a | self.assertEqual(len(parent.__path__), 2) |
|---|
| 236 | n/a | self.assertEqual(len(parent.child.__path__), 2) |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | self.assertEqual(parent.child.one.attr, 'parent child one') |
|---|
| 239 | n/a | self.assertEqual(parent.child.two.attr, 'parent child two') |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | with self.assertRaises(ImportError): |
|---|
| 242 | n/a | import parent.child.three |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | # now add project3 |
|---|
| 245 | n/a | sys.path.append(os.path.join(self.root, 'project3')) |
|---|
| 246 | n/a | import parent.child.three |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | # the paths dynamically get longer, to include the new directories |
|---|
| 249 | n/a | self.assertEqual(len(parent.__path__), 3) |
|---|
| 250 | n/a | self.assertEqual(len(parent.child.__path__), 3) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | self.assertEqual(parent.child.three.attr, 'parent child three') |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | class ZipWithMissingDirectory(NamespacePackageTest): |
|---|
| 256 | n/a | paths = ['missing_directory.zip'] |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | @unittest.expectedFailure |
|---|
| 259 | n/a | def test_missing_directory(self): |
|---|
| 260 | n/a | # This will fail because missing_directory.zip contains: |
|---|
| 261 | n/a | # Length Date Time Name |
|---|
| 262 | n/a | # --------- ---------- ----- ---- |
|---|
| 263 | n/a | # 29 2012-05-03 18:13 foo/one.py |
|---|
| 264 | n/a | # 0 2012-05-03 20:57 bar/ |
|---|
| 265 | n/a | # 38 2012-05-03 20:57 bar/two.py |
|---|
| 266 | n/a | # --------- ------- |
|---|
| 267 | n/a | # 67 3 files |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | # Because there is no 'foo/', the zipimporter currently doesn't |
|---|
| 270 | n/a | # know that foo is a namespace package |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | import foo.one |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | def test_present_directory(self): |
|---|
| 275 | n/a | # This succeeds because there is a "bar/" in the zip file |
|---|
| 276 | n/a | import bar.two |
|---|
| 277 | n/a | self.assertEqual(bar.two.attr, 'missing_directory foo two') |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | class ModuleAndNamespacePackageInSameDir(NamespacePackageTest): |
|---|
| 281 | n/a | paths = ['module_and_namespace_package'] |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def test_module_before_namespace_package(self): |
|---|
| 284 | n/a | # Make sure we find the module in preference to the |
|---|
| 285 | n/a | # namespace package. |
|---|
| 286 | n/a | import a_test |
|---|
| 287 | n/a | self.assertEqual(a_test.attr, 'in module') |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | class ReloadTests(NamespacePackageTest): |
|---|
| 291 | n/a | paths = ['portion1'] |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | def test_simple_package(self): |
|---|
| 294 | n/a | import foo.one |
|---|
| 295 | n/a | foo = importlib.reload(foo) |
|---|
| 296 | n/a | self.assertEqual(foo.one.attr, 'portion1 foo one') |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | def test_cant_import_other(self): |
|---|
| 299 | n/a | import foo |
|---|
| 300 | n/a | with self.assertRaises(ImportError): |
|---|
| 301 | n/a | import foo.two |
|---|
| 302 | n/a | foo = importlib.reload(foo) |
|---|
| 303 | n/a | with self.assertRaises(ImportError): |
|---|
| 304 | n/a | import foo.two |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def test_dynamic_path(self): |
|---|
| 307 | n/a | import foo.one |
|---|
| 308 | n/a | with self.assertRaises(ImportError): |
|---|
| 309 | n/a | import foo.two |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | # Now modify sys.path and reload. |
|---|
| 312 | n/a | sys.path.append(os.path.join(self.root, 'portion2')) |
|---|
| 313 | n/a | foo = importlib.reload(foo) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | # And make sure foo.two is now importable |
|---|
| 316 | n/a | import foo.two |
|---|
| 317 | n/a | self.assertEqual(foo.two.attr, 'portion2 foo two') |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | if __name__ == "__main__": |
|---|
| 321 | n/a | unittest.main() |
|---|