[Python-checkins] r52279 - sandbox/trunk/import_in_py/importer.py sandbox/trunk/import_in_py/test_importer.py
brett.cannon
python-checkins at python.org
Tue Oct 10 22:52:33 CEST 2006
Author: brett.cannon
Date: Tue Oct 10 22:52:33 2006
New Revision: 52279
Modified:
sandbox/trunk/import_in_py/importer.py
sandbox/trunk/import_in_py/test_importer.py
Log:
Add tests for the filesystem importer. Along the way fix bugs to pass the
tests.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Tue Oct 10 22:52:33 2006
@@ -136,8 +136,8 @@
# XXX Does not worry about case-insensitive filesystems.
for handler in self.handlers:
file_name = fullname + '.' + handler.handles
- possible_file = os.path.join(self.path_entry, file_name)
- if os.path.isfile(possible_file):
+ file_path = os.path.join(self.path_entry, file_name)
+ if os.path.isfile(file_path):
break
else:
return None
Modified: sandbox/trunk/import_in_py/test_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/test_importer.py (original)
+++ sandbox/trunk/import_in_py/test_importer.py Tue Oct 10 22:52:33 2006
@@ -117,6 +117,10 @@
def setUp(self):
"""Generate the path to a temporary file to test with."""
self.module = 'source_tester'
+ try:
+ del sys.modules[self.module]
+ except KeyError:
+ pass
self.directory = tempfile.gettempdir()
self.source_path = os.path.join(self.directory, self.module+'.py')
self.attr_name = 'test_attr'
@@ -201,10 +205,7 @@
def test_load_module_fresh(self):
# Test a basic module load where there is no sys.modules entry.
- try:
- del sys.modules[self.module]
- except KeyError:
- pass
+ # PyPycTests.setUp() clears sys.modules for us.
new_module = self.loader.load_module(self.module)
self.verify_module(new_module, self.source_path)
@@ -215,6 +216,48 @@
sys.modules[self.module] = new_module
loaded_module = self.loader.load_module(self.module)
self.failUnless(loaded_module is new_module)
+
+
+class FileSystemImporterTests(PyPycTests):
+
+ """Test the filesystem importer."""
+
+ def setUp(self):
+ """Create a basic importer."""
+ super(self.__class__, self).setUp()
+ self.importer = importer.FileSystemImporter(self.directory,
+ importer.PySourceHandler())
+
+ def test_find_module_single_handler(self):
+ # Having a single handler should work without issue.
+ loader = self.importer.find_module(self.module)
+ self.failUnless(isinstance(loader, importer.FileSystemLoader))
+ self.failUnlessEqual(loader.file_path, self.source_path)
+ self.failUnless(isinstance(loader.handler, importer.PySourceHandler))
+
+ def test_find_module_cannot_find(self):
+ # Should return None if it can't find the module.
+ found = self.importer.find_module('gobbledeegook')
+ self.failUnlessEqual(found, None)
+
+ def test_find_module_multiple_handlers(self):
+ # Modules should be found based on the order of the handlers.
+ fs_importer = importer.FileSystemImporter(self.directory,
+ importer.PyBytecodeHandler(),
+ importer.PySourceHandler())
+ loader = fs_importer.find_module(self.module)
+ self.failUnless(isinstance(loader, importer.FileSystemLoader))
+ self.failUnlessEqual(loader.file_path, self.bytecode_path)
+ self.failUnless(isinstance(loader.handler, importer.PyBytecodeHandler))
+
+ def test_find_to_load(self):
+ # Make sure that one can go from find_module() to getting a module
+ # imported.
+ loader = self.importer.find_module(self.module)
+ self.failUnless(loader)
+ module = loader.load_module(self.module)
+ self.verify_module(module, self.source_path)
+ self.failUnlessEqual(module, sys.modules[self.module])
def test_main():
@@ -224,6 +267,7 @@
SourceHandlerTests,
BytecodeHandlerTests,
FileSystemLoaderTests,
+ FileSystemImporterTests,
)
More information about the Python-checkins
mailing list