Skip to content

Commit

Permalink
Solve rmtree issue on windows (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
SBrandeis authored Jun 11, 2021
1 parent 51d260d commit 1487493
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DUMMY_MODEL_ID,
DUMMY_MODEL_ID_REVISION_ONE_SPECIFIC_COMMIT,
require_git_lfs,
set_write_permission_and_retry,
)


Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests/test_hubmixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
6 changes: 6 additions & 0 deletions tests/testing_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import stat
import unittest
from contextlib import contextmanager
from distutils.util import strtobool
Expand Down Expand Up @@ -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)

0 comments on commit 1487493

Please sign in to comment.