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

brett.cannon python-checkins at python.org
Wed Nov 15 22:50:59 CET 2006


Author: brett.cannon
Date: Wed Nov 15 22:50:58 2006
New Revision: 52759

Modified:
   sandbox/trunk/import_in_py/importer.py
   sandbox/trunk/import_in_py/test_importer.py
Log:
Add tests for the filesystem path_hooks factory function.


Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py	(original)
+++ sandbox/trunk/import_in_py/importer.py	Wed Nov 15 22:50:58 2006
@@ -248,7 +248,12 @@
 
     def __call__(self, path_entry):
         """If path_entry is a directory, return an importer object for it, else
-        raise ImportError."""
+        raise ImportError.
+   
+        Both relative and absolute paths are accepted (the former because of
+        interactive interpreter usage).
+    
+        """
         absolute_path = os.path.abspath(path_entry)
         if os.path.isdir(absolute_path):
             return FileSystemImporter(absolute_path, *self.handlers)

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	Wed Nov 15 22:50:58 2006
@@ -287,11 +287,37 @@
             self.failUnlessEqual(module.__file__, self.pkg_module_path)
 
 
-class FileSystemFactoryTests(TestPyPycPackages):
+class FileSystemFactoryTests(unittest.TestCase):
 
     """Test the filesystem path_hooks factory function."""
 
-    pass # XXX
+    def setUp(self):
+        mock_handler = mock_importer.MockHandler('.X')
+        self.factory = importer.FileSystemFactory(mock_handler)
+
+    def test_absolute_path_accepted(self):
+        # Passing in an absolute path to a directory should be accepted.
+        importer_ = self.factory(os.path.abspath(os.getcwd()))
+        self.failUnless(isinstance(importer_, importer.FileSystemImporter))
+
+    def test_relative_path_accepted(self):
+        # Passing in a relative directory path should be accepted.
+        importer_ = self.factory('')
+        self.failUnless(isinstance(importer_, importer.FileSystemImporter))
+
+    def test_bad_directory(self):
+        # Should raise ImportError when used on a non-existing directory.
+        self.failUnlessRaises(ImportError, self.factory, 'sdadfasdfsdfsadfsdf')
+
+    def test_file(self):
+        # Passing in a file should raise ImportError.
+        with open(test_support.TESTFN, 'w') as test_file:
+            test_file.write('for testing FileSystemFactory')
+        try:
+            self.failUnlessRaises(ImportError, self.factory,
+                                    test_support.TESTFN)
+        finally:
+            os.remove(test_support.TESTFN)
 
 
 class FileSystemImporterTests(TestPyPycPackages):


More information about the Python-checkins mailing list