Move permissions fixes to general make_tempdir implementation

This commit is contained in:
Paul O'Leary McCann 2023-01-24 19:04:31 +09:00
parent 7268c4b94d
commit 13fe001b15
2 changed files with 12 additions and 16 deletions

View File

@ -1,11 +1,8 @@
import os
from pathlib import Path
import pytest
import shutil
import srsly
import stat
from typer.testing import CliRunner
import tempfile
from spacy.cli._util import app
from .util import make_tempdir
@ -104,23 +101,12 @@ def test_project_run(project_dir):
def test_project_clone():
# Git clones create some readonly/hidden files. On Windows, trying to
# delete these gives an error, so we have to handle that specially.
workspace = Path(tempfile.mkdtemp())
def _fix_perms(func, path, exc_info):
os.chmod(path, stat.S_IWUSR)
func(path)
try:
with make_tempdir() as workspace:
out = workspace / "project"
target = "benchmarks/ner_conll03"
result = CliRunner().invoke(app, ["project", "clone", target, str(out)])
assert result.exit_code == 0
assert (out / "README.md").is_file()
finally:
shutil.rmtree(str(workspace), ignore_errors=False, onerror=_fix_perms)
def test_project_push_pull(project_dir):

View File

@ -32,6 +32,7 @@ import inspect
import pkgutil
import logging
import socket
import stat
try:
import cupy.random
@ -1032,9 +1033,18 @@ def make_tempdir() -> Generator[Path, None, None]:
YIELDS (Path): The path of the temp directory.
"""
d = Path(tempfile.mkdtemp())
# On Windows, attempts to delete a git directory fail with permissions
# errors due to read only / hidden files. This mainly addresses that issue.
def _fix_perms(func, path, exc_info):
# IWUSR = give user write permission
os.chmod(path, stat.S_IWUSR)
# func is the function that gave the error, so it behaves like rmtree.
func(path)
yield d
try:
shutil.rmtree(str(d))
shutil.rmtree(str(d), ignore_errors=False, onerror=_fix_perms)
except PermissionError as e:
warnings.warn(Warnings.W091.format(dir=d, msg=e))