Back to index

python3.2  3.2.2
test_path.py
Go to the documentation of this file.
00001 from importlib import _bootstrap
00002 from importlib import machinery
00003 from .. import util
00004 from . import util as import_util
00005 import imp
00006 import os
00007 import sys
00008 import tempfile
00009 from test import support
00010 from types import MethodType
00011 import unittest
00012 
00013 
00014 class FinderTests(unittest.TestCase):
00015 
00016     """Tests for PathFinder."""
00017 
00018     def test_failure(self):
00019         # Test None returned upon not finding a suitable finder.
00020         module = '<test module>'
00021         with util.import_state():
00022             self.assertTrue(machinery.PathFinder.find_module(module) is None)
00023 
00024     def test_sys_path(self):
00025         # Test that sys.path is used when 'path' is None.
00026         # Implicitly tests that sys.path_importer_cache is used.
00027         module = '<test module>'
00028         path = '<test path>'
00029         importer = util.mock_modules(module)
00030         with util.import_state(path_importer_cache={path: importer},
00031                                path=[path]):
00032             loader = machinery.PathFinder.find_module(module)
00033             self.assertTrue(loader is importer)
00034 
00035     def test_path(self):
00036         # Test that 'path' is used when set.
00037         # Implicitly tests that sys.path_importer_cache is used.
00038         module = '<test module>'
00039         path = '<test path>'
00040         importer = util.mock_modules(module)
00041         with util.import_state(path_importer_cache={path: importer}):
00042             loader = machinery.PathFinder.find_module(module, [path])
00043             self.assertTrue(loader is importer)
00044 
00045     def test_path_hooks(self):
00046         # Test that sys.path_hooks is used.
00047         # Test that sys.path_importer_cache is set.
00048         module = '<test module>'
00049         path = '<test path>'
00050         importer = util.mock_modules(module)
00051         hook = import_util.mock_path_hook(path, importer=importer)
00052         with util.import_state(path_hooks=[hook]):
00053             loader = machinery.PathFinder.find_module(module, [path])
00054             self.assertTrue(loader is importer)
00055             self.assertTrue(path in sys.path_importer_cache)
00056             self.assertTrue(sys.path_importer_cache[path] is importer)
00057 
00058     def test_path_importer_cache_has_None(self):
00059         # Test that if sys.path_importer_cache has None that None is returned.
00060         clear_cache = {path: None for path in sys.path}
00061         with util.import_state(path_importer_cache=clear_cache):
00062             for name in ('asynchat', 'sys', '<test module>'):
00063                 self.assertTrue(machinery.PathFinder.find_module(name) is None)
00064 
00065     def test_path_importer_cache_has_None_continues(self):
00066         # Test that having None in sys.path_importer_cache causes the search to
00067         # continue.
00068         path = '<test path>'
00069         module = '<test module>'
00070         importer = util.mock_modules(module)
00071         with util.import_state(path=['1', '2'],
00072                             path_importer_cache={'1': None, '2': importer}):
00073             loader = machinery.PathFinder.find_module(module)
00074             self.assertTrue(loader is importer)
00075 
00076 
00077 
00078 class DefaultPathFinderTests(unittest.TestCase):
00079 
00080     """Test importlib._bootstrap._DefaultPathFinder."""
00081 
00082     def test_implicit_hooks(self):
00083         # Test that the implicit path hooks are used.
00084         bad_path = '<path>'
00085         module = '<module>'
00086         assert not os.path.exists(bad_path)
00087         existing_path = tempfile.mkdtemp()
00088         try:
00089             with util.import_state():
00090                 nothing = _bootstrap._DefaultPathFinder.find_module(module,
00091                                                         path=[existing_path])
00092                 self.assertTrue(nothing is None)
00093                 self.assertTrue(existing_path in sys.path_importer_cache)
00094                 result = isinstance(sys.path_importer_cache[existing_path],
00095                                     imp.NullImporter)
00096                 self.assertFalse(result)
00097                 nothing = _bootstrap._DefaultPathFinder.find_module(module,
00098                                                             path=[bad_path])
00099                 self.assertTrue(nothing is None)
00100                 self.assertTrue(bad_path in sys.path_importer_cache)
00101                 self.assertTrue(isinstance(sys.path_importer_cache[bad_path],
00102                                            imp.NullImporter))
00103         finally:
00104             os.rmdir(existing_path)
00105 
00106 
00107     def test_path_importer_cache_has_None(self):
00108         # Test that the default hook is used when sys.path_importer_cache
00109         # contains None for a path.
00110         module = '<test module>'
00111         importer = util.mock_modules(module)
00112         path = '<test path>'
00113         # XXX Not blackbox.
00114         original_hook = _bootstrap._DEFAULT_PATH_HOOK
00115         mock_hook = import_util.mock_path_hook(path, importer=importer)
00116         _bootstrap._DEFAULT_PATH_HOOK = mock_hook
00117         try:
00118             with util.import_state(path_importer_cache={path: None}):
00119                 loader = _bootstrap._DefaultPathFinder.find_module(module,
00120                                                                     path=[path])
00121                 self.assertTrue(loader is importer)
00122         finally:
00123             _bootstrap._DEFAULT_PATH_HOOK = original_hook
00124 
00125 
00126 def test_main():
00127     from test.support import run_unittest
00128     run_unittest(FinderTests, DefaultPathFinderTests)
00129 
00130 if __name__ == '__main__':
00131     test_main()