| 1 |
from glob import glob |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
import Cheetah.ImportHooks |
| 7 |
from Cheetah.compat import PY2, cache_from_source |
| 8 |
|
| 9 |
|
| 10 |
ImportHooksTemplatesDir = os.path.join( |
| 11 |
os.path.dirname(__file__), 'ImportHooksTemplates') |
| 12 |
|
| 13 |
|
| 14 |
def setUpModule(): |
| 15 |
sys.path.append(ImportHooksTemplatesDir) |
| 16 |
|
| 17 |
|
| 18 |
def tearDownModule(): |
| 19 |
assert sys.path[-1] == ImportHooksTemplatesDir |
| 20 |
del sys.path[-1] |
| 21 |
|
| 22 |
|
| 23 |
def _cleanup(): |
| 24 |
py_files = os.path.join(ImportHooksTemplatesDir, '*.py') |
| 25 |
pyc_files = py_files + 'c' |
| 26 |
for fname in glob(py_files) + glob(pyc_files): |
| 27 |
os.remove(fname) |
| 28 |
__pycache__ = os.path.join(ImportHooksTemplatesDir, '__pycache__') |
| 29 |
if os.path.isdir(__pycache__): |
| 30 |
shutil.rmtree(__pycache__) |
| 31 |
|
| 32 |
for modname in list(sys.modules.keys()): |
| 33 |
if '.ImportHooksTemplates.' in modname \ |
| 34 |
or modname.endswith('.ImportHooksTemplates'): |
| 35 |
del sys.modules[modname] |
| 36 |
|
| 37 |
for modname in 'index', 'layout': |
| 38 |
if modname in sys.modules: |
| 39 |
del sys.modules[modname] |
| 40 |
|
| 41 |
Cheetah.ImportHooks.uninstall() |
| 42 |
|
| 43 |
|
| 44 |
def _exec(code, _dict): |
| 45 |
exec(code, _dict) |
| 46 |
|
| 47 |
|
| 48 |
class ImportHooksTest(unittest.TestCase): |
| 49 |
def setUp(self): |
| 50 |
_cleanup() |
| 51 |
|
| 52 |
def tearDown(self): |
| 53 |
_cleanup() |
| 54 |
|
| 55 |
def test_CheetahDirOwner(self): |
| 56 |
files = list(sorted(os.listdir(ImportHooksTemplatesDir))) |
| 57 |
self.assertListEqual(files, ['index.tmpl', 'layout.tmpl']) |
| 58 |
|
| 59 |
cdo = Cheetah.ImportHooks.CheetahDirOwner(ImportHooksTemplatesDir) |
| 60 |
index_mod = cdo.getmod('index') |
| 61 |
files = os.listdir(ImportHooksTemplatesDir) |
| 62 |
self.assertIn('index.py', files) |
| 63 |
self.assertNotIn('layout.py', files) |
| 64 |
|
| 65 |
index_co = index_mod.__co__ |
| 66 |
del index_mod.__co__ |
| 67 |
self.assertRaises(ImportError, _exec, index_co, index_mod.__dict__) |
| 68 |
|
| 69 |
cdo.getmod('layout') |
| 70 |
files = os.listdir(ImportHooksTemplatesDir) |
| 71 |
self.assertIn('layout.py', files) |
| 72 |
if not PY2: |
| 73 |
files = os.listdir( |
| 74 |
os.path.join(ImportHooksTemplatesDir, '__pycache__')) |
| 75 |
self.assertIn(os.path.basename(cache_from_source('layout.py')), files) |
| 76 |
|
| 77 |
def test_ImportHooks(self): |
| 78 |
files = os.listdir(ImportHooksTemplatesDir) |
| 79 |
self.assertNotIn('index.py', files) |
| 80 |
self.assertNotIn('layout.py', files) |
| 81 |
Cheetah.ImportHooks.install() |
| 82 |
from index import index |
| 83 |
files = os.listdir(ImportHooksTemplatesDir) |
| 84 |
self.assertIn('index.py', files) |
| 85 |
self.assertIn('layout.py', files) |
| 86 |
if not PY2: |
| 87 |
files = os.listdir( |
| 88 |
os.path.join(ImportHooksTemplatesDir, '__pycache__')) |
| 89 |
self.assertIn(os.path.basename(cache_from_source('index.py')), files) |
| 90 |
self.assertIn(os.path.basename(cache_from_source('layout.py')), files) |
| 91 |
|
| 92 |
def test_import_builtin(self): |
| 93 |
Cheetah.ImportHooks.install() |
| 94 |
for nm in sys.builtin_module_names: |
| 95 |
if nm not in sys.modules: |
| 96 |
__import__(nm) |
| 97 |
return |
| 98 |
raise self.fail("All builtin modules are imported") |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
if not PY2 and sys.version_info < (3, 10): |
| 103 |
def test_import_bootlocale(self): |
| 104 |
if '_bootlocale' in sys.modules: |
| 105 |
del sys.modules['_bootlocale'] |
| 106 |
Cheetah.ImportHooks.install() |
| 107 |
import _bootlocale |
| 108 |
|
| 109 |
try: |
| 110 |
ModuleNotFoundError |
| 111 |
except NameError: |
| 112 |
pass |
| 113 |
else: |
| 114 |
def test_module_not_found(self): |
| 115 |
Cheetah.ImportHooks.install() |
| 116 |
try: |
| 117 |
import no_such_module |
| 118 |
except ModuleNotFoundError: |
| 119 |
pass |
| 120 |
except ImportError: |
| 121 |
self.fail("raised ImportError, expected ModuleNotFoundError") |