[Python-checkins] r52579 - sandbox/trunk/import_in_py/mock_importer.py sandbox/trunk/import_in_py/test_importer.py

brett.cannon python-checkins at python.org
Thu Nov 2 00:40:31 CET 2006


Author: brett.cannon
Date: Thu Nov 2 00:40:31 2006
New Revision: 52579
Modified:
 sandbox/trunk/import_in_py/mock_importer.py
 sandbox/trunk/import_in_py/test_importer.py
Log:
Add tests for the filesystem importer and top-level packages.
Modified: sandbox/trunk/import_in_py/mock_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/mock_importer.py	(original)
+++ sandbox/trunk/import_in_py/mock_importer.py	Thu Nov 2 00:40:31 2006
@@ -19,10 +19,17 @@
 self.handles = handles
 
 def handle_code(self, loader, mod_name, path):
- """Mock implementation of a handler."""
- called_with = loader, mod_name, path
- sys.modules[mod_name] = called_with
- return called_with
+ """Mock implementation of a handler.
+ 
+ An object that can have arbitrary attributes attached to it must be
+ returned.
+ 
+ """
+ self.loader = loader
+ self.module_name = mod_name
+ self.path = path
+ sys.modules[mod_name] = self
+ return self
 
 class MockPyPycLoader(object):
 
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	Thu Nov 2 00:40:31 2006
@@ -12,6 +12,7 @@
 import new
 import os
 import py_compile
+import shutil
 import StringIO
 import sys
 import tempfile
@@ -180,17 +181,22 @@
 self.module_name + self.file_ext)
 with open(self.file_path, 'w') as test_file:
 test_file.write("A file for testing the 'importer' module.")
- mock_handler = mock_importer.MockHandler(self.file_ext)
+ self.pkg_name = 'test_pkg'
+ self.pkg_path = os.path.join(self.directory, self.pkg_name)
+ os.mkdir(os.path.join(self.directory, self.pkg_name))
+ self.pkg_init_path = os.path.join(self.pkg_path,
+ '__init__'+self.file_ext)
+ with open(self.pkg_init_path, 'w') as init_file:
+ init_file.write("This is a file for testing packages.")
+ self.handler = mock_importer.MockHandler(self.file_ext)
 self.importer = importer.FileSystemImporter(self.directory,
- mock_handler)
+ self.handler)
 self.importer.loader = mock_importer.MockPyPycLoader
 
 def tearDown(self):
 """Clean up the created file."""
- try:
- os.remove(self.file_path)
- except OSError:
- pass
+ os.remove(self.file_path)
+ shutil.rmtree(self.pkg_path)
 
 def test_find_module_single_handler(self):
 # Having a single handler should work without issue.
@@ -198,6 +204,7 @@
 self.failUnless(isinstance(loader, mock_importer.MockPyPycLoader))
 self.failUnlessEqual(loader.file_path, self.file_path)
 self.failUnless(isinstance(loader.handler, mock_importer.MockHandler))
+ self.failUnless(loader.package is None)
 
 def test_find_module_cannot_find(self):
 # Should return None if it can't find the module.
@@ -217,6 +224,14 @@
 self.failUnless(isinstance(loader, mock_importer.MockPyPycLoader))
 self.failUnlessEqual(loader.file_path, self.file_path)
 self.failUnless(isinstance(loader.handler, mock_importer.MockHandler))
+ 
+ def test_pkg_discovery(self):
+ # If a module name refers to a directory with an __init__ file it
+ # should be recognized as a package.
+ loader = self.importer.find_module(self.pkg_name)
+ self.failUnlessEqual(loader.file_path, self.pkg_init_path)
+ self.failUnlessEqual(loader.handler, self.handler)
+ self.failUnlessEqual(loader.package, self.pkg_path)
 
 
 class FileSystemLoaderMockEnv(unittest.TestCase):
@@ -226,7 +241,6 @@
 
 def setUp(self):
 """Create a fresh loader per run."""
- # XXX mock
 mock_handler = mock_importer.MockHandler()
 self.test_path = "<test path>"
 self.module_name = "test_module_name"
@@ -249,8 +263,9 @@
 # Test a basic module load where there is no sys.modules entry.
 # PyPycFileHelper.setUp() clears sys.modules for us.
 new_module = self.loader.load_module(self.module_name)
- expected = self.loader, self.module_name, self.test_path
- self.failUnlessEqual(new_module, expected)
+ self.failUnlessEqual(new_module.loader, self.loader)
+ self.failUnlessEqual(new_module.module_name, self.module_name)
+ self.failUnlessEqual(new_module.path, self.test_path)
 
 def test_load_module_sys_modules(self):
 # Make sure that the loader returns the module from sys.modules if it


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /