2010-05-04 14:14:47 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2011-04-22 20:03:18 +08:00
|
|
|
import imp
|
2010-05-04 14:14:47 +08:00
|
|
|
from zipimport import zipimporter
|
|
|
|
|
2013-02-03 05:58:02 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
2010-05-04 14:14:47 +08:00
|
|
|
from django.utils.importlib import import_module
|
2013-02-03 05:58:02 +08:00
|
|
|
from django.utils.module_loading import import_by_path, module_has_submodule
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2010-05-04 14:14:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2010-09-28 16:18:12 +08:00
|
|
|
class DefaultLoader(unittest.TestCase):
|
2011-04-22 20:03:18 +08:00
|
|
|
def setUp(self):
|
|
|
|
sys.meta_path.insert(0, ProxyFinder())
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.meta_path.pop(0)
|
|
|
|
|
2010-05-04 14:14:47 +08:00
|
|
|
def test_loader(self):
|
|
|
|
"Normal module existence can be tested"
|
2013-02-26 20:19:18 +08:00
|
|
|
test_module = import_module('utils_tests.test_module')
|
2011-08-28 10:37:16 +08:00
|
|
|
test_no_submodule = import_module(
|
2013-02-26 20:19:18 +08:00
|
|
|
'utils_tests.test_no_submodule')
|
2010-05-04 14:14:47 +08:00
|
|
|
|
|
|
|
# An importable child
|
|
|
|
self.assertTrue(module_has_submodule(test_module, 'good_module'))
|
2013-02-26 20:19:18 +08:00
|
|
|
mod = import_module('utils_tests.test_module.good_module')
|
2010-05-04 14:14:47 +08:00
|
|
|
self.assertEqual(mod.content, 'Good Module')
|
|
|
|
|
|
|
|
# A child that exists, but will generate an import error if loaded
|
|
|
|
self.assertTrue(module_has_submodule(test_module, 'bad_module'))
|
2013-02-26 20:19:18 +08:00
|
|
|
self.assertRaises(ImportError, import_module, 'utils_tests.test_module.bad_module')
|
2010-05-04 14:14:47 +08:00
|
|
|
|
|
|
|
# A child that doesn't exist
|
|
|
|
self.assertFalse(module_has_submodule(test_module, 'no_such_module'))
|
2013-02-26 20:19:18 +08:00
|
|
|
self.assertRaises(ImportError, import_module, 'utils_tests.test_module.no_such_module')
|
2010-05-04 14:14:47 +08:00
|
|
|
|
2011-04-22 20:03:18 +08:00
|
|
|
# A child that doesn't exist, but is the name of a package on the path
|
|
|
|
self.assertFalse(module_has_submodule(test_module, 'django'))
|
2013-02-26 20:19:18 +08:00
|
|
|
self.assertRaises(ImportError, import_module, 'utils_tests.test_module.django')
|
2011-04-22 20:03:18 +08:00
|
|
|
|
2011-01-30 21:10:47 +08:00
|
|
|
# Don't be confused by caching of import misses
|
2013-02-26 20:19:18 +08:00
|
|
|
import types # causes attempted import of utils_tests.types
|
|
|
|
self.assertFalse(module_has_submodule(sys.modules['utils_tests'], 'types'))
|
2011-01-30 21:10:47 +08:00
|
|
|
|
2011-08-28 10:37:16 +08:00
|
|
|
# A module which doesn't have a __path__ (so no submodules)
|
|
|
|
self.assertFalse(module_has_submodule(test_no_submodule, 'anything'))
|
|
|
|
self.assertRaises(ImportError, import_module,
|
2013-02-26 20:19:18 +08:00
|
|
|
'utils_tests.test_no_submodule.anything')
|
2011-08-28 10:37:16 +08:00
|
|
|
|
2010-09-28 16:18:12 +08:00
|
|
|
class EggLoader(unittest.TestCase):
|
2010-05-04 14:14:47 +08:00
|
|
|
def setUp(self):
|
2010-11-02 13:31:19 +08:00
|
|
|
self.old_path = sys.path[:]
|
2012-12-08 18:13:52 +08:00
|
|
|
self.egg_dir = '%s/eggs' % os.path.dirname(upath(__file__))
|
2010-05-04 14:14:47 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.path = self.old_path
|
2010-05-04 22:48:43 +08:00
|
|
|
sys.path_importer_cache.clear()
|
|
|
|
|
|
|
|
sys.modules.pop('egg_module.sub1.sub2.bad_module', None)
|
|
|
|
sys.modules.pop('egg_module.sub1.sub2.good_module', None)
|
|
|
|
sys.modules.pop('egg_module.sub1.sub2', None)
|
|
|
|
sys.modules.pop('egg_module.sub1', None)
|
|
|
|
sys.modules.pop('egg_module.bad_module', None)
|
|
|
|
sys.modules.pop('egg_module.good_module', None)
|
|
|
|
sys.modules.pop('egg_module', None)
|
2010-05-04 14:14:47 +08:00
|
|
|
|
|
|
|
def test_shallow_loader(self):
|
|
|
|
"Module existence can be tested inside eggs"
|
|
|
|
egg_name = '%s/test_egg.egg' % self.egg_dir
|
|
|
|
sys.path.append(egg_name)
|
|
|
|
egg_module = import_module('egg_module')
|
|
|
|
|
|
|
|
# An importable child
|
|
|
|
self.assertTrue(module_has_submodule(egg_module, 'good_module'))
|
|
|
|
mod = import_module('egg_module.good_module')
|
|
|
|
self.assertEqual(mod.content, 'Good Module')
|
|
|
|
|
|
|
|
# A child that exists, but will generate an import error if loaded
|
|
|
|
self.assertTrue(module_has_submodule(egg_module, 'bad_module'))
|
|
|
|
self.assertRaises(ImportError, import_module, 'egg_module.bad_module')
|
|
|
|
|
|
|
|
# A child that doesn't exist
|
|
|
|
self.assertFalse(module_has_submodule(egg_module, 'no_such_module'))
|
|
|
|
self.assertRaises(ImportError, import_module, 'egg_module.no_such_module')
|
|
|
|
|
|
|
|
def test_deep_loader(self):
|
|
|
|
"Modules deep inside an egg can still be tested for existence"
|
|
|
|
egg_name = '%s/test_egg.egg' % self.egg_dir
|
|
|
|
sys.path.append(egg_name)
|
|
|
|
egg_module = import_module('egg_module.sub1.sub2')
|
|
|
|
|
|
|
|
# An importable child
|
|
|
|
self.assertTrue(module_has_submodule(egg_module, 'good_module'))
|
|
|
|
mod = import_module('egg_module.sub1.sub2.good_module')
|
|
|
|
self.assertEqual(mod.content, 'Deep Good Module')
|
|
|
|
|
|
|
|
# A child that exists, but will generate an import error if loaded
|
|
|
|
self.assertTrue(module_has_submodule(egg_module, 'bad_module'))
|
|
|
|
self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.bad_module')
|
|
|
|
|
|
|
|
# A child that doesn't exist
|
|
|
|
self.assertFalse(module_has_submodule(egg_module, 'no_such_module'))
|
|
|
|
self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.no_such_module')
|
|
|
|
|
2013-02-03 05:58:02 +08:00
|
|
|
|
|
|
|
class ModuleImportTestCase(unittest.TestCase):
|
|
|
|
def test_import_by_path(self):
|
|
|
|
cls = import_by_path(
|
|
|
|
'django.utils.module_loading.import_by_path')
|
|
|
|
self.assertEqual(cls, import_by_path)
|
|
|
|
|
|
|
|
# Test exceptions raised
|
|
|
|
for path in ('no_dots_in_path', 'unexistent.path',
|
2013-03-06 02:13:36 +08:00
|
|
|
'utils_tests.unexistent'):
|
2013-02-03 05:58:02 +08:00
|
|
|
self.assertRaises(ImproperlyConfigured, import_by_path, path)
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured) as cm:
|
|
|
|
import_by_path('unexistent.module.path', error_prefix="Foo")
|
|
|
|
self.assertTrue(str(cm.exception).startswith('Foo'))
|
|
|
|
|
|
|
|
|
2011-04-22 20:03:18 +08:00
|
|
|
class ProxyFinder(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._cache = {}
|
|
|
|
|
|
|
|
def find_module(self, fullname, path=None):
|
|
|
|
tail = fullname.rsplit('.', 1)[-1]
|
|
|
|
try:
|
2012-08-15 17:47:02 +08:00
|
|
|
fd, fn, info = imp.find_module(tail, path)
|
|
|
|
if fullname in self._cache:
|
|
|
|
old_fd = self._cache[fullname][0]
|
|
|
|
if old_fd:
|
|
|
|
old_fd.close()
|
|
|
|
self._cache[fullname] = (fd, fn, info)
|
2011-04-22 20:03:18 +08:00
|
|
|
except ImportError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self # this is a loader as well
|
|
|
|
|
|
|
|
def load_module(self, fullname):
|
|
|
|
if fullname in sys.modules:
|
|
|
|
return sys.modules[fullname]
|
|
|
|
fd, fn, info = self._cache[fullname]
|
2012-08-15 17:47:02 +08:00
|
|
|
try:
|
|
|
|
return imp.load_module(fullname, fd, fn, info)
|
|
|
|
finally:
|
|
|
|
if fd:
|
|
|
|
fd.close()
|
2011-04-22 20:03:18 +08:00
|
|
|
|
2010-05-04 14:14:47 +08:00
|
|
|
class TestFinder(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.importer = zipimporter(*args, **kwargs)
|
|
|
|
|
|
|
|
def find_module(self, path):
|
|
|
|
importer = self.importer.find_module(path)
|
|
|
|
if importer is None:
|
|
|
|
return
|
|
|
|
return TestLoader(importer)
|
|
|
|
|
|
|
|
class TestLoader(object):
|
|
|
|
def __init__(self, importer):
|
|
|
|
self.importer = importer
|
|
|
|
|
|
|
|
def load_module(self, name):
|
|
|
|
mod = self.importer.load_module(name)
|
|
|
|
mod.__loader__ = self
|
|
|
|
return mod
|
|
|
|
|
2010-05-04 22:48:43 +08:00
|
|
|
class CustomLoader(EggLoader):
|
|
|
|
"""The Custom Loader test is exactly the same as the EggLoader, but
|
|
|
|
it uses a custom defined Loader and Finder that is intentionally
|
|
|
|
split into two classes. Although the EggLoader combines both functions
|
|
|
|
into one class, this isn't required.
|
|
|
|
"""
|
2010-05-04 14:14:47 +08:00
|
|
|
def setUp(self):
|
2010-05-04 22:48:43 +08:00
|
|
|
super(CustomLoader, self).setUp()
|
2010-05-04 14:14:47 +08:00
|
|
|
sys.path_hooks.insert(0, TestFinder)
|
|
|
|
sys.path_importer_cache.clear()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2010-05-04 22:48:43 +08:00
|
|
|
super(CustomLoader, self).tearDown()
|
2010-05-04 14:14:47 +08:00
|
|
|
sys.path_hooks.pop(0)
|