From 14874937c701c8eac451c8547b0c2e2b4cc62510 Mon Sep 17 00:00:00 2001 From: Simon Brandeis <33657802+SBrandeis@users.noreply.github.com> Date: Fri, 11 Jun 2021 18:29:00 +0200 Subject: [PATCH] Solve rmtree issue on windows (#105) --- tests/test_hf_api.py | 7 +++++-- tests/test_hubmixin.py | 3 ++- tests/test_repository.py | 3 ++- tests/testing_utils.py | 6 ++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 3972a832c6..0679bd551f 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -31,6 +31,7 @@ DUMMY_MODEL_ID, DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT, require_git_lfs, + set_write_permission_and_retry, ) @@ -118,7 +119,9 @@ def setUp(self) -> None: self.tmp_file_content = "Content of the file" with open(self.tmp_file, "w+") as f: f.write(self.tmp_file_content) - self.addCleanup(lambda: shutil.rmtree(self.tmp_dir)) + self.addCleanup( + lambda: shutil.rmtree(self.tmp_dir, onerror=set_write_permission_and_retry) + ) def test_upload_file_validation(self): with self.assertRaises(ValueError, msg="Wrong repo type"): @@ -304,7 +307,7 @@ def setUpClass(cls): def setUp(self): try: - shutil.rmtree(WORKING_REPO_DIR) + shutil.rmtree(WORKING_REPO_DIR, onerror=set_write_permission_and_retry) except FileNotFoundError: pass diff --git a/tests/test_hubmixin.py b/tests/test_hubmixin.py index 09aa7e75d8..a1f59f595f 100644 --- a/tests/test_hubmixin.py +++ b/tests/test_hubmixin.py @@ -8,6 +8,7 @@ from huggingface_hub.hub_mixin import ModelHubMixin from .testing_constants import ENDPOINT_STAGING, PASS, USER +from .testing_utils import set_write_permission_and_retry REPO_NAME = "mixin-repo-{}".format(int(time.time() * 10e3)) @@ -59,7 +60,7 @@ class HubMixingCommonTest(unittest.TestCase): class HubMixingTest(HubMixingCommonTest): def tearDown(self) -> None: try: - shutil.rmtree(WORKING_REPO_DIR) + shutil.rmtree(WORKING_REPO_DIR, onerror=set_write_permission_and_retry) except FileNotFoundError: pass diff --git a/tests/test_repository.py b/tests/test_repository.py index 8de6cf3543..a37111be0e 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -24,6 +24,7 @@ from huggingface_hub.repository import Repository from .testing_constants import ENDPOINT_STAGING, PASS, USER +from .testing_utils import set_write_permission_and_retry REPO_NAME = "repo-{}".format(int(time.time() * 10e3)) @@ -48,7 +49,7 @@ def setUpClass(cls): def setUp(self): try: - shutil.rmtree(WORKING_REPO_DIR) + shutil.rmtree(WORKING_REPO_DIR, onerror=set_write_permission_and_retry) except FileNotFoundError: pass diff --git a/tests/testing_utils.py b/tests/testing_utils.py index bbe2a1f01e..b693b1f68a 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -1,4 +1,5 @@ import os +import stat import unittest from contextlib import contextmanager from distutils.util import strtobool @@ -139,3 +140,8 @@ def offline_socket(*args, **kwargs): yield else: raise ValueError("Please use a value from the OfflineSimulationMode enum.") + + +def set_write_permission_and_retry(func, path, excinfo): + os.chmod(path, stat.S_IWRITE) + func(path)