summaryrefslogtreecommitdiff
path: root/tests/regressiontests/utils/module_loading.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-05-04 08:15:34 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-05-04 08:15:34 +0000
commitbc9708735fd1e3f3940ebc013f37f2b1b2594910 (patch)
tree8026858c1608ff8ba597fdc9dd95dc0e19460820 /tests/regressiontests/utils/module_loading.py
parenta263d3b445072a78e140a9b5d75f793532a13ee1 (diff)
[1.1.X] Fixed #13464 -- Reworked module_has_submodule to break the requirement for loader and finder to be the same class. Thanks to Alex Gaynor for the report and patch, and Brett Cannon for suggesting the approach.
Backport of r13082 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@13085 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/utils/module_loading.py')
-rw-r--r--tests/regressiontests/utils/module_loading.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/tests/regressiontests/utils/module_loading.py b/tests/regressiontests/utils/module_loading.py
new file mode 100644
index 0000000000..7b233f8e14
--- /dev/null
+++ b/tests/regressiontests/utils/module_loading.py
@@ -0,0 +1,139 @@
+import os
+import sys
+from unittest import TestCase
+from zipimport import zipimporter
+
+from django.utils.importlib import import_module
+from django.utils.module_loading import module_has_submodule
+
+class DefaultLoader(TestCase):
+ def test_loader(self):
+ "Normal module existence can be tested"
+ test_module = import_module('regressiontests.utils.test_module')
+
+ # An importable child
+ self.assertTrue(module_has_submodule(test_module, 'good_module'))
+ mod = import_module('regressiontests.utils.test_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(test_module, 'bad_module'))
+ self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.bad_module')
+
+ # A child that doesn't exist
+ self.assertFalse(module_has_submodule(test_module, 'no_such_module'))
+ self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module')
+
+class EggLoader(TestCase):
+ def setUp(self):
+ self.old_path = sys.path
+ self.egg_dir = '%s/eggs' % os.path.dirname(__file__)
+
+ def tearDown(self):
+ sys.path = self.old_path
+
+ 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')
+
+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
+
+class CustomLoader(TestCase):
+ def setUp(self):
+ self.egg_dir = '%s/eggs' % os.path.dirname(__file__)
+ self.old_path = sys.path
+ sys.path_hooks.insert(0, TestFinder)
+ sys.path_importer_cache.clear()
+
+ def tearDown(self):
+ sys.path = self.old_path
+ sys.path_hooks.pop(0)
+
+ def test_shallow_loader(self):
+ "Module existence can be tested with a custom loader"
+ 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 existence can be tested deep inside a custom loader"
+ 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')