From 32e0973ef1e502e22e3c848dad5279b4d58e044b Mon Sep 17 00:00:00 2001 From: Nicolas Schneider Date: Wed, 6 Apr 2016 12:38:39 +0200 Subject: fix randomly failing test execution on Windows shutil.rmtree, which is used by tempfile.TemporaryDirectory, randomly fails on Windows, because the directory is not empty although it should be, because all files were deleted by shutil.rmtree internals before trying to remove the directory. A simple retry approach fixes the issue. --- run_tests.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'run_tests.py') diff --git a/run_tests.py b/run_tests.py index 3a5ccbd42..f8d6aa2a5 100755 --- a/run_tests.py +++ b/run_tests.py @@ -42,6 +42,25 @@ class TestResult: self.buildtime = buildtime self.testtime = testtime +class AutoDeletedDir(): + def __init__(self, dir): + self.dir = dir + def __enter__(self): + os.makedirs(self.dir, exist_ok=True) + return self.dir + def __exit__(self, type, value, traceback): + # On Windows, shutil.rmtree fails sometimes, because 'the directory is not empty'. + # Retrying fixes this. + # That's why we don't use tempfile.TemporaryDirectory, but wrap the deletion in the AutoDeletedDir class. + retries = 5 + for i in range(0, retries): + try: + shutil.rmtree(self.dir) + return + except OSError: + if i == retries: + raise + passing_tests = 0 failing_tests = 0 skipped_tests = 0 @@ -200,8 +219,8 @@ def parse_test_args(testdir): def run_test(skipped, testdir, extra_args, flags, compile_commands, install_commands, should_succeed): if skipped: return None - with tempfile.TemporaryDirectory(prefix='b ', dir='.') as build_dir: - with tempfile.TemporaryDirectory(prefix='i ', dir=os.getcwd()) as install_dir: + with AutoDeletedDir(tempfile.mkdtemp(prefix='b ', dir='.')) as build_dir: + with AutoDeletedDir(tempfile.mkdtemp(prefix='i ', dir=os.getcwd())) as install_dir: try: return _run_test(testdir, build_dir, install_dir, extra_args, flags, compile_commands, install_commands, should_succeed) finally: -- cgit v1.2.3