[Python-checkins] cpython (3.2): #15872: Add tests for a 3.3 regression in the new fd-based shutil.rmtree

hynek.schlawack python-checkins at python.org
Mon Dec 10 09:18:33 CET 2012


http://hg.python.org/cpython/rev/c9b9f786ec25
changeset:   80799:c9b9f786ec25
branch:      3.2
parent:      80796:181c170c6270
user:        Hynek Schlawack <hs at ox.cx>
date:        Mon Dec 10 09:00:09 2012 +0100
summary:
  #15872: Add tests for a 3.3 regression in the new fd-based shutil.rmtree

It cause shutil.rmtree not ignore all errors. Also add a test ensuring that
rmtree fails when being called on a symlink. Patch by Serhiy Storchaka.

files:
  Lib/test/test_shutil.py |  46 +++++++++++++++++++++++++++++
  Misc/NEWS               |   4 ++
  2 files changed, 50 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -100,10 +100,56 @@
         self.tempdirs.append(d)
         return d
 
+    @support.skip_unless_symlink
+    def test_rmtree_fails_on_symlink(self):
+        tmp = self.mkdtemp()
+        dir_ = os.path.join(tmp, 'dir')
+        os.mkdir(dir_)
+        link = os.path.join(tmp, 'link')
+        os.symlink(dir_, link)
+        self.assertRaises(OSError, shutil.rmtree, link)
+        self.assertTrue(os.path.exists(dir_))
+        self.assertTrue(os.path.lexists(link))
+        errors = []
+        def onerror(*args):
+            errors.append(args)
+        shutil.rmtree(link, onerror=onerror)
+        self.assertEqual(len(errors), 1)
+        self.assertIs(errors[0][0], os.path.islink)
+        self.assertEqual(errors[0][1], link)
+        self.assertIsInstance(errors[0][2][1], OSError)
+
     def test_rmtree_errors(self):
         # filename is guaranteed not to exist
         filename = tempfile.mktemp()
         self.assertRaises(OSError, shutil.rmtree, filename)
+        # test that ignore_errors option is honoured
+        shutil.rmtree(filename, ignore_errors=True)
+
+        # existing file
+        tmpdir = self.mkdtemp()
+        self.write_file((tmpdir, "tstfile"), "")
+        filename = os.path.join(tmpdir, "tstfile")
+        with self.assertRaises(OSError) as cm:
+            shutil.rmtree(filename)
+        self.assertEqual(cm.exception.filename, filename)
+        self.assertTrue(os.path.exists(filename))
+        # test that ignore_errors option is honoured
+        shutil.rmtree(filename, ignore_errors=True)
+        self.assertTrue(os.path.exists(filename))
+        errors = []
+        def onerror(*args):
+            errors.append(args)
+        shutil.rmtree(filename, onerror=onerror)
+        self.assertEqual(len(errors), 2)
+        self.assertIs(errors[0][0], os.listdir)
+        self.assertEqual(errors[0][1], filename)
+        self.assertIsInstance(errors[0][2][1], OSError)
+        self.assertEqual(errors[0][2][1].filename, filename)
+        self.assertIs(errors[1][0], os.rmdir)
+        self.assertEqual(errors[1][1], filename)
+        self.assertIsInstance(errors[1][2][1], OSError)
+        self.assertEqual(errors[1][2][1].filename, filename)
 
     # See bug #1071513 for why we don't run this on cygwin
     # and bug #1076467 for why we don't run this as root.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -685,6 +685,10 @@
 Tests
 -----
 
+- Issue #15872: Add tests for a 3.3 regression which caused the new fd-based
+  shutil.rmtree not ignore all errors. Also add a test ensuring that rmtree
+  fails when being called on a symlink. Patch by Serhiy Storchaka.
+
 - Issue #16559: Add more tests for the json module, including some from the
   official test suite at json.org.  Patch by Serhiy Storchaka.
 

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


More information about the Python-checkins mailing list