[Python-checkins] r56788 - sandbox/trunk/import_in_py/zipimport_/tests.py

brett.cannon python-checkins at python.org
Tue Aug 7 06:58:53 CEST 2007


Author: brett.cannon
Date: Tue Aug  7 06:58:53 2007
New Revision: 56788

Modified:
   sandbox/trunk/import_in_py/zipimport_/tests.py
Log:
Add tests for the constructor.


Modified: sandbox/trunk/import_in_py/zipimport_/tests.py
==============================================================================
--- sandbox/trunk/import_in_py/zipimport_/tests.py	(original)
+++ sandbox/trunk/import_in_py/zipimport_/tests.py	Tue Aug  7 06:58:53 2007
@@ -1,7 +1,24 @@
 from zipimport_ import zipimport
+#import zipimport
 
+import contextlib
+import os
 from test import test_support
 import unittest
+import zipfile
+
+ at contextlib.contextmanager
+def temp_zipfile():
+    """Create a temporary zip file for testing."""
+    path = test_support.TESTFN + '.zip'
+    zip_file = zipfile.ZipFile(path, 'w')
+    try:
+        zip_file.writestr('_top_level.py', 'attr = None')
+        zip_file.close()
+        yield path
+    finally:
+        os.unlink(path)
+
 
 class ZipImportErrorTests(unittest.TestCase):
 
@@ -12,9 +29,29 @@
         self.assert_(issubclass(zipimport.ZipImportError, ImportError))
 
 
+class ZipImportCreation(unittest.TestCase):
+
+    """Test the creation of a zipimport.zipimporter instance."""
+
+    def test_nonzip(self):
+        # ZipImportError should be raised if a non-zip file is specified.
+            with open(test_support.TESTFN, 'w') as test_file:
+                test_file.write("# Test file for zipimport.")
+            try:
+                self.assertRaises(zipimport.ZipImportError,
+                        zipimport.zipimporter, test_support.TESTFN)
+            finally:
+                os.unlink(test_support.TESTFN)
+
+    def test_zipfile(self):
+        # A zipfile should return an instance of zipimporter.
+        with temp_zipfile() as zip_path:
+            zip_importer = zipimport.zipimporter(zip_path)
+            self.assert_(isinstance(zip_importer, zipimport.zipimporter))
+
 
 def test_main():
-    test_support.run_unittest(ZipImportErrorTests)
+    test_support.run_unittest(ZipImportErrorTests, ZipImportCreation)
 
 
 if __name__ == '__main__':


More information about the Python-checkins mailing list