| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | from importlib import machinery | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2009-02-01 04:28:04 +00:00
										 |  |  | from . import util as import_util | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | from contextlib import nested | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  | import imp | 
					
						
							| 
									
										
										
										
											2009-02-05 22:02:03 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | from types import MethodType | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | class FinderTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Tests for SysPathImporter.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_failure(self): | 
					
						
							|  |  |  |         # Test None returned upon not finding a suitable finder. | 
					
						
							|  |  |  |         def mock_implicit_hooks(): | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  |         # XXX Not blackbox. | 
					
						
							|  |  |  |         original_hooks = machinery.PathFinder._implicit_hooks | 
					
						
							|  |  |  |         machinery.PathFinder._implicit_hooks = staticmethod(mock_implicit_hooks) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with util.import_state(): | 
					
						
							|  |  |  |                 self.assert_(machinery.PathFinder.find_module('XXX') is None) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             machinery.PathFinder._implicit_hooks = original_hooks | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_sys_path(self): | 
					
						
							|  |  |  |         # Test that sys.path is used when 'path' is None. | 
					
						
							|  |  |  |         # Implicitly tests that sys.path_importer_cache is used. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         with util.import_state(path_importer_cache={path: importer}, | 
					
						
							|  |  |  |                                path=[path]): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module) | 
					
						
							|  |  |  |             self.assert_(loader is importer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_path(self): | 
					
						
							|  |  |  |         # Test that 'path' is used when set. | 
					
						
							|  |  |  |         # Implicitly tests that sys.path_importer_cache is used. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         with util.import_state(path_importer_cache={path: importer}): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module, [path]) | 
					
						
							|  |  |  |             self.assert_(loader is importer) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_path_importer_cache_has_None(self): | 
					
						
							|  |  |  |         # Test that the default hook is used when sys.path_importer_cache | 
					
						
							|  |  |  |         # contains None for a path. | 
					
						
							| 
									
										
										
										
											2009-02-05 22:02:03 +00:00
										 |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         # XXX Not blackbox. | 
					
						
							|  |  |  |         original_hook = machinery.PathFinder._default_hook | 
					
						
							|  |  |  |         mock_hook = import_util.mock_path_hook(path, importer=importer) | 
					
						
							|  |  |  |         machinery.PathFinder._default_hook = staticmethod(mock_hook) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with util.import_state(path_importer_cache={path: None}): | 
					
						
							|  |  |  |                 loader = machinery.PathFinder.find_module(module, path=[path]) | 
					
						
							|  |  |  |                 self.assert_(loader is importer) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             machinery.PathFinder._default_hook = original_hook | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_path_hooks(self): | 
					
						
							|  |  |  |         # Test that sys.path_hooks is used. | 
					
						
							| 
									
										
										
										
											2009-02-05 23:36:02 +00:00
										 |  |  |         # Test that sys.path_importer_cache is set. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         hook = import_util.mock_path_hook(path, importer=importer) | 
					
						
							|  |  |  |         with util.import_state(path_hooks=[hook]): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module, [path]) | 
					
						
							|  |  |  |             self.assert_(loader is importer) | 
					
						
							|  |  |  |             self.assert_(path in sys.path_importer_cache) | 
					
						
							|  |  |  |             self.assert_(sys.path_importer_cache[path] is importer) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_implicit_hooks(self): | 
					
						
							|  |  |  |         # Test that the implicit path hooks are used. | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  |         existing_path = os.path.dirname(support.TESTFN) | 
					
						
							|  |  |  |         bad_path = '<path>' | 
					
						
							|  |  |  |         module = '<module>' | 
					
						
							|  |  |  |         assert not os.path.exists(bad_path) | 
					
						
							|  |  |  |         with util.import_state(): | 
					
						
							|  |  |  |             nothing = machinery.PathFinder.find_module(module, | 
					
						
							|  |  |  |                                                        path=[existing_path]) | 
					
						
							|  |  |  |             self.assert_(nothing is None) | 
					
						
							|  |  |  |             self.assert_(existing_path in sys.path_importer_cache) | 
					
						
							|  |  |  |             self.assert_(not isinstance(sys.path_importer_cache[existing_path], | 
					
						
							|  |  |  |                                         imp.NullImporter)) | 
					
						
							|  |  |  |             nothing = machinery.PathFinder.find_module(module, path=[bad_path]) | 
					
						
							|  |  |  |             self.assert_(nothing is None) | 
					
						
							|  |  |  |             self.assert_(bad_path in sys.path_importer_cache) | 
					
						
							|  |  |  |             self.assert_(isinstance(sys.path_importer_cache[bad_path], | 
					
						
							|  |  |  |                                     imp.NullImporter)) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | def test_main(): | 
					
						
							|  |  |  |     from test.support import run_unittest | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  |     run_unittest(PathTests, __path__Tests, FinderTests) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |