mirror of https://github.com/pypa/pip
Use explicit default value for TempDirectory delete flag
Now we can opt-in to globally-managed + globally-configured file deletion for pre-existing directories by passing an explicit `None`.
This commit is contained in:
parent
ace7d093b2
commit
45991bcc1e
|
@ -13,7 +13,7 @@ from pip._internal.utils.misc import rmtree
|
|||
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
|
||||
|
||||
if MYPY_CHECK_RUNNING:
|
||||
from typing import Any, Dict, Iterator, Optional, TypeVar
|
||||
from typing import Any, Dict, Iterator, Optional, TypeVar, Union
|
||||
|
||||
_T = TypeVar('_T', bound='TempDirectory')
|
||||
|
||||
|
@ -77,6 +77,13 @@ def tempdir_registry():
|
|||
_tempdir_registry = old_tempdir_registry
|
||||
|
||||
|
||||
class _Default(object):
|
||||
pass
|
||||
|
||||
|
||||
_default = _Default()
|
||||
|
||||
|
||||
class TempDirectory(object):
|
||||
"""Helper class that owns and cleans up a temporary directory.
|
||||
|
||||
|
@ -101,16 +108,21 @@ class TempDirectory(object):
|
|||
def __init__(
|
||||
self,
|
||||
path=None, # type: Optional[str]
|
||||
delete=None, # type: Optional[bool]
|
||||
delete=_default, # type: Union[bool, None, _Default]
|
||||
kind="temp", # type: str
|
||||
globally_managed=False, # type: bool
|
||||
):
|
||||
super(TempDirectory, self).__init__()
|
||||
|
||||
# If we were given an explicit directory, resolve delete option now.
|
||||
# Otherwise we wait until cleanup and see what tempdir_registry says.
|
||||
if path is not None and delete is None:
|
||||
delete = False
|
||||
if delete is _default:
|
||||
if path is not None:
|
||||
# If we were given an explicit directory, resolve delete option
|
||||
# now.
|
||||
delete = False
|
||||
else:
|
||||
# Otherwise, we wait until cleanup and see what
|
||||
# tempdir_registry says.
|
||||
delete = None
|
||||
|
||||
if path is None:
|
||||
path = self._create(kind)
|
||||
|
|
|
@ -10,6 +10,7 @@ from pip._internal.utils.misc import ensure_dir
|
|||
from pip._internal.utils.temp_dir import (
|
||||
AdjacentTempDirectory,
|
||||
TempDirectory,
|
||||
_default,
|
||||
global_tempdir_manager,
|
||||
tempdir_registry,
|
||||
)
|
||||
|
@ -216,12 +217,15 @@ not_deleted_kind = "not-deleted"
|
|||
|
||||
@pytest.mark.parametrize("delete,kind,exists", [
|
||||
(None, deleted_kind, False),
|
||||
(_default, deleted_kind, False),
|
||||
(True, deleted_kind, False),
|
||||
(False, deleted_kind, True),
|
||||
(None, not_deleted_kind, True),
|
||||
(_default, not_deleted_kind, True),
|
||||
(True, not_deleted_kind, False),
|
||||
(False, not_deleted_kind, True),
|
||||
(None, "unspecified", False),
|
||||
(_default, "unspecified", False),
|
||||
(True, "unspecified", False),
|
||||
(False, "unspecified", True),
|
||||
])
|
||||
|
@ -236,6 +240,24 @@ def test_tempdir_registry(kind, delete, exists):
|
|||
assert os.path.exists(path) == exists
|
||||
|
||||
|
||||
@pytest.mark.parametrize("delete,exists", [
|
||||
(_default, True), (None, False)
|
||||
])
|
||||
def test_temp_dir_does_not_delete_explicit_paths_by_default(
|
||||
tmpdir, delete, exists
|
||||
):
|
||||
path = tmpdir / "example"
|
||||
path.mkdir()
|
||||
|
||||
with tempdir_registry() as registry:
|
||||
registry.set_delete(deleted_kind, True)
|
||||
|
||||
with TempDirectory(path=path, delete=delete, kind=deleted_kind) as d:
|
||||
assert str(d.path) == path
|
||||
assert os.path.exists(path)
|
||||
assert os.path.exists(path) == exists
|
||||
|
||||
|
||||
@pytest.mark.parametrize("should_delete", [True, False])
|
||||
def test_tempdir_registry_lazy(should_delete):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue