[Python-checkins] r57079 - sandbox/trunk/import_in_py/tests/test_fs_importer.py

brett.cannon python-checkins at python.org
Thu Aug 16 04:11:19 CEST 2007


Author: brett.cannon
Date: Thu Aug 16 04:11:09 2007
New Revision: 57079

Modified:
   sandbox/trunk/import_in_py/tests/test_fs_importer.py
Log:
Add tests for importing a package before a same-named module, and
case-sensitivity (both modules and packages).


Modified: sandbox/trunk/import_in_py/tests/test_fs_importer.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_fs_importer.py	(original)
+++ sandbox/trunk/import_in_py/tests/test_fs_importer.py	Thu Aug 16 04:11:09 2007
@@ -17,7 +17,7 @@
 
 class PyFileImporterTests(TestPyPycPackages):
 
-    """Test the PyFileImporterTests.
+    """Test the PyFileImporterTests (and thus also FileImporter).
 
     No need to check for searching any deeper than a package (e.g., don't need
     to look for a sub-module or sub-package) since the import machinery will
@@ -90,15 +90,87 @@
 
     def test_package_before_module(self):
         # A package should always be found before a module with the same name.
-        raise NotImplementedError
+        # This should not vary based on whether one is source and another is
+        # bytecode, etc.
+        module_path = os.path.join(self.directory, self.pkg_name + self.py_ext)
+        with open(module_path, 'w') as module_file:
+            module_file.write('# Testing packcage over module import.')
+        try:
+            # Source/source.
+            found = self.importer.find_module(self.pkg_name)
+            self.assert_('__init__' in found)
+            # Source/bytecode.
+            py_compile.compile(self.pkg_init_path)
+            os.unlink(self.pkg_init_path)
+            found = self.importer.find_module(self.pkg_name)
+            self.assert_('__init__' in found)
+            # Bytecode/bytecode.
+            py_compile.compile(module_path)
+            os.unlink(module_path)
+            found = self.importer.find_module(self.pkg_name)
+            self.assert_('__init__' in found)
+            # Bytecode/source.
+            # tearDown will remove the package __init__ file.
+            with open(self.pkg_init_path, 'w') as pkg_file:
+                pkg_file.write('# testing package/module import preference.')
+            os.unlink(self.pkg_init_path + ('c' if __debug__ else 'o'))
+            found = self.importer.find_module(self.pkg_name)
+            self.assert_('__init__' in found)
+        finally:
+            test_support.unlink(module_path)
+            test_support.unlink(os.path.join(self.directory,
+                                self.pkg_name + self.pyc_ext))
 
     def test_module_case_sensitivity(self):
-        # Case-sensitivity for searching after a module needs support.
-        raise NotImplementedError
+        # Case-sensitivity should always matter as long as PYTHONCASEOK is not
+        # set.
+        name_len = len(self.top_level_module_name)
+        bad_case_name = (self.top_level_module_name[:name_len/2].upper() +
+                            self.top_level_module_name[name_len/2:].lower())
+        env_guard = test_support.EnvironmentVarGuard()
+        env_guard.unset('PYTHONCASEOK')
+        with env_guard:
+            self.failUnless(not self.importer.find_module(bad_case_name))
+        if sys.platform not in ('win32', 'mac', 'darwin', 'cygwin', 'os2emx',
+                'riscos'):
+            return
+        env_guard = test_support.EnvironmentVarGuard()
+        env_guard.set('PYTHONCASEOK', '1')
+        with env_guard:
+            assert os.environ['PYTHONCASEOK']
+            self.failUnless(self.importer.find_module(bad_case_name))
 
     def test_package_case_sensitivity(self):
-        # Packages should be properly support for their case-sensitivity.
-        raise NotImplementedError
+        # Case-sensitivity should always matter as long as PYTHONCASEOK is not
+        # set.
+        name_len = len(self.pkg_name)
+        bad_case_name = (self.pkg_name[:name_len/2].upper() +
+                            self.pkg_name[name_len/2:].lower())
+        bad_init_name = os.path.join(self.directory, self.pkg_name,
+                                        '__INit__.py')
+        env_guard = test_support.EnvironmentVarGuard()
+        env_guard.unset('PYTHONCASEOK')
+        with env_guard:
+            self.failUnless(not self.importer.find_module(bad_case_name))
+            os.unlink(self.pkg_init_path)
+            with open(bad_init_name, 'w') as init_file:
+                init_file.write('# Test case-sensitivity of imports.')
+            self.failUnless(not self.importer.find_module(self.pkg_name))
+        if sys.platform not in ('win32', 'mac', 'darwin', 'cygwin', 'os2emx',
+                'riscos'):
+            return
+        os.unlink(bad_init_name)
+        with open(self.pkg_init_path, 'w') as init_file:
+            init_file.write('# Used for testing import.')
+        env_guard = test_support.EnvironmentVarGuard()
+        env_guard.set('PYTHONCASEOK', '1')
+        with env_guard:
+            assert os.environ['PYTHONCASEOK']
+            self.failUnless(self.importer.find_module(bad_case_name))
+            with open(bad_init_name, 'w') as init_file:
+                init_file.write('# Used to test case-insensitivity of import.')
+            self.failUnless(self.importer.find_module(self.pkg_name))
+
 
 
 class ExtensionFileImporterTests(unittest.TestCase):


More information about the Python-checkins mailing list