[Python-checkins] python/dist/src/Lib/test test_site.py,1.4,1.5

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Tue Jul 13 09:12:29 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8794/Lib/test

Modified Files:
	test_site.py 
Log Message:
Fixes a bug in testing code handling .pth files that did not restore the original
module that is removed for testing "import" lines.  Originally deleted the
entry from sys.modules and then just let other code that needed it to import it
again.  Problem with this solution is that it lead to code that had already
imported the module in question to have their own reference to a new copy of
the module in question that new code couldn't reach.  This lead to a failure in
test_strptime since it monkey-patched the 'time' module it had a reference to
while _strptime had its own reference to another copy of 'time' from being
imported by test___all__ that it was using for a calculation.

Also moved the testing code out of the PthFile class and into the actual test
class.  This was to stop using 'assert' which is useless with a -O execution.


Index: test_site.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_site.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** test_site.py	10 Jul 2004 02:10:45 -0000	1.4
--- test_site.py	13 Jul 2004 07:12:25 -0000	1.5
***************
*** 57,60 ****
--- 57,67 ----
                              "by _init_pathinfo(): %s" % (entry, dir_set))
  
+     def pth_file_tests(self, pth_file):
+         """Contain common code for testing results of reading a .pth file"""
+         self.failUnless(pth_file.imported in sys.modules,
+                 "%s not in sys.path" % pth_file.imported)
+         self.failUnless(site.makepath(pth_file.good_dir_path)[0] in sys.path)
+         self.failUnless(not os.path.exists(pth_file.bad_dir_path))
+ 
      def test_addpackage(self):
          # Make sure addpackage() imports if the line starts with 'import',
***************
*** 63,72 ****
          # file resides; invalid directories are not added
          pth_file = PthFile()
!         pth_file.cleanup()  # to make sure that nothing is pre-existing that
!                             # shouldn't be
          try:
              pth_file.create()
              site.addpackage(pth_file.base_dir, pth_file.filename, set())
!             unittest.FunctionTestCase(pth_file.test)
          finally:
              pth_file.cleanup()
--- 70,79 ----
          # file resides; invalid directories are not added
          pth_file = PthFile()
!         pth_file.cleanup(prep=True)  # to make sure that nothing is
!                                       # pre-existing that shouldn't be
          try:
              pth_file.create()
              site.addpackage(pth_file.base_dir, pth_file.filename, set())
!             self.pth_file_tests(pth_file)
          finally:
              pth_file.cleanup()
***************
*** 76,84 ****
          # calls addpackage() for every .pth file in the directory
          pth_file = PthFile()
!         pth_file.cleanup() # Make sure that nothing is pre-existing that is
!                            # tested for
          try:
              site.addsitedir(pth_file.base_dir, set())
!             unittest.FunctionTestCase(pth_file.test)
          finally:
              pth_file.cleanup()
--- 83,92 ----
          # calls addpackage() for every .pth file in the directory
          pth_file = PthFile()
!         pth_file.cleanup(prep=True) # Make sure that nothing is pre-existing
!                                     # that is tested for
          try:
+             pth_file.create()
              site.addsitedir(pth_file.base_dir, set())
!             self.pth_file_tests(pth_file)
          finally:
              pth_file.cleanup()
***************
*** 93,97 ****
          self.base_dir = os.path.abspath('')
          self.file_path = os.path.join(self.base_dir, self.filename)
!         self.imported = "time"
          self.good_dirname = good_dirname
          self.bad_dirname = bad_dirname
--- 101,105 ----
          self.base_dir = os.path.abspath('')
          self.file_path = os.path.join(self.base_dir, self.filename)
!         self.imported = imported
          self.good_dirname = good_dirname
          self.bad_dirname = bad_dirname
***************
*** 121,152 ****
          os.mkdir(self.good_dir_path)
  
!     def cleanup(self):
          """Make sure that the .pth file is deleted, self.imported is not in
          sys.modules, and that both self.good_dirname and self.bad_dirname are
          not existing directories."""
!         try:
              os.remove(self.file_path)
!         except OSError:
!             pass
!         try:
!             del sys.modules[self.imported]
!         except KeyError:
!             pass
!         try:
              os.rmdir(self.good_dir_path)
!         except OSError:
!             pass
!         try:
              os.rmdir(self.bad_dir_path)
-         except OSError:
-             pass
- 
-     def test(self):
-         """Test to make sure that was and was not supposed to be created by
-         using the .pth file occurred"""
-         assert site.makepath(self.good_dir_path)[0] in sys.path
-         assert self.imported in sys.modules
-         assert not os.path.exists(self.bad_dir_path)
- 
  
  class ImportSideEffectTests(unittest.TestCase):
--- 129,149 ----
          os.mkdir(self.good_dir_path)
  
!     def cleanup(self, prep=False):
          """Make sure that the .pth file is deleted, self.imported is not in
          sys.modules, and that both self.good_dirname and self.bad_dirname are
          not existing directories."""
!         if os.path.exists(self.file_path):
              os.remove(self.file_path)
!         if prep:
!             self.imported_module = sys.modules.get(self.imported)
!             if self.imported_module:
!                 del sys.modules[self.imported]
!         else:
!             if self.imported_module:
!                 sys.modules[self.imported] = self.imported_module
!         if os.path.exists(self.good_dir_path):
              os.rmdir(self.good_dir_path)
!         if os.path.exists(self.bad_dir_path):
              os.rmdir(self.bad_dir_path)
  
  class ImportSideEffectTests(unittest.TestCase):



More information about the Python-checkins mailing list