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

Yuya Nishihara yuya at tcha.org
Mon Dec 17 12:00:56 UTC 2018


# HG changeset patch
# User Yuya Nishihara <yuya at 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.

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:


More information about the Mercurial-devel mailing list