2010-05-04 06:14:47 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2015-01-28 12:35:27 +00:00
|
|
|
from importlib import import_module
|
2010-05-04 06:14:47 +00:00
|
|
|
from zipimport import zipimporter
|
|
|
|
|
2018-11-26 19:05:02 +00:00
|
|
|
from django.test import SimpleTestCase, modify_settings
|
2014-12-21 20:19:05 +00:00
|
|
|
from django.test.utils import extend_sys_path
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.module_loading import (
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules,
|
|
|
|
import_string,
|
|
|
|
module_has_submodule,
|
2015-01-28 12:35:27 +00:00
|
|
|
)
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2010-10-11 12:55:17 +00:00
|
|
|
|
2010-09-28 08:18:12 +00:00
|
|
|
class DefaultLoader(unittest.TestCase):
|
2010-05-04 06:14:47 +00:00
|
|
|
def test_loader(self):
|
|
|
|
"Normal module existence can be tested"
|
2022-02-03 19:24:19 +00:00
|
|
|
test_module = import_module("utils_tests.test_module")
|
|
|
|
test_no_submodule = import_module("utils_tests.test_no_submodule")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
# An importable child
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(module_has_submodule(test_module, "good_module"))
|
|
|
|
mod = import_module("utils_tests.test_module.good_module")
|
|
|
|
self.assertEqual(mod.content, "Good Module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
# A child that exists, but will generate an import error if loaded
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(module_has_submodule(test_module, "bad_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("utils_tests.test_module.bad_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
# A child that doesn't exist
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(module_has_submodule(test_module, "no_such_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("utils_tests.test_module.no_such_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2011-04-22 12:03:18 +00:00
|
|
|
# A child that doesn't exist, but is the name of a package on the path
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(module_has_submodule(test_module, "django"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("utils_tests.test_module.django")
|
2011-04-22 12:03:18 +00:00
|
|
|
|
2011-01-30 13:10:47 +00:00
|
|
|
# Don't be confused by caching of import misses
|
2013-10-18 11:25:30 +00:00
|
|
|
import types # NOQA: causes attempted import of utils_tests.types
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
self.assertFalse(module_has_submodule(sys.modules["utils_tests"], "types"))
|
2011-01-30 13:10:47 +00:00
|
|
|
|
2011-08-28 02:37:16 +00:00
|
|
|
# A module which doesn't have a __path__ (so no submodules)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(module_has_submodule(test_no_submodule, "anything"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("utils_tests.test_no_submodule.anything")
|
2011-08-28 02:37:16 +00:00
|
|
|
|
2017-06-08 18:34:20 +00:00
|
|
|
def test_has_sumbodule_with_dotted_path(self):
|
|
|
|
"""Nested module existence can be tested."""
|
2022-02-03 19:24:19 +00:00
|
|
|
test_module = import_module("utils_tests.test_module")
|
2017-06-08 18:34:20 +00:00
|
|
|
# A grandchild that exists.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(
|
|
|
|
module_has_submodule(test_module, "child_module.grandchild_module"), True
|
|
|
|
)
|
2017-06-08 18:34:20 +00:00
|
|
|
# A grandchild that doesn't exist.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(
|
|
|
|
module_has_submodule(test_module, "child_module.no_such_module"), False
|
|
|
|
)
|
2017-06-08 18:34:20 +00:00
|
|
|
# A grandchild whose parent doesn't exist.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(
|
|
|
|
module_has_submodule(test_module, "no_such_module.grandchild_module"), False
|
|
|
|
)
|
2017-06-08 18:34:20 +00:00
|
|
|
# A grandchild whose parent is not a package.
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIs(
|
|
|
|
module_has_submodule(test_module, "good_module.no_such_module"), False
|
|
|
|
)
|
2017-06-08 18:34:20 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-09-28 08:18:12 +00:00
|
|
|
class EggLoader(unittest.TestCase):
|
2010-05-04 06:14:47 +00:00
|
|
|
def setUp(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
self.egg_dir = "%s/eggs" % os.path.dirname(__file__)
|
2010-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2010-05-04 14:48:43 +00:00
|
|
|
sys.path_importer_cache.clear()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
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 06:14:47 +00:00
|
|
|
|
|
|
|
def test_shallow_loader(self):
|
|
|
|
"Module existence can be tested inside eggs"
|
2022-02-03 19:24:19 +00:00
|
|
|
egg_name = "%s/test_egg.egg" % self.egg_dir
|
2014-01-26 05:50:40 +00:00
|
|
|
with extend_sys_path(egg_name):
|
2022-02-03 19:24:19 +00:00
|
|
|
egg_module = import_module("egg_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# An importable child
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(module_has_submodule(egg_module, "good_module"))
|
|
|
|
mod = import_module("egg_module.good_module")
|
|
|
|
self.assertEqual(mod.content, "Good Module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# A child that exists, but will generate an import error if loaded
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(module_has_submodule(egg_module, "bad_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("egg_module.bad_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# A child that doesn't exist
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(module_has_submodule(egg_module, "no_such_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("egg_module.no_such_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
|
|
|
def test_deep_loader(self):
|
|
|
|
"Modules deep inside an egg can still be tested for existence"
|
2022-02-03 19:24:19 +00:00
|
|
|
egg_name = "%s/test_egg.egg" % self.egg_dir
|
2014-01-26 05:50:40 +00:00
|
|
|
with extend_sys_path(egg_name):
|
2022-02-03 19:24:19 +00:00
|
|
|
egg_module = import_module("egg_module.sub1.sub2")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# An importable child
|
2022-02-03 19:24:19 +00:00
|
|
|
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")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# A child that exists, but will generate an import error if loaded
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(module_has_submodule(egg_module, "bad_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("egg_module.sub1.sub2.bad_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2014-01-26 05:50:40 +00:00
|
|
|
# A child that doesn't exist
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(module_has_submodule(egg_module, "no_such_module"))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_module("egg_module.sub1.sub2.no_such_module")
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2013-02-02 21:58:02 +00:00
|
|
|
|
2018-11-26 19:05:02 +00:00
|
|
|
class ModuleImportTests(SimpleTestCase):
|
2014-01-20 20:15:14 +00:00
|
|
|
def test_import_string(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
cls = import_string("django.utils.module_loading.import_string")
|
2014-01-20 20:15:14 +00:00
|
|
|
self.assertEqual(cls, import_string)
|
|
|
|
|
|
|
|
# Test exceptions raised
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ImportError):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_string("no_dots_in_path")
|
2015-06-03 15:05:03 +00:00
|
|
|
msg = 'Module "utils_tests" does not define a "unexistent" attribute'
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(ImportError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
import_string("utils_tests.unexistent")
|
2014-01-20 20:15:14 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={"append": "utils_tests.test_module"})
|
2013-09-06 23:23:25 +00:00
|
|
|
class AutodiscoverModulesTestCase(SimpleTestCase):
|
2014-10-31 09:22:15 +00:00
|
|
|
def tearDown(self):
|
|
|
|
sys.path_importer_cache.clear()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
sys.modules.pop("utils_tests.test_module.another_bad_module", None)
|
|
|
|
sys.modules.pop("utils_tests.test_module.another_good_module", None)
|
|
|
|
sys.modules.pop("utils_tests.test_module.bad_module", None)
|
|
|
|
sys.modules.pop("utils_tests.test_module.good_module", None)
|
|
|
|
sys.modules.pop("utils_tests.test_module", None)
|
2014-10-31 09:22:15 +00:00
|
|
|
|
2013-09-06 23:23:25 +00:00
|
|
|
def test_autodiscover_modules_found(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules("good_module")
|
2013-09-06 23:23:25 +00:00
|
|
|
|
|
|
|
def test_autodiscover_modules_not_found(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules("missing_module")
|
2013-09-06 23:23:25 +00:00
|
|
|
|
|
|
|
def test_autodiscover_modules_found_but_bad_module(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ImportError, "No module named 'a_package_name_that_does_not_exist'"
|
|
|
|
):
|
|
|
|
autodiscover_modules("bad_module")
|
2013-09-06 23:23:25 +00:00
|
|
|
|
|
|
|
def test_autodiscover_modules_several_one_bad_module(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ImportError, "No module named 'a_package_name_that_does_not_exist'"
|
|
|
|
):
|
|
|
|
autodiscover_modules("good_module", "bad_module")
|
2013-09-06 23:23:25 +00:00
|
|
|
|
|
|
|
def test_autodiscover_modules_several_found(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules("good_module", "another_good_module")
|
2013-09-06 23:23:25 +00:00
|
|
|
|
2014-10-31 09:22:15 +00:00
|
|
|
def test_autodiscover_modules_several_found_with_registry(self):
|
|
|
|
from .test_module import site
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
autodiscover_modules("good_module", "another_good_module", register_to=site)
|
|
|
|
self.assertEqual(site._registry, {"lorem": "ipsum"})
|
2014-10-31 09:22:15 +00:00
|
|
|
|
2013-09-06 23:23:25 +00:00
|
|
|
def test_validate_registry_keeps_intact(self):
|
|
|
|
from .test_module import site
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(Exception, "Some random exception."):
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules("another_bad_module", register_to=site)
|
2013-09-06 23:23:25 +00:00
|
|
|
self.assertEqual(site._registry, {})
|
|
|
|
|
2014-10-31 09:22:15 +00:00
|
|
|
def test_validate_registry_resets_after_erroneous_module(self):
|
|
|
|
from .test_module import site
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(Exception, "Some random exception."):
|
2022-02-03 19:24:19 +00:00
|
|
|
autodiscover_modules(
|
|
|
|
"another_good_module", "another_bad_module", register_to=site
|
|
|
|
)
|
|
|
|
self.assertEqual(site._registry, {"lorem": "ipsum"})
|
2014-10-31 09:22:15 +00:00
|
|
|
|
|
|
|
def test_validate_registry_resets_after_missing_module(self):
|
|
|
|
from .test_module import site
|
2022-02-03 19:24:19 +00:00
|
|
|
|
|
|
|
autodiscover_modules(
|
|
|
|
"does_not_exist", "another_good_module", "does_not_exist2", register_to=site
|
|
|
|
)
|
|
|
|
self.assertEqual(site._registry, {"lorem": "ipsum"})
|
2014-10-31 09:22:15 +00:00
|
|
|
|
2013-02-02 21:58:02 +00:00
|
|
|
|
2023-01-18 08:46:01 +00:00
|
|
|
class TestFinder:
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.importer = zipimporter(*args, **kwargs)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2023-01-18 08:46:01 +00:00
|
|
|
def find_spec(self, path, target=None):
|
|
|
|
return self.importer.find_spec(path, target)
|
2010-05-04 06:14:47 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-05-04 14:48:43 +00:00
|
|
|
class CustomLoader(EggLoader):
|
|
|
|
"""The Custom Loader test is exactly the same as the EggLoader, but
|
2021-09-16 18:20:54 +00:00
|
|
|
it uses a custom defined Loader class. Although the EggLoader combines both
|
|
|
|
functions into one class, this isn't required.
|
2010-05-04 14:48:43 +00:00
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2010-05-04 06:14:47 +00:00
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2010-05-04 06:14:47 +00:00
|
|
|
sys.path_hooks.insert(0, TestFinder)
|
|
|
|
sys.path_importer_cache.clear()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().tearDown()
|
2010-05-04 06:14:47 +00:00
|
|
|
sys.path_hooks.pop(0)
|