mirror of https://github.com/pypa/pip
Assert TempDirectory.path has not been cleaned up on access
This commit is contained in:
parent
7ea1fcdb13
commit
e6bde63620
|
@ -54,12 +54,16 @@ class TempDirectory(object):
|
|||
path = self._create(kind)
|
||||
|
||||
self._path = path
|
||||
self._deleted = False
|
||||
self.delete = delete
|
||||
self.kind = kind
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
# type: () -> str
|
||||
assert not self._deleted, (
|
||||
"Attempted to access deleted path: {}".format(self._path)
|
||||
)
|
||||
return self._path
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -88,8 +92,9 @@ class TempDirectory(object):
|
|||
def cleanup(self):
|
||||
"""Remove the temporary directory created and reset state
|
||||
"""
|
||||
if os.path.exists(self.path):
|
||||
rmtree(self.path)
|
||||
self._deleted = True
|
||||
if os.path.exists(self._path):
|
||||
rmtree(self._path)
|
||||
|
||||
|
||||
class AdjacentTempDirectory(TempDirectory):
|
||||
|
|
|
@ -55,7 +55,26 @@ def test_deletes_readonly_files():
|
|||
create_file(tmp_dir.path, "subfolder", "readonly-file")
|
||||
readonly_file(tmp_dir.path, "subfolder", "readonly-file")
|
||||
|
||||
assert not os.path.exists(tmp_dir.path)
|
||||
|
||||
def test_path_access_after_context_raises():
|
||||
with TempDirectory() as tmp_dir:
|
||||
path = tmp_dir.path
|
||||
|
||||
with pytest.raises(AssertionError) as e:
|
||||
_ = tmp_dir.path
|
||||
|
||||
assert path in str(e.value)
|
||||
|
||||
|
||||
def test_path_access_after_clean_raises():
|
||||
tmp_dir = TempDirectory()
|
||||
path = tmp_dir.path
|
||||
tmp_dir.cleanup()
|
||||
|
||||
with pytest.raises(AssertionError) as e:
|
||||
_ = tmp_dir.path
|
||||
|
||||
assert path in str(e.value)
|
||||
|
||||
|
||||
def test_create_and_cleanup_work():
|
||||
|
@ -66,7 +85,6 @@ def test_create_and_cleanup_work():
|
|||
assert os.path.exists(created_path)
|
||||
|
||||
tmp_dir.cleanup()
|
||||
assert tmp_dir.path is not None
|
||||
assert not os.path.exists(created_path)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue