[Python-checkins] cpython (2.7): Improve the test case to avoid spurious errors about already existing symlinks.

jason.coombs python-checkins at python.org
Thu Mar 8 16:43:30 CET 2012


http://hg.python.org/cpython/rev/c3bd31d3c7f5
changeset:   75495:c3bd31d3c7f5
branch:      2.7
user:        Jason R. Coombs <jaraco at jaraco.com>
date:        Thu Mar 08 10:31:29 2012 -0500
summary:
  Improve the test case to avoid spurious errors about already existing symlinks.

files:
  Lib/test/test_import.py |  18 ++++++++++++++----
  1 files changed, 14 insertions(+), 4 deletions(-)


diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -494,7 +494,10 @@
     package_name = 'sample'
 
     def setUp(self):
-        if os.path.exists('sample-tagged'): shutil.rmtree('sample-tagged')
+        if os.path.exists(self.tagged):
+            shutil.rmtree(self.tagged)
+        if os.path.exists(self.package_name):
+            self.remove_symlink(self.package_name)
         self.orig_sys_path = sys.path[:]
 
         symlink = getattr(os, 'symlink', None) or self._symlink_win32
@@ -583,23 +586,30 @@
     # regression test for issue6727
     @unittest.skipUnless(
         not hasattr(sys, 'getwindowsversion')
-        or sys.getwindowsversion() >= (6,0),
+        or sys.getwindowsversion() >= (6, 0),
         "Windows Vista or later required")
     def test_symlinked_dir_importable(self):
         # make sure sample can only be imported from the current directory.
         sys.path[:] = ['.']
 
         # and try to import the package
-        pkg = __import__(self.package_name)
+        __import__(self.package_name)
 
     def tearDown(self):
         # now cleanup
         if os.path.exists(self.package_name):
-            os.rmdir(self.package_name)
+            self.remove_symlink(self.package_name)
         if os.path.exists(self.tagged):
             shutil.rmtree(self.tagged)
         sys.path[:] = self.orig_sys_path
 
+    @staticmethod
+    def remove_symlink(name):
+        # On Windows, to remove a directory symlink, one must use rmdir
+        try:
+            os.rmdir(name)
+        except OSError:
+            os.remove(name)
 
 def test_main(verbose=None):
     run_unittest(ImportTests, PycRewritingTests, PathsTests,

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list