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

brett.cannon python-checkins at python.org
Tue Oct 10 02:51:23 CEST 2006


Author: brett.cannon
Date: Tue Oct 10 02:51:23 2006
New Revision: 52263

Modified:
   sandbox/trunk/import_in_py/test_importer.py
Log:
Initial tests for the filesystem loader.

Factored out basic Python source test code into separate superclass to be used
by both the Python source handler tests and the filesystem loader tests.


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 02:51:23 2006
@@ -107,45 +107,82 @@
     def tearDown(self):
         """Reset stdout to what it is by default."""
         sys.stdout = self._orig_stdout
+        
+        
+class SourceFileTests(unittest.TestCase):
+    
+    """Base class to help in generating a fresh source  file."""
+    
+    def setUp(self):
+        """Generate the path to a temporary file to test with."""
+        self.module = 'source_tester'
+        self.directory = tempfile.gettempdir()
+        self.file_path = os.path.join(self.directory, self.module+'.py')
+        self.attr_name = 'test_attr'
+        self.attr_value = None
+        with open(self.file_path, 'w') as py_file:
+            py_file.write('%s = %r' % (self.attr_name, self.attr_value))
 
+    def tearDown(self):
+        """If the temporary path was used, make sure to clean up."""
+        os.remove(self.file_path)
+        
+    def verify_module(self, module):
+        """Verify that the module is the one created during setup and has the
+        expected attributes and values."""
+        self.failUnlessEqual(module.__name__, self.module)
+        self.failUnlessEqual(module.__file__, self.file_path)
+        self.failUnless(hasattr(module, self.attr_name))
+        self.failUnlessEqual(getattr(module, self.attr_name), self.attr_value)
 
-class SourceHandlerTests(unittest.TestCase):
 
-    """Test the Python source code handler."""
+class SourceHandlerTests(SourceFileTests):
 
+    """Test the Python source code handler."""
+    
     def setUp(self):
-        """Generate the path to a temporary file to test with."""
-        self.test_module = 'source_tester'
-        self.test_dir = tempfile.gettempdir()
-        self.file_path = os.path.join(self.test_dir, self.test_module+'.py')
+        """Create a handler to use for each test."""
+        super(self.__class__, self).setUp()
         self.handler = importer.PySourceHandler()
 
-    def tearDown(self):
-        """If the temporary path was used, make sure to clean up."""
-        if os.path.exists(self.file_path):
-            os.remove(self.file_path)
-
     def test_handle(self):
         # Should claim it handles 'py' data.
         self.failUnlessEqual(self.handler.handles, 'py')
 
     def test_handle_file_module(self):
-        # Should be handle a module that is directly pointed at.
-        with open(self.file_path, 'w') as py_file:
-            py_file.write("test_attr = None")
-        new_module = new.module(self.test_module)
-        self.handler.handle_file(new_module, self.test_module,
+        # Should be able to handle a module that is directly pointed at.
+        new_module = new.module(self.module)
+        self.handler.handle_file(new_module, self.module,
                                   None, self.file_path)
-        self.failUnlessEqual(self.test_module, new_module.__name__)
-        self.failUnlessEqual(self.file_path, new_module.__file__)
-        self.failUnless(hasattr(new_module, 'test_attr'))
-                
+        self.verify_module(new_module)
+                             
+                             
+class FileSystemLoaderTests(SourceFileTests):
+    
+    """Test the filesystem loader."""
+    
+    def setUp(self):
+        """Create a fresh loader per run."""
+        super(self.__class__, self).setUp()
+        self.loader = importer.FileSystemLoader(self.file_path,
+                                                importer.PySourceHandler())
+                                                
+    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
+        new_module = self.loader.load_module(self.module)
+        self.verify_module(new_module)
+        
 
 def test_main():
     test_support.run_unittest(
                 BuiltinImporterTests,
                 FrozenImporterTests,
                 SourceHandlerTests,
+                FileSystemLoaderTests,
             )
 
 


More information about the Python-checkins mailing list