| 1 |
import os |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from importlib import import_module |
| 5 |
from zipimport import zipimporter |
| 6 |
|
| 7 |
from django.test import SimpleTestCase, modify_settings |
| 8 |
from django.test.utils import extend_sys_path |
| 9 |
from django.utils.module_loading import ( |
| 10 |
autodiscover_modules, |
| 11 |
import_string, |
| 12 |
module_has_submodule, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class DefaultLoader(unittest.TestCase): |
| 17 |
def test_loader(self): |
| 18 |
"Normal module existence can be tested" |
| 19 |
test_module = import_module("utils_tests.test_module") |
| 20 |
test_no_submodule = import_module("utils_tests.test_no_submodule") |
| 21 |
|
| 22 |
# An importable child |
| 23 |
self.assertTrue(module_has_submodule(test_module, "good_module")) |
| 24 |
mod = import_module("utils_tests.test_module.good_module") |
| 25 |
self.assertEqual(mod.content, "Good Module") |
| 26 |
|
| 27 |
# A child that exists, but will generate an import error if loaded |
| 28 |
self.assertTrue(module_has_submodule(test_module, "bad_module")) |
| 29 |
with self.assertRaises(ImportError): |
| 30 |
import_module("utils_tests.test_module.bad_module") |
| 31 |
|
| 32 |
# A child that doesn't exist |
| 33 |
self.assertFalse(module_has_submodule(test_module, "no_such_module")) |
| 34 |
with self.assertRaises(ImportError): |
| 35 |
import_module("utils_tests.test_module.no_such_module") |
| 36 |
|
| 37 |
# A child that doesn't exist, but is the name of a package on the path |
| 38 |
self.assertFalse(module_has_submodule(test_module, "django")) |
| 39 |
with self.assertRaises(ImportError): |
| 40 |
import_module("utils_tests.test_module.django") |
| 41 |
|
| 42 |
# Don't be confused by caching of import misses |
| 43 |
import types # NOQA: causes attempted import of utils_tests.types |
| 44 |
|
| 45 |
self.assertFalse(module_has_submodule(sys.modules["utils_tests"], "types")) |
| 46 |
|
| 47 |
# A module which doesn't have a __path__ (so no submodules) |
| 48 |
self.assertFalse(module_has_submodule(test_no_submodule, "anything")) |
| 49 |
with self.assertRaises(ImportError): |
| 50 |
import_module("utils_tests.test_no_submodule.anything") |
| 51 |
|
| 52 |
def test_has_sumbodule_with_dotted_path(self): |
| 53 |
"""Nested module existence can be tested.""" |
| 54 |
test_module = import_module("utils_tests.test_module") |
| 55 |
# A grandchild that exists. |
| 56 |
self.assertIs( |
| 57 |
module_has_submodule(test_module, "child_module.grandchild_module"), True |
| 58 |
) |
| 59 |
# A grandchild that doesn't exist. |
| 60 |
self.assertIs( |
| 61 |
module_has_submodule(test_module, "child_module.no_such_module"), False |
| 62 |
) |
| 63 |
# A grandchild whose parent doesn't exist. |
| 64 |
self.assertIs( |
| 65 |
module_has_submodule(test_module, "no_such_module.grandchild_module"), False |
| 66 |
) |
| 67 |
# A grandchild whose parent is not a package. |
| 68 |
self.assertIs( |
| 69 |
module_has_submodule(test_module, "good_module.no_such_module"), False |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
class EggLoader(unittest.TestCase): |
| 74 |
def setUp(self): |
| 75 |
self.egg_dir = "%s/eggs" % os.path.dirname(__file__) |
| 76 |
|
| 77 |
def tearDown(self): |
| 78 |
sys.path_importer_cache.clear() |
| 79 |
|
| 80 |
sys.modules.pop("egg_module.sub1.sub2.bad_module", None) |
| 81 |
sys.modules.pop("egg_module.sub1.sub2.good_module", None) |
| 82 |
sys.modules.pop("egg_module.sub1.sub2", None) |
| 83 |
sys.modules.pop("egg_module.sub1", None) |
| 84 |
sys.modules.pop("egg_module.bad_module", None) |
| 85 |
sys.modules.pop("egg_module.good_module", None) |
| 86 |
sys.modules.pop("egg_module", None) |
| 87 |
|
| 88 |
def test_shallow_loader(self): |
| 89 |
"Module existence can be tested inside eggs" |
| 90 |
egg_name = "%s/test_egg.egg" % self.egg_dir |
| 91 |
with extend_sys_path(egg_name): |
| 92 |
egg_module = import_module("egg_module") |
| 93 |
|
| 94 |
# An importable child |
| 95 |
self.assertTrue(module_has_submodule(egg_module, "good_module")) |
| 96 |
mod = import_module("egg_module.good_module") |
| 97 |
self.assertEqual(mod.content, "Good Module") |
| 98 |
|
| 99 |
# A child that exists, but will generate an import error if loaded |
| 100 |
self.assertTrue(module_has_submodule(egg_module, "bad_module")) |
| 101 |
with self.assertRaises(ImportError): |
| 102 |
import_module("egg_module.bad_module") |
| 103 |
|
| 104 |
# A child that doesn't exist |
| 105 |
self.assertFalse(module_has_submodule(egg_module, "no_such_module")) |
| 106 |
with self.assertRaises(ImportError): |
| 107 |
import_module("egg_module.no_such_module") |
| 108 |
|
| 109 |
def test_deep_loader(self): |
| 110 |
"Modules deep inside an egg can still be tested for existence" |
| 111 |
egg_name = "%s/test_egg.egg" % self.egg_dir |
| 112 |
with extend_sys_path(egg_name): |
| 113 |
egg_module = import_module("egg_module.sub1.sub2") |
| 114 |
|
| 115 |
# An importable child |
| 116 |
self.assertTrue(module_has_submodule(egg_module, "good_module")) |
| 117 |
mod = import_module("egg_module.sub1.sub2.good_module") |
| 118 |
self.assertEqual(mod.content, "Deep Good Module") |
| 119 |
|
| 120 |
# A child that exists, but will generate an import error if loaded |
| 121 |
self.assertTrue(module_has_submodule(egg_module, "bad_module")) |
| 122 |
with self.assertRaises(ImportError): |
| 123 |
import_module("egg_module.sub1.sub2.bad_module") |
| 124 |
|
| 125 |
# A child that doesn't exist |
| 126 |
self.assertFalse(module_has_submodule(egg_module, "no_such_module")) |
| 127 |
with self.assertRaises(ImportError): |
| 128 |
import_module("egg_module.sub1.sub2.no_such_module") |
| 129 |
|
| 130 |
|
| 131 |
class ModuleImportTests(SimpleTestCase): |
| 132 |
def test_import_string(self): |
| 133 |
cls = import_string("django.utils.module_loading.import_string") |
| 134 |
self.assertEqual(cls, import_string) |
| 135 |
|
| 136 |
# Test exceptions raised |
| 137 |
with self.assertRaises(ImportError): |
| 138 |
import_string("no_dots_in_path") |
| 139 |
msg = 'Module "utils_tests" does not define a "unexistent" attribute' |
| 140 |
with self.assertRaisesMessage(ImportError, msg): |
| 141 |
import_string("utils_tests.unexistent") |
| 142 |
|
| 143 |
|
| 144 |
@modify_settings(INSTALLED_APPS={"append": "utils_tests.test_module"}) |
| 145 |
class AutodiscoverModulesTestCase(SimpleTestCase): |
| 146 |
def tearDown(self): |
| 147 |
sys.path_importer_cache.clear() |
| 148 |
|
| 149 |
sys.modules.pop("utils_tests.test_module.another_bad_module", None) |
| 150 |
sys.modules.pop("utils_tests.test_module.another_good_module", None) |
| 151 |
sys.modules.pop("utils_tests.test_module.bad_module", None) |
| 152 |
sys.modules.pop("utils_tests.test_module.good_module", None) |
| 153 |
sys.modules.pop("utils_tests.test_module", None) |
| 154 |
|
| 155 |
def test_autodiscover_modules_found(self): |
| 156 |
autodiscover_modules("good_module") |
| 157 |
|
| 158 |
def test_autodiscover_modules_not_found(self): |
| 159 |
autodiscover_modules("missing_module") |
| 160 |
|
| 161 |
def test_autodiscover_modules_found_but_bad_module(self): |
| 162 |
with self.assertRaisesMessage( |
| 163 |
ImportError, "No module named 'a_package_name_that_does_not_exist'" |
| 164 |
): |
| 165 |
autodiscover_modules("bad_module") |
| 166 |
|
| 167 |
def test_autodiscover_modules_several_one_bad_module(self): |
| 168 |
with self.assertRaisesMessage( |
| 169 |
ImportError, "No module named 'a_package_name_that_does_not_exist'" |
| 170 |
): |
| 171 |
autodiscover_modules("good_module", "bad_module") |
| 172 |
|
| 173 |
def test_autodiscover_modules_several_found(self): |
| 174 |
autodiscover_modules("good_module", "another_good_module") |
| 175 |
|
| 176 |
def test_autodiscover_modules_several_found_with_registry(self): |
| 177 |
from .test_module import site |
| 178 |
|
| 179 |
autodiscover_modules("good_module", "another_good_module", register_to=site) |
| 180 |
self.assertEqual(site._registry, {"lorem": "ipsum"}) |
| 181 |
|
| 182 |
def test_validate_registry_keeps_intact(self): |
| 183 |
from .test_module import site |
| 184 |
|
| 185 |
with self.assertRaisesMessage(Exception, "Some random exception."): |
| 186 |
autodiscover_modules("another_bad_module", register_to=site) |
| 187 |
self.assertEqual(site._registry, {}) |
| 188 |
|
| 189 |
def test_validate_registry_resets_after_erroneous_module(self): |
| 190 |
from .test_module import site |
| 191 |
|
| 192 |
with self.assertRaisesMessage(Exception, "Some random exception."): |
| 193 |
autodiscover_modules( |
| 194 |
"another_good_module", "another_bad_module", register_to=site |
| 195 |
) |
| 196 |
self.assertEqual(site._registry, {"lorem": "ipsum"}) |
| 197 |
|
| 198 |
def test_validate_registry_resets_after_missing_module(self): |
| 199 |
from .test_module import site |
| 200 |
|
| 201 |
autodiscover_modules( |
| 202 |
"does_not_exist", "another_good_module", "does_not_exist2", register_to=site |
| 203 |
) |
| 204 |
self.assertEqual(site._registry, {"lorem": "ipsum"}) |
| 205 |
|
| 206 |
|
| 207 |
class TestFinder: |
| 208 |
def __init__(self, *args, **kwargs): |
| 209 |
self.importer = zipimporter(*args, **kwargs) |
| 210 |
|
| 211 |
def find_spec(self, path, target=None): |
| 212 |
return self.importer.find_spec(path, target) |
| 213 |
|
| 214 |
|
| 215 |
class CustomLoader(EggLoader): |
| 216 |
"""The Custom Loader test is exactly the same as the EggLoader, but |
| 217 |
it uses a custom defined Loader class. Although the EggLoader combines both |
| 218 |
functions into one class, this isn't required. |
| 219 |
""" |
| 220 |
|
| 221 |
def setUp(self): |
| 222 |
super().setUp() |
| 223 |
sys.path_hooks.insert(0, TestFinder) |
| 224 |
sys.path_importer_cache.clear() |
| 225 |
|
| 226 |
def tearDown(self): |
| 227 |
super().tearDown() |
| 228 |
sys.path_hooks.pop(0) |