Patchwork [STABLE] run-tests: fix permission to clean up unreadable directories

login
register
mail settings
Submitter Yuya Nishihara
Date Dec. 17, 2018, noon
Message ID <0b83681014bfc4cde7f4.1545048056@mimosa>
Download mbox | patch
Permalink /patch/37225/
State New
Headers show

Comments

Yuya Nishihara - Dec. 17, 2018, noon
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1545047169 -32400
#      Mon Dec 17 20:46:09 2018 +0900
# Branch stable
# Node ID 0b83681014bfc4cde7f4f43fc05e9345f4645032
# Parent  120ecb17242b4edd5a14d6abb094df7d97d78835
run-tests: fix permission to clean up unreadable directories

I found many hgtests.* directories left in $TMPDIR, which couldn't be deleted
because test-ssh-repoerror.t created some directories with a-rx mode.
Augie Fackler - Dec. 18, 2018, 3:09 p.m.
On Mon, Dec 17, 2018 at 09:00:56PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1545047169 -32400
> #      Mon Dec 17 20:46:09 2018 +0900
> # Branch stable
> # Node ID 0b83681014bfc4cde7f4f43fc05e9345f4645032
> # Parent  120ecb17242b4edd5a14d6abb094df7d97d78835
> run-tests: fix permission to clean up unreadable directories

queued for stable, thanks

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -586,6 +586,17 @@  def rename(src, dst):
     shutil.copy(src, dst)
     os.remove(src)
 
+def makecleanable(path):
+    """Try to fix directory permission recursively so that the entire tree
+    can be deleted"""
+    for dirpath, dirnames, _filenames in os.walk(path, topdown=True):
+        for d in dirnames:
+            p = os.path.join(dirpath, d)
+            try:
+                os.chmod(p, os.stat(p).st_mode & 0o777 | 0o700)  # chmod u+rwx
+            except OSError:
+                pass
+
 _unified_diff = difflib.unified_diff
 if PYTHON3:
     import functools
@@ -952,7 +963,13 @@  class Test(unittest.TestCase):
                 (self._testtmp.decode('utf-8'),
                  self._threadtmp.decode('utf-8')))
         else:
-            shutil.rmtree(self._testtmp, True)
+            try:
+                shutil.rmtree(self._testtmp)
+            except OSError:
+                # unreadable directory may be left in $TESTTMP; fix permission
+                # and try again
+                makecleanable(self._testtmp)
+                shutil.rmtree(self._testtmp, True)
             shutil.rmtree(self._threadtmp, True)
 
         if self._usechg: